Check if specific key is pressed

Aug 16, 2014 at 8:58pm
Hello

I want to know how to check if specific keys are pressed.
But also on different systems.

How to do this for Windows systems?
How to do this for all systems (Windows, Linux, Mac,...)?

I already searched but only found a Windows solution and something about key-codes, are those 'codes' the same on all systems?

Thanks for reading,
Niely
Aug 16, 2014 at 9:35pm
How to do this for Windows systems?


The easiest way to get real-time key state on Windows is with GetAsyncKeyState:

1
2
3
4
if(GetAsyncKeyState('A') & 0x8000)
{
    // the 'A' key is currently being held down
}


How to do this for all systems (Windows, Linux, Mac,...)?


There is no standard solution that works on all systems. If you want crossplatform support, you'll need an additional library. Libraries like SFML, SDL, wx, Qt, ncurses all have keyboard polling support.

I already searched but only found a Windows solution and something about key-codes, are those 'codes' the same on all systems?


Absolutely not. Every OS works completely differently.

If you want portability across multiple OS's, then you want an external lib like the above mentioned ones. They provide a simplistic interface that works on multiple OS's
Aug 16, 2014 at 10:33pm
Thanks for your awesome reply! :)
And I'm currently about to indeed start working a lot with SFML.
(If I know how to attach the SFML Library to the Geany GUI).

Didn't knew it was possible with that as well.
Thanks a lot man!
Aug 16, 2014 at 11:16pm
Equivalent SFML code for checking key state:

1
2
3
4
if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
{
    // the 'A' key is currently being held down
}
Aug 17, 2014 at 7:42am
Thanks for mentioning that one as well.
Topic archived. No new replies allowed.