Keyboard input

Started by
8 comments, last by Sneftel 17 years, 1 month ago
Hi, I'm looking for a simple way to read single characters from the keyboard. I want to use this in a directx game so I can't use the normal console commands. Also I don't want to use DirectInput. My first idea is to run the GetKeyboardState function to get all states of the keyboard. In this case I would have to check each value whether its pressed or not. Is there any other way to read the pressed key directly? Cheers Zerd
Advertisement
What language? What application type? Assuming C++, you can check for WM_CHAR in your message pump if you're using a Win32 application, or GetAsyncKeyState() if you're using a console app (Or a Win32 app in fact).

Example of GetAsymcKeyState:
bool IsKeyPressed(int nKey){   return GetAsyncKeyState(nKey) & 0x8000;}if(IsKeyPressed('A')){   // A is presed}if(IsKeyPressed(VK_ESCAPE)){   // Escape is presed}
Yes you are right, C++.
Thanks Steve, you allways try to help me ;) Very kind!

But the problem with this method is that I have to handle every key of the keyboard. So this would probably be a little bit of a mess:
if(IsKeyPressed('A')) { ..}if(IsKeyPressed('B')) { ..}...


Any approach for that?
If you're going to be checking a large number of keys, then GetKeyboardState() is the way to go. If it's only going to be a few (10 or so), then GetAsyncKeyState() shouldn't hurt much.
In my engine, I use WM_KEYDOWN and WM_KEYUPin my message pump to record the status of all keys in an array of bools. Then I can just check each bool as I want from my code.
I need this for an own implementation of an inputBox for the GUI I'm working on. So yes it is a larger number of keys... Basicly all characters and numbers.
Don't use anything except WM_KEYDOWN and WM_CHAR for implementing an input box. If you do it any other way, key-repeat will feel all weird and your code won't work on international keyboards.
Since I'm implementing the "Immediate Mode Graphical User Interface"-idea I don't use the Windows Message pumps... I didn't have any trouble implementing the other elements of the GUI, the inputBox is the first serious problem...
Quote:Original post by Zerd
Since I'm implementing the "Immediate Mode Graphical User Interface"-idea I don't use the Windows Message pumps

IMGUI stuff doesn't prevent you from listening for windows messages.
Mh.. In this case, what is the best way to pass the messages to the GUI system?

I have a class GUI which handles all GuiElements. In the main-loop gui.renderAll() is called to display everything which is registered to the GUI. Right now GUI is no global variable. So if I would work with the Windows message pump I'd have to define the GUI-instance global to be able to access it from the WindowProc right? (Sorry I'm new to IMGUI... Until now I thought there should be NO message-routing between the elements...)
I couldn't really say without being more familiar with the details of your design. It is possible; it's just a matter of figuring out how to do it most elegantly. Personally, I've found a registered listener approach to work relatively well: the GUI registers itself as a listener for whichever class does wndproc stuff, and gets called back on WM_CHAR.

This topic is closed to new replies.

Advertisement