Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by BennettSteele Jan 6, 2012 at 3:11 AM 37 replies 17.6k views
Original Post
BennettSteele
BennettSteele
So im finally done coding the WinMain() stuff, and now i just need to update my key presses. I used to use GLUT with disabled key repeats, but now im using the windows message handler way.. like VK_ ... and i need to know how i can disable key repeats. Is there any way i can stop key-repeating?
bradbobak
bradbobak
Take a look at what WM_KEYDOWN sends (in lParam). Microsofts site says there is a bit telling if this is a key repeated or not.
LevyDee
LevyDee
The simple solution is to set a flag. So if 'a' is pressed DOWN, you would set a "down" flag. You could skip and 'a' DOWN events if that flag is on. The flag would not change until you received an 'a' UP message, then you would toggle the flag.
BennettSteele
BennettSteele
Hm... i think i know what youre saying... so... like this:


bool Key_Flags[1];
bool A_Key=false;
case 'a'://down
Key_Flags[0]=true;
break;
case 'a'://up
Key_Flags[0]=false;
break;
if(Key_Flags[0])
{
A_Key=true;
}


or... DX im getting... sort of... the idea. some sample code might set my logic straight.
BennettSteele
BennettSteele
I read it... and it makes some sense. I cant really think of a way to non-repeatable keys though... like how i have this:


bool A_DOWN=false;
bool move_left=false;
if(KeyDown=='A')
{
A_DOWN=true;
}
else if(KeyUp=='A')
{
A_DOWN=false;
}

move_left=true;


and i wouldnt need to worry about move_left becoming false again since it only would check once if it was up or down. but windows ( DX ) has key repeats... all i need is possibly some sample code to show me how i can turn repeating key-presses work with what i wrote above.
BennettSteele
BennettSteele
I got it!


class Key
{
public:
bool Down,Check_For;
unsigned int Key_ID;
};


it doesnt matter what Down is, whenever it is push up or down, it only changes Down to Check_For! i think that works...
ApochPiQ
ApochPiQ
The article and accompanying source code should clearly demonstrate exactly how to handle key-repeat in Win32... unless I'm wildly forgetting what I put in the demo app, which is thoroughly possible :-)
BennettSteele
BennettSteele
Ok, so i have everything done and working perfectly.

Only problem is: i dont know how to use this with the WinMain aka VK_ values.

Here is what im using: (KeyStats.h)

#include <string>
class Key_Press
{
public:
bool Down,Check_For;
Key_Press()
{
Down=false;Check_For=true;
}
};
class KeyStats
{
public:
Key_Press Win_Keys[256];
Key_Press Let_Keys[126];
bool IsKeyDown(unsigned int Key);
void Press(unsigned int Key);
void Release(unsigned int Key);
std::string KeyName(unsigned int Key);
unsigned int FixKeyUP(unsigned int Key);
unsigned int FixKeyDWN(unsigned int Key);
};
#define KEY_EXCLMTN 256
#define KEY_DQUOTE 257
#define KEY_NUMB 258
#define KEY_DOLLAR 259


At the end, im defining my own key values, like VK_, but for Characters.

Here is the source file: (KeyStats.cpp)

#include "KeyStats.h"
#include "App.h"
extern myApp APP;
bool KeyStats::IsKeyDown(unsigned int Key)
{
if(Key<256)
{
return KeyStats::Win_Keys[Key].Down;
}
else if(Key<349)
{
unsigned int nKey=Key-256;
return KeyStats::Let_Keys[nKey].Down;
}
else
{
return false;
}
}
void KeyStats::Press(unsigned int Key)
{
if(Key<256)
{
if(KeyStats::Win_Keys[Key].Check_For==true)
{
KeyStats::Win_Keys[Key].Down=true;
KeyStats::Win_Keys[Key].Check_For=false;
}
}
else if(Key<349)
{
unsigned int nKey=Key-256;
if(KeyStats::Let_Keys[nKey].Check_For==true)
{
KeyStats::Let_Keys[nKey].Down=true;
KeyStats::Let_Keys[nKey].Check_For=false;
}
}
APP.KeyLog.Write((KeyStats::KeyName(Key)+" Is Down\n").c_str());
}
void KeyStats::Release(unsigned int Key)
{
if(Key<256)
{
if(KeyStats::Win_Keys[Key].Check_For==false)
{
KeyStats::Win_Keys[Key].Down=false;
KeyStats::Win_Keys[Key].Check_For=true;
}
}
else if(Key<349)
{
unsigned int nKey=Key-256;
if(KeyStats::Let_Keys[nKey].Check_For==false)
{
KeyStats::Let_Keys[nKey].Down=false;
KeyStats::Let_Keys[nKey].Check_For=true;
}
}
APP.KeyLog.Write((KeyStats::KeyName(Key)+" Is Up\n").c_str());
}
std::string KeyStats::KeyName(unsigned int Key)
{
std::string Name;
switch(KeyStats::FixKeyUP(Key))
{
case KEY_EXCLMTN :Name="Exclamation";break;
case KEY_DQUOTE :Name="Double Quote";break;
case KEY_NUMB :Name="Number Sign";break;
case KEY_DOLLAR :Name="Dollar Sign";break;
case KEY_PERCENT :Name="Percent sign";break;
default:
{
std::stringstream nRep;nRep<<"Unknown("<<Key<<")";
Name=nRep.str();
}break;
}
return Name;
}
unsigned int KeyStats::FixKeyDWN(unsigned int Key)
{
unsigned int nKey=Key;
if(Key>=256)//one of "our" keys we defined
{
if(Key>=KEY_EXCLMTN && Key<=KEY_OPENPERENT)
{
nKey+=16;
}
else if(Key==KEY_CLOSEPERENT)
{
nKey=KEY_0;
}
else if(Key>=KEY_a && Key<=KEY_z)
{
nKey-=6;
}
nKey-=256+33;
}
return nKey;
}
unsigned int KeyStats::FixKeyUP(unsigned int Key)
{
unsigned int nKey=Key;
if(Key<256)//one of "our" keys we defined
{
if(Key>=(int)'!' && Key<=(int)'(')
{
nKey+=16;
}
else if(Key==(int)')')
{
nKey=(int)'0';
}
else if(Key>=(int)'a' && Key<=(int)'z')
{
nKey-=6;
}
nKey+=256-33;
}
return nKey;
}



