Skip to main content
GameDev.net gamedev.net
🔒 Locked

Detecting a Key Release??

Started by _damN_ Nov 5, 2010 at 1:38 AM 4 replies 3.5k views
Original Post
_damN_
_damN_
Hi...

Im trying to write a function that detects when a specific key is released after it has been pressed.

I can detect when a key is pressed like:

bool KeyPressed(UINT uiID)
{
if(m_uiKeys[uiID])
return true;
return false;
}

I cant for the life of me detects when it is released in a different function...

bool KeyReleased(UINT uID)
{
// Code here...
}

Whats the best way to detect the key release. Im using C++ and the Win32 API...

Thanks!
_damN_
_damN_
What do you mean?
NumberXaero
NumberXaero
Build a key state for every key, containing, flags down, firstDown, and firstUp.
When input is received update the target key. Initially, all key flags are false.

So you get a down event

UpdateKey(bool up){    if(!up)    // down now...    {        if(mbDown) {       // ... and down before            mbFirstDown = false;     // no longer first down        } else {      // down, but not down before            mbFirstDown = true;     // first down        }                mbDown = true; // key now down    }    else    // up now    {        if(mbDown) {     // down before, now up            mbFirstUp = true;       // up (released) after being down by press        }            mbDown = false;    // not down anymore        mbFirstDown = false;    // not init down    }}


Then check this key state directly when you need to in your code
szecs
szecs
I misunderstood it a bit, because you don't show us relevant pieces of the code.
For example do you ever set m_uiKeys[] anywhere?

And can't you simply use
bool KeyPressed(UINT uiID){if(m_uiKeys[uiID])return true;return false;}//I cant for the life of me detects when it is released in a different function...bool KeyReleased(UINT uID){if( ! m_uiKeys[uiID])return true;return false;}

?

How do you handle input anyway? You poll the keyboard state or you use events? You have to show all input related code.
rip-off
rip-off
You can maintain parallel arrays of previousState, currentState booleans. Alternatively, use a event based model.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.