Check if specific key is pressed

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
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
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!
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
}
Thanks for mentioning that one as well.
Registered users can post here. Sign in or register to post.