I'm creating a simple 3D game on Windows 7 in C++ using the free version of the Havok physics engine. I want to use the WASD keys to move the character. The structure of the code is such that I need to capture this input asychronously; there is a function called in every frame of the scene to update the character's position (I want to try checking if a key is currently pressed instead of using some kind of listener for events). I searched around for a good solution, as I know little to nothing about win32 functions, and put this together:
if (GetAsyncKeyState(0x41) & 0x8000) posX=-1.0f; //A
if (GetAsyncKeyState(0x44) & 0x8000) posX=1.0f; //D
if (GetAsyncKeyState(0x57) & 0x8000) posX=1.0f; //W
if (GetAsyncKeyState(0x53) & 0x8000) posX=-1.0f; //S
After checking with some printf statements, the visual debugger doesn't seem to be picking up any input with this. I know of WM_KEYDOWN and WM_KEYUP, but I can't find simple explanations on how to use them, and as far as I can tell they are more event-based than asynchronous.
Is there a problem with the snippet above, or should I try another approach?
Best guess: You're checking for "A" instead of "a". Unless of course you hold down the shift key as well, just pressing the a-key won't trigger your code.
It appears that my problem wasn't GetAsyncKeyState() after all, but my use of FindWindow() and GetWindowRect(). It wasn't recognizing that the current window was the visual debugger. Fixed.
Related
I would like to know how to detect the keyboard adapter on Windows when using two keyboards, that a key eg. W was pressed on first or on the second keyboard.
Can anybody help me how to do it?
I would like to use a cheap solution for macros. I would plan to bind macros for entrie second keyboard, when somebody press key W (just for example), keybaord would type a world uint8_t.
I am trying to make a text box in allegro and need a way of getting the ascii keycodes from the key presses. The ev.type == ALLEGRO_EVENT_KEY_DOWN does not always work. I have tried getting the event to work faster but it is still slow.
If there is a way I could make this into a function that could give the Ascii char of what ever key is pressed, it would be great. (I have been looking but I cant find something easy and fast for the source code that I am using)
Perhaps you are looking for an ALLEGRO_EVENT_KEY_CHAR event type. These events are generated every time a character is typed on the keyboard, or auto-repeated because the key was held down long enough. In other words, while ALLEGRO_EVENT_KEY_UP/DOWN events correspond to the keyboard state, ALLEGRO_EVENT_KEY_CHAR events correspond to the character input buffer state.
I'm starting to work on a really basic 3D engine to get started with OpenGL. All the garphics are fine, and I'm now working on keyboard controls.
To to that, I implemented a class Camera, which holds a activateKey(GLint) method. It is meant to store, when a SDL_KEYDOWN event is received in the main loop, the value of the key pressed, doing camera->activateKey(windowEvent.key.keysym.sym);. For instance, when I press 'W' to go forward, the array GLboolean keys[1024] is going to be filled at the SDLK_w index with GL_TRUE. This will allow me to control pressed key at every loop and move the camera acordingly to the user will.
But when I press an 'exotic' key (like F9... I know, it's not that exotic, but it's not a letter), and I try to store GL_TRUE in keys[] at the corresponding index, I get an access violation. I managed to find that the GLint value for F9 is 1073741890, whereas W is 119. So it's clearly an out of bound exception.
My question is: Is there a more elegant way to solve my problem than controlling just before filling keys[] with GL_TRUE that the key is included in 'WSAD'? Maybe the type of the keys[] array is improvable?
One way to go, is to use SDL_SCANCODE's instead of SDLK. Those are mapped on the QWERTY keyboard (so AZERTY owners will still have to map the controls like an QWERTY keyboard) and are a Uint8 type, so far below 1024. Basically, one can do:
GLboolean keys[256];
and call
camera->activateKey(windowEvent.key.keysym.scancode);
to compare inside the methods to values like SDL_SCANCODE_W.
If someone has a more optimized answer, I'll gladly take it, because I fear compatibility issues between OS's, and keyboard types.
EDIT
Thanks to Ben (see comments), I would add that it's preferable to get rid of the scancodes as soon as possible; by this, I mean converting scancodes into custom program commands (like GO_FORWARD) and manipulating those instead. This ables a much easier handling of events and key remapping.
I'm trying to write a Keyboard class that can read in the keyboard buttons. I have looked at this link - http://www.daniweb.com/software-development/cpp/code/216732/reading-scan-codes-from-the-keyboard But as stated on there, it is not very accurate for all computers (I don't know if this is even true). Therefore, my question is whats the best method in implementing my keyboard class? This will be used for Windows
Many thanks
There are three ways to read keyboard input:
By reading input from a console window as described in your link. It's true that it's hard to get this to work correctly, for starters because it's reading ANSI characters and not Unicode characters, but there are other issues. Console input/output is kind of obscure, as is the documentation for it
By handling UI events associated with a normal window. In this case you would handle the WM_KEYDOWN message in a window procedure
By going deep into the Win32 API with functions like SetWindowsHookEx. In this case you don't even need a window (normal or console), and you can read keystrokes pressed in any application or in the desktop
It's hard to suggest which one to use without knowing how you intend to use this Keyboard class.
The down arrow key of my laptop is very loose and it does not seems to last very long.
Is it possible to write any programm(in any language but especially C++) hat simulates the down arrow key.say I made a programm such that when I press A,B,C on the key board it simulates down arrow key.
If not then,
Is there any software available to do this?
Use the On-screen keyboard
If you want to simulate input, use the SendInput API. This injects input at a fairly low level, windows automatically routes it to the appropriate thread based on who has focus. Call it twice, once to send the key down, and again to send the key up.
Perhaps the easiest thing to do is to write a simple app that calls RegisterHotkey for some combination like ctrl-alt-Z, and then calls SendInput for a keypress then keyrelease of the down arrow key.
You might need to wait a short time after receiving WM_HOTKEY to give you time to release the set of hotkeys so that the down arrow gets processed alone without those modifiers from your hotkey interfering with it. (...otherwise the focused app might think you typed in shift+alt+downarrow instead of plain downarrow!)
if you're using linux, xmodmap: http://www.xfree86.org/4.2.0/xmodmap.1.html
I think that you are actually looking for Sharpkeys www.randyrants.com/sharpkeys/
This works with the windows registry and can be used to change mappings of keys.
You can easily write a program that sends WM_KEYDOWN and WM_KEYUP messages to the window which has the current focus. Once you have this program, bind it to a function key in the properties for the .exe file.