• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

38 replies to this topic

### #1bennettbugs  Members

Posted 05 January 2012 - 09:11 PM

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?

Posted 05 January 2012 - 09:31 PM

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.

### #3Slig Commando  Members

Posted 05 January 2012 - 09:50 PM

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.

### #4bennettbugs  Members

Posted 05 January 2012 - 10:44 PM

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.

### #5ApochPiQ  Moderators

Posted 06 January 2012 - 12:35 AM

Wielder of the Sacred Wands

### #6bennettbugs  Members

Posted 07 January 2012 - 05:26 PM

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.

### #7bennettbugs  Members

Posted 07 January 2012 - 06:35 PM

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

### #8ApochPiQ  Moderators

Posted 07 January 2012 - 10:30 PM

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

### #9bennettbugs  Members

Posted 09 January 2012 - 07:48 PM

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?

### #10Cornstalks  Members

Posted 09 January 2012 - 08:14 PM

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;
// ...


[ 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 ]

### #11bennettbugs  Members

Posted 09 January 2012 - 08:28 PM

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.

### #12bennettbugs  Members

Posted 09 January 2012 - 08:53 PM

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

### #13RulerOfNothing  Members

Posted 09 January 2012 - 09:22 PM

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 &&)

### #14Cornstalks  Members

Posted 09 January 2012 - 11:32 PM

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".
[ 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 ]

### #15bennettbugs  Members

Posted 10 January 2012 - 05:12 PM

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?

### #16RulerOfNothing  Members

Posted 10 January 2012 - 05:45 PM

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.

### #17Cornstalks  Members

Posted 10 January 2012 - 05:48 PM

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).
[ 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 ]

### #18bennettbugs  Members

Posted 10 January 2012 - 09:07 PM

Oh well... so would using

if (lParam & 0x00000000)


Checks for no key repeats?

### #19RulerOfNothing  Members

Posted 10 January 2012 - 09:25 PM

lParam & 0x0000000 will always evaluate to zero because of how AND operators work.

### #20bennettbugs  Members

Posted 11 January 2012 - 05:08 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.