Jump to content

bool KeyPressed() - How to implement this?!

Please view my earlier topic for details : https://linustechtips.com/main/topic/565178-screw-the-key-pause/

 

I would like to know how to implement the bool KeyPressed() flag in my code. A snippet of code as an example will definitely help...

 

Thanks a ton!

Nothing to see here ;)

Link to comment
https://linustechtips.com/topic/573467-bool-keypressed-how-to-implement-this/
Share on other sites

Link to post
Share on other sites

2 minutes ago, Paralectic said:

That's too complex - I don't understand anything! Please help me with this... I am still only capable of developing console "games" in C++. Why don't you post a very simple example - the function handles a keypress, and the definition of the function.

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

As from Stackoverflow.

GetAsyncKeyState() is what you're looking for. It reads the physical state of the keyboard, regardless of the input queue state. If the high-bit is set, then the key was down at the time of the call.

// Fetch tab key state.
SHORT tabKeyState = GetAsyncKeyState( VK_TAB );

// Test high bit - if set, key was down when GetAsyncKeyState was called.
if( ( 1 << 16 ) & tabKeyState )
{
    // TAB key down... 
}

And here is the documentation for the used function GetAsyncKeyState()

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx

Link to post
Share on other sites

you need 2 arrays of booleans (each element represents a key, true = down, false = up)

1 array represents the state of the previous update

the other represents the state of the current update

bool KeyPressed(int keyCode)
{
	return !oldKeys[keyCode] && currentKeys[keyCode];
}

each update you will have to copy over currentKeys to oldKeys and update currentKeys

if using a HWND it will receive events of key state changes

Link to post
Share on other sites

13 hours ago, Paralectic said:

As from Stackoverflow.


GetAsyncKeyState() is what you're looking for. It reads the physical state of the keyboard, regardless of the input queue state. If the high-bit is set, then the key was down at the time of the call.

// Fetch tab key state.
SHORT tabKeyState = GetAsyncKeyState( VK_TAB );

// Test high bit - if set, key was down when GetAsyncKeyState was called.
if( ( 1 << 16 ) & tabKeyState )
{
    // TAB key down... 
}

And here is the documentation for the used function GetAsyncKeyState()

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx

Thanks man! Will try it out!

Nothing to see here ;)

Link to post
Share on other sites

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

×