Advertisement

Key Down problem

Started by July 04, 2001 08:45 AM
4 comments, last by Cobramania 23 years, 5 months ago
Hi all, i have this function to move the sprite: if (KEY_DOWN(VK_UP)) construction_cursor_y-=2; if (KEY_DOWN(VK_DOWN)) construction_cursor_y+=2; if (KEY_DOWN(VK_LEFT)) construction_cursor_x-=2; if (KEY_DOWN(VK_RIGHT)) construction_cursor_x+=2; the problem is, i want if i hold the button, the function will run only once. But now, since the game run in real time, that function is always run when i press and hold the button. I need a solution so the function will run only once. Thank you very much
Hi!

This is an idea I got from a NeHe''s tutorial.

//globals
bool keys[256];
bool keyhold[256];

bool CheckKeyPress(int IDKEY)
{
if(keys[IDKEY] && !keyhold[IDKEY])
{
keyhold[IDKEY]=true;
return true;
}
else
return false;
}

When you catch WM_KEYDOWN make keys[VK_?]=true
When you catch WM_KEYUP make keys[VK_?]=false and keyhold[VK_?]=false
VK_? should be WPARAM in WndPRoc I think....

This function will return true if the key was pressed but only once per key press, until the key is released, like:

if(CheckKeyPress(VK_RIGHT)
construction_cursor_x+=2;


That should do I guess...
Let me know if it helps, dude!!

See ya!


oro...?
SKeTch
Advertisement
I have an idea for you. I am having the exact same problem with my game (I''m using that exact same function too) exept my object never moves! If you can at least get yours moving once, you should put it in some kind of game loop.

C++
C++
DO NOT LISTEN TO MY ADVICE!
WHATEVER YOU DO, ITS WRONG AND YOU WILL WASTE YOUR TIME!

C++
C++
Hi SketchTurner,

your soulution works really smooth, thanks........
i figured it out myself last night, but with single bool variable for every key,
and you give me the solution using array and external function.

Thank you very much
I''m glad I helped.

See ya around man, keep it up!

oro...?
SKeTch

This topic is closed to new replies.

Advertisement