Win32 disable or get around key repeating (only check if up or down once)

Started by
37 comments, last by BennettSteele 12 years, 2 months ago
VK_... are just defines for numbers, they are basically ints.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Advertisement
yeah, but when i tried using a function to take a VK_ and then change a key state based on that, it changes it to a char key press.... it was to a unsigned int though.... i dont think that would matter
i usually handle it this way (kinda like marsong's post)


//for each input loop set the following
previousKeyState = currentKeyState;
currentKeyState = getKeyState();


in this case WM_KEYDOWN may be what you would use instead of getKeyState();

but for a single press you would do something like this:


//if using a keyboard state
bool isKeyReleased(Key key)
{
if(previousKeyState.KeyDown(key) && !currentKeyState.KeyDown(key))
return true;
return false;
}
//if using chars
bool isKeyReleased(char key)
{
if((previousKeyState== key) && (currentKeyState!= key))
return true;
return false;
}


So what is the value for checking if it was the first key press?
0x40000000.

From the documentation for WM_KEYDOWN:

lParam

The repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown following.
Bits Meaning
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.
16-23 The scan code. The value depends on the OEM.
24 Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28 Reserved; do not use.
29 The context code. The value is always 0 for a WM_KEYDOWN message.
30 The previous key state. The value is 1 if the key is down before the message is sent, or it is zero if the key is up.
31 The transition state. The value is always 0 for a WM_KEYDOWN message.
[/quote]
Bit 30 has a hex value of 0x40000000, and the bitwise AND operator in this case is being used to see if the bit is set.


EDIT: Bah, didn't realise there was a page 2 to this topic. Damn quick reply on page 1...
I had the very same problem today, this is how I handled it:

case WM_KEYDOWN: {
switch (wParam) {
case VK_UP:
if (!(lParam & (1 << 30)))
KeyPressed(UP_ARROW);
break;


Didn't know what exact number it was so I just shifted one to 30 places.
so...


if(!(lParam & (1 << 30)))
{
switch(wParam)
{
case VK_ESCAPE:
{
APP.Quit("Escape Was Pressed");
APP.KeyLog.Write("Quit App Requested\n");
}break;
}


should only work if it was the first time it was pressed and not a key-repeat?
Yes, that way you'll handle all keys only when they're pressed. Try it.
Ok, i think it works... just one problem though, how would i do this with a mouse button press? (like typing w/o repeats, but clicking)
Nevermind! :D

I fixed it... :P

Thanks for all the help guys! :3

This topic is closed to new replies.

Advertisement