Jump to content
  • Advertisement
Sign in to follow this  
Claymz

WM_KEYDOWN message pause

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

[color=#000000][font=verdana, arial, helvetica, sans-serif]

Hi all![/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

First of all, some additional info:[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]

In the wParam parameter of WM_KEYDOWN message, first 15 bits have the following meaning:[/font]
[color=#000000][font=verdana, arial, helvetica, sans-serif]

"0-15 The repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative."[/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

So, what i want to do is, when i hold down a certain button on keyboard (or a mouse for that matter) when playing a game(for steering), i would like that this button would continuously be sending WM_KEYDOWN message, without the pause, which occurs after the first message - that is, if you hold down button, first message is sent, than for a second nothing happens, and after that, if your still holding down that same button, these type of messages are sent quick one after another. How to accomplish such a funcionality? If your not sure what i mean, you can open text editing program such as textbook and hold one specific character on the keyboard down, and you will see the short pause right after the first output of that same character, after that, it will be outputed constantly.[/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

Thank you in advance![/font]

Share this post


Link to post
Share on other sites
Advertisement
Don't use repeating messages to handle continuous input. All you need to know is that, if you have received a key-down message, then the key is pressed, and it remains pressed until you receive a key-up message. Keep a state of the key, and read that state in your game loop instead.

Share this post


Link to post
Share on other sites
And what would be the most simple solution to doing this (what function to use to get the information about certain key not pressed any more)?

Share this post


Link to post
Share on other sites
Well, now ive got this:

bool qDown, eDown = false;
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_KEYDOWN:
switch(wParam)
{
case 'Q':
qDown = true;
break;
case 'E':
eDown = true;
break;
}
return 0;
case WM_KEYUP:
{
case 'Q':
qDown = false;
break;
case 'E':
eDown = false;
break;
}
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if(qDown)
{
//do sth
}
if(eDown)
{
//do sth
}
}


Problem is, if i press 'E', itll act as if i wouldve hold it, even when i press 'Q' and then release it, it acts as if right after that i wouldve hold the 'E' key again.

Share this post


Link to post
Share on other sites
Your switch statement looks mighty strange - shouldn't you have a second (inner) switch inside each case for switch (msg)?

In any event, I would use something like this instead:

// as a global these will be auto-inited to false
bool keydown[256];

LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
keydown[(int) wParam] = true;
return 0;

case WM_KEYUP:
keydown[(int) wParam] = false;
return 0;
}

return DefWindowProc (hWnd, msg, wParam, lParam);
}


while (msg.message != WM_QUIT)
{
if (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
{
if (keydown[0x51]) // 'Q'
{
DoQKeyDownStuff ();
}

if (keydown[0x45]) // 'E'
{
DoEKeyDownStuff ();
}
}


Note: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx for use of virtual key codes here

Share this post


Link to post
Share on other sites
I do not know whether that is your problem, but the line

bool qDown, eDown = false;

only initializes the variable eDown to false, qDown will not be initialized. So you should write

bool qDown = false, eDown = false;

or split it up in two lines (which I find a lot better readable, but that's personal opinion).

Edit: nevermind, just realized that those are globals, so they will be initialized to false by default (as mhagain also pointed out).

Share this post


Link to post
Share on other sites

Sorry, the problem was i forgot one line, right after case WM_KEYUP:there should have been switch(wParam), so it works now. Thanks for the help. And this second approach is much better when dealing with a large variety of keys, so thanks for that aswell!

Share this post


Link to post
Share on other sites
Hidden
I've used systems like the above before and can't personally see the advantage over just testing GetAsyncKeyState('Q') & 0x8000 in the loop any more. It is effectively equivalent to if(keydown[0x51]) in mhaigin's example above as far as I can see.

I may of course be missing some subtlety.

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!