Advertisement Jump to content
Sign in to follow this  


This topic is 3874 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

Im processing these messages and setting the states of my keys as such WM_KEYDOWN if wparam < 255 keystates[wparam] = true WM_KEYUP if wparam < 255 keystates[wparam] = false and then depending on my code, performing actions based off the keys states such as if keystates['M'] renderMap = !rendermap which im using as a toggle to display my map or not, but i seem to be having the issue where when the 'm' is pressed *down* it seems to toggle on and off, many times, meaning that you have to keep pressing it until your lucky enough it toggles th way you want, Can anybody tell me why this would be happening

Share this post

Link to post
Share on other sites
The way you have it right now, the rendermap variable will toggle every frame the 'M' key is held down. What you probably want is for the rendermap variable to toggle if the 'M' key is HIT, not pressed.

To detect key hits, you'll need to check if the key is currently down, but not down the previous frame. So, you'll have to create an array that holds the key states from the previous frame, like this (in C++):

for (int i = 0; i < 255; i = i + 1)
keystatesLastFrame = keystates;

if (wparam < 255)
keystates[wparam] = true;
keystates[wparam] = false;

and to check for a key HIT, you'd do

if (keystates['M'] && !keystatesLastFrame['M'])
rendermap = !rendermap

Hope this helps!


Share this post

Link to post
Share on other sites
You could also do
if (keystates['M'])
renderMap = !renderMap;
keystates['M'] = false;

No extra variables needed, and it works!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!