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

Started by
37 comments, last by BennettSteele 12 years, 3 months ago
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?
Advertisement
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.
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.
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.
This article should be useful.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
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...
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 :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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?
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;
// ...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement