90 posts
WM_KEYDOWN instant respons?
Edited by C_Worm on Reason: Initial post
I've been thinking about this problem for some time now and read on msdn and in some books but i wonder how to get the WM_KEYDOWN to respond instantly while holding for example the "UP"-key?

right now its a little paus before the repeats start!

i know that maybe you could use GetAsyncKey(); in the main-loop in the WinMain function(?) but i would like to get some help on how to implement this feature in the WndProc message-handler!

here's my code this far:

rcFill is just a filled rect that moves when keybuttons are pressed :)

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  switch(message) { case WM_KEYDOWN: { if(WParam == VK_UP) { LParam = ( 1 << 30); rcFill.top -= vel; rcFill.bottom -= vel; InvalidateRect(hwnd, 0, true); } if(WParam == VK_DOWN) { rcFill.top += vel; rcFill.bottom += vel; InvalidateRect(hwnd, 0, true); } if(WParam == VK_RIGHT) { rcFill.right += vel; rcFill.left +=vel; InvalidateRect(hwnd, 0, true); } if(WParam == VK_LEFT) { rcFill.right -= vel; rcFill.left -=vel; InvalidateRect(hwnd, 0, true); } } break; 

Simon Anciaux
1051 posts
WM_KEYDOWN instant respons?
Edited by Simon Anciaux on Reason: Note
You need to keep the state of the keys in memory. When you receive a key down message, you mark the key as down. As long as you don't receive a key up message you don't change the state so the key will stay down. One problem with that is when your application loses or gains the focus because it will not receive the key up/down message. To work around that you will need to update the state when your application loses and gains the focus to make sure you don't have a "stuck key".

See the "Other" chapter in Keyboard input... raw input, text input, key names in the wiki for an example. It uses scancodes but it's the same with virtual key codes.

EDIT: In your current code your update is done based on the repeat of the key down messages. Those repeat come at interval, they are not continuous (each frame).
90 posts
WM_KEYDOWN instant respons?
Do you mean like having a global bool and set it to true in WM_KEYDOWN and false in KEY_UP like this?:

  1 2 3 4 5 6 7 8 9 10 11 12 13 Static bool isDown = false; case WM_KEYDOWN: { if (wParam == VK_RIGHT) isDown = true; } case WM_KEYUP: { if (wParam == VK_RIGHT) isDown = false; } 
Simon Anciaux
1051 posts
WM_KEYDOWN instant respons?
More or less. You can use an array to keep the state of every key you need.
  1 2 3 4 5 6 7 8 9 10 11 12 13 bool key_state[ 256 ] = { 0 }; ... case WM_KEYDOWN: { key_state[ wParam ] = true; } case WM_KEYUP: { key_state[ wParam ] = false; }