• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BennettSteele

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

38 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Hm... i think i know what youre saying... so... like this:

[CODE]
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;
}
[/CODE]

or... DX im getting... sort of... the idea. some sample code might set my logic straight.
-1

Share this post


Link to post
Share on other sites
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:

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

move_left=true;
[/CODE]

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

Share this post


Link to post
Share on other sites
I got it!

[CODE]
class Key
{
public:
bool Down,Check_For;
unsigned int Key_ID;
};
[/CODE]

it doesnt matter what Down is, whenever it is push up or down, it only changes Down to Check_For! i think that works...
0

Share this post


Link to post
Share on other sites
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 :-)
0

Share this post


Link to post
Share on other sites
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)
[CODE]
#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
[/CODE]

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

Here is the source file: (KeyStats.cpp)
[CODE]
#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;
}
[/CODE]


This is where i use it:(WindProc.cpp)
[CODE]
//Blah blah blah
case WM_KEYDOWN:
{
APP.Keys.Press(wParam);
}break;
case WM_KEYUP:
{
APP.Keys.Release(wParam);
}break;

[/CODE]


How would i be able to use the VK_ keys with my class?
0

Share this post


Link to post
Share on other sites
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:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx"]http://msdn.microsof...1(v=vs.85).aspx[/url]

WM_KEYDOWN:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280(v=vs.85).aspx"]http://msdn.microsof...0(v=vs.85).aspx[/url]

Code to give you an idea: (in WndProc)
[code]
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;
// ...
[/code]
1

Share this post


Link to post
Share on other sites
DX Just that easy? So how instead of using the

[left][CODE]
if (lParam & 0x40000000)
[/CODE][/left]


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

Share this post


Link to post
Share on other sites
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 &&)
0

Share this post


Link to post
Share on other sites
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".
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='bennettbugs' timestamp='1326237135' post='4901439']
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?
[/quote]

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

Share this post


Link to post
Share on other sites
Well, you want to test whether bit number 30 is zero in that case, so you would probably want if ( (lParam & 0x40000000)==0) as your condition. Also, I just remembered that 0x40000000 has a bit set at bit number 30, not bit number 31. Sorry about that mistake.
0

Share this post


Link to post
Share on other sites
[quote name='bennettbugs' timestamp='1326323291' post='4901810']
So what is the value for checking if it was the first key press?
[/quote]


Did you even read the code I posted? If the key was pressed, and it's not a repeat, obviously it's the first key press. Read the documentation, google how to use the bitwise AND operator, and try it out yourself. A lot of these questions you're asking could easily be solved by you if you did some reading and experimenting.
0

Share this post


Link to post
Share on other sites
I read it, but instead of
[CODE]
(lParam & 0x40000000)// this is a key repeat
{

}
else
{
//key press, use this
}
[/CODE]

i would rather not have the "else" part...

I also researched some, a little bit of it made some sense... but using a hex for bits confuses me.
0

Share this post


Link to post
Share on other sites
[quote name='bennettbugs' timestamp='1326335326' post='4901869']
I read it, but instead of
[CODE]
(lParam & 0x40000000)// this is a key repeat
{

}
else
{
//key press, use this
}
[/CODE]

i would rather not have the "else" part...
[/quote]

The "else" part only executes if the if statement evaluates to false. You could easily just do

[CODE]
(!(lParam & 0x40000000))// this is not a key repeat
{

}
[/CODE]

by using the logical NOT operator.

[quote name='bennettbugs' timestamp='1326335326' post='4901869']
I also researched some, a little bit of it made some sense... but using a hex for bits confuses me.
[/quote]

It makes a lot more sense then using decimal, and it's actually a lot easier than octal and binary once you're used to it. Keep reading up on hex, and write a hex to binary (and binary to hex) converter program to practice.
0

Share this post


Link to post
Share on other sites
DX its not exactly working for me...

[CODE]
bool KeyRep=((lParam & 0x40000000));

if(KeyRep)//pressed
{
Left_Down=true;
}
if(KeyRep)//released
{
Left_Down=false;
}
[/CODE]

whats wrong with this?

And i will look more into binary/hex/etc numbers and data. ;)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0