• ### What is your GameDev Story?

This topic is 4906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wrote a program, here is a bit of it: case WM_KEYDOWN: if(wParam == VK_DOWN) { glm.menu_index += 1.0f; if(glm.menu_index == 4.0) glm.menu_index = 0.0; } if(wParam == VK_UP) { glm.menu_index -= 1.0f; if(glm.menu_index == -1.0) glm.menu_index = 3.0; } if(wParam == VK_RETURN) if(glm.menu_index == 3.0) PostQuitMessage(0); break; Now the question is... When i move the cursor with the keboard it moves toooo fast... which kinda makes me miss the menu selection i need... it is annoying... is there anyway to fix it without Sleep(); ??? Basically i need a slower reaction when i press those buttons in the menu... Thanks in advance! Spirit.

##### Share on other sites
it looks like all of your movement is based on constant values. it could process a VK_UP message hundreds of times a second (depending on how fast your computer is ). because this isnt based on movement, but selection, a flag for each button could help. something like this

if ( wParam == VK_UP ){    //make sure the button wasnt pressed on the last iteration    if ( !upFlag )    {       //do up stuff here    }    //we dont care what happened the last iteration, if it was    //up or down, its now been pressed, so keep this true until    // if(wParam==VK_UP) returns false    upFlag = true;}else{    //not pressed, so now the next time the if statement evaluates to true    //(which is the next time you press the button down) it will do your     //menu stuff    upFlag = false;}

this way, it will only move once per button press. if you wanted it to move as the button is being held down, that is a little more complicated, i could give some psuedo code for that if that is what are you aiming for. hopefully this will do what you want.

##### Share on other sites
that AP was me, wasnt logged in for some reason...

##### Share on other sites
Thanks that is good... But a new problem bornes... I can't scroll down the menu many time if the key is down... I have to press it 3 times if i want to go down or up, but i want it to be able to do this with one click... Only slower :)

##### Share on other sites
Right which is what i was thinking you may have wanted. If you can, do something like this:

static const int SCROLL_MILLI = 50;....float timeHeld = 0;///if ( wParam == VK_UP ){    if ( !upFlag || timeHeld >= SCROLL_MILLI )    {        timeHeld = 0;//reseting our counter        //do our menu stuff    }    //increments the time held    timeHeld += GetTickCount();// GetTickCount or equivalent timing method    upFlag = true;}else{    upFlag = false;    timeHeld = 0;//resets counter}

what this should do, with some tweaking ( i havent tested it ) is that it will move the menu item instantly the first time it was pressed, then wait 50 milliseconds (half a second) before doing it again, and so on.. and so on. if you want, you can change the response time, or can even make the response time faster (ie smaller) with each successive menu movement, like making SCROLL_MILLI not a constant, and where you do the menu stuff, do like a SCROLL_MILLI -= 15, and where you set the upFlag to false, reset SCROLL_MILLI to its default value.

hopefully this gives you the flexibility you need. if this codes work (it might even compile), let me know.

##### Share on other sites
Oh, well then just add a timer to it.
Every time you flick to a new menu option, you set a egg-clock to half a second. Whenever you try to flick to a new option, before actually selecting th new thing you check if the egg-clock is still running.

Something like this:
case WM_KEYDOWN:if((wParam == VK_DOWN) && (GetCurrentTime() > timer)) // check if timer is up yet{glm.menu_index += 1.0f;if(glm.menu_index == 4.0)glm.menu_index = 0.0;timer = GetCurrentTime() + 500 // set the timer to half a second into the future}if((wParam == VK_UP) && (GetCurrentTime() > timer)) // check if timer is up yet{glm.menu_index -= 1.0f;if(glm.menu_index == -1.0)glm.menu_index = 3.0;timer = GetCurrentTime() + 500 // set the timer to half a second into the future}

This doesnt move your selection until 500 milliseconds have passed since the last time you moved the selection.
How you get the current time is up to you. It depends on what libraries you are using. Usually graphic libraries include such a function, and lots of others do as well.

##### Share on other sites
Cool thanks! i liked the one with the timer :)

##### Share on other sites
If you respond only to WM_KEYDOWNs, the autorepeat function will kick in and you won't ever have to worry about timers. This is the standard way to handle key holds in windows. That timer method is messy and if you plan on using it, then it should at least be encapsulated in an input manager.

##### Share on other sites
hehe, beat you with a similar answer by 50 seconds. i don't like using windows messaging (direct input anyone?) so im not familiar with the messages and their behavoirs, but either way, skittleo is probably right.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015231
×