🔒 Locked
Keyboard input
Advertisement
Original Post
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
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:
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:
Any approach for that?
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.
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.
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 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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement