Sign in to follow this  

Keyboard input

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this