I am making a wrapper for keyboard input using Direct Input. To grab the key states, the function GetDeviceState() is called with a char buffer.
This is all well and good, but now to send key events I must iterate through the buffer and check against all the keys that were pressed. I was hoping there would be a callback instead that only passed the key codes that were pressed.
If anybody has experience with Direct Input, is iterating through the key code buffer the only way to check for key presses?
That answer is to use GetDeviceData() instead. You will be able to get whether the key was pressed or released and the offset of the key that was pressed, which is pretty close to what I was looking for as now I can initiate my own callbacks and the overhead if no key is pressed is minimal.
Related
Is there a way of getting a list of all currently pressed keys in a Lanterna Terminal/Screen?
i.e. not getting the last pressed key, neither waiting for a key to be pressed. I'd like to get a list of all keys that are currently being held down.
I realize this could be a limitation with lanterna with terminals. If so, is there an alternative for implementing a text UI that support polling pressed keys?
I don't think the software interface to keyboards typically exposes a way to ask this question. As far as I know, the best approach is to build it yourself: watch for key-down and key-up events, and maintain a set of the keys which have gone down without a later up event.
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.
From MSDN , I have learned that GetKeyState is associated with message queue of current thread.
Then I created two sample applications : KeyPresser and BackChecker.
I press a key in KeyPresser , in this application , I am using GetKeyState/GetAsyncKeyState/GetKeyboardState to retrieve the pressed key state , and they tell me that key is pressing down.
Then I send(or post) a message form KeyPresser to BackChecker , to notify BackChecker to check the key state in it's thread .
I get same result from BackChecker indicate that key is pressed. But I think GetKeyState/GetKeyboardState should return zero because the key is pressed in thread of KeyPresser , which is not associated with thread of BackChecker.
Why?
Keyboard input on Windows is buffered. It ensures the user can keep typing, even if the program is temporarily unresponsive. It always is, to some degree, no loss of keystrokes that way. They are added to the message queue, the program's message loop retrieves them with GetMessage() later.
Along with the keystroke, it also stores the state of all the other keys. To ensure that, when the message is eventually retrieved, you can reliable tell what other keys were down. Very important for the modifier keys for example. A shortcut keystroke like Ctrl+A would not work reliably otherwise.
So you generally always use GetKeyState(), you get the state of the keys as they were recorded originally. Or GetKeyboardState(), you get the whole enchilada. Using GetAsyncKeyState() is much less common, it doesn't rely on the buffered state, only needed if the app has very unusual message handling. Might be appropriate in a game perhaps.
I'm making a program that will take keystrokes entered and change the output to form a message of my choosing. I'm thinking of using GetAsyncKeystate() to see if a key is down, but I'm not sure how to change the value of the key pressed.
Your best bet is a low-level keyboard hook. You don't get a ton of context, but you do get the raw keystrokes. If you need context, then you're probably looking at a text service via TSF, but that tends to get complex quickly.
I am writing a C++ CLI application how can I detect if any key is pressed by the user. I've seen that in c# but how can it be implement in c++
while(1)
{
while(/* code to check if any key is pressed*/)
{ //rest of the code
// sleep function
}
}
Hint: like in CLI games to move or to take certain action when a key is pressed or don't do any thing if no input is given.
On windows at least you could use GetKeyState
we can use _kbhit() function in c++. _kbhit is equal to 1 if any key is pressed. You have to clear the _kbhit buffer else it will remain 1. Method for clearing is character = getch(); This will save the last entered key in character which you can compare and decide which action to perform on which key.
While loop can be CPU consuming, i do not advice busy waiting method, instead you should think of event hooking.
Here you can read about winapi keystroke event hooking C++ Win32 keyboard events
If you are still interested to use the while loop, you should also free some resources by sleeping after checking that a condition is false (e.g. nanosleep )