This is where i use it:(WindProc.cpp)

//Blah blah blah
case WM_KEYDOWN:
{
APP.Keys.Press(wParam);
}break;
case WM_KEYUP:
{
APP.Keys.Release(wParam);
}break;




How would i be able to use the VK_ keys with my class?
Cornstalks
Cornstalks
I'm not sure why you're redefining key codes like that, as it's confusing as heck. I'd do it pretty differently if I were you. It can be a lot simpler. Don't forget to actually, thoroughly read (not just skim) the docs I link to.

Virtual Key Codes:
http://msdn.microsof...1(v=vs.85).aspx

WM_KEYDOWN:
http://msdn.microsof...0(v=vs.85).aspx

Code to give you an idea: (in WndProc)

switch (Msg)
{
case WM_KEYDOWN:
switch (wParam)
{
// '0'-'9' and 'A'-'Z' Virtual Key Codes are the same as ASCII/UTF8 codes
case 'W':
std::cout << "'W' key pressed!\n";
break;
case 'F':
std::cout << "'F' key pressed!\n";
break;
case VK_LEFT:
std::cout << "Left arrow key pressed!\n";
break;
default:
std::cout << "VK value of key press: " << (int)wParam << '\n';
}

// Extract bit 30 of lParam to check if it's a repeat-keypress (read the docs for more info)
if (lParam & 0x40000000)
{
std::cout << "The keypress is a repeat, you should ignore it if you want\n";
}
else
{
std::cout << "The keypress is not a repeat, you probably shouldn't ignore it\n";
}
return 0;
// ...
BennettSteele
BennettSteele
DX Just that easy? So how instead of using the


if (lParam & 0x40000000)




I could just check first for the first key-press? also, i think i will use your Wind-Proc method, but keep the key-class i made... that way i can organize the keys and allow re-defining keys.
BennettSteele
BennettSteele

Oh! I think i get it... so what does lParam & 0x000000000 mean?

RulerOfNothing
RulerOfNothing
if (lParam & 0x40000000) tests whether or not the leftmost bit of lParam is set (& is the bitwise AND operator, and should not be confused with the logical AND operator &&)
Cornstalks
Cornstalks
0x... is a Hexadecimal number. 0x40000000 (hex) = 01000000000000000000000000000000 (binary) = 1073741824 (decimal). In C++, there are ways to natively write numbers in decimal, hexadecimal, and octal (but not binary) formats, and I chose hexadecimal for this case because it's easy to read. Like RulerOfNothing said, the & operator is the bitwise AND operator. Googling "C++ operators" should give you plenty to read, as should "bitwise AND".
BennettSteele
BennettSteele
Oh! so bits 0-15 (16 bits) is the same as 0x40000000 because the 4 is a power of 2? so the 4 counts for 16 bits?
RulerOfNothing
RulerOfNothing
One thing you should remember about hexadecimal is that each hexadecimal digit corresponds to 4 bits, so if you have a 8-digit hexadecimal number, it is actually 32 bits long.
Cornstalks
Cornstalks

Oh! so bits 0-15 (16 bits) is the same as 0x40000000 because the 4 is a power of 2? so the 4 counts for 16 bits?


Nope. I'm really not sure what it is you were trying to say there either. You should google hexadecimal and how to convert between hex and binary (and decimal).
BennettSteele
BennettSteele
Oh well... so would using


if (lParam & 0x00000000)


Checks for no key repeats?
RulerOfNothing
RulerOfNothing
lParam & 0x0000000 will always evaluate to zero because of how AND operators work.
BennettSteele
BennettSteele
So what is the value for checking if it was the first key press?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.