I've been exploring some keyboard input stuff lately, and found some functions in the FAQ to be of some use. But, I've ran into some problems that I don't want to run into in future implementations of these methods. My main focus is game programming, and this error is a big issue. The code which i'll paste below, grabs keypresses, and prints them out in a txt file(I use a file because I can c&p it btw), 10x more than I wanted the program to do. e.g it catches 50 keypresses, and not just one. Since i'm just starting out on this method of input for game programming, currently with my code I would make my character zoom across the screen like mad..very big issue as far as i'm concerned.

So i'm asking for a way to capture key presses without it going mad like it does in this program. I'de like it to capture them at a good rate too. For instance, if a user presses a key twice, I want it to capture both of those keypresses, not just the first. I also don't want it to capture 50 keypresses on the first press, and 50 on the next...I haven't been able to find a solution for this yet, since it seems every keypress is about 50 of them depending on how fast your comp is, at least that's what i've come up with to explain it..and i'm not good enough of a coder to know about limits and such.

I had tried coding it like this:
a) capture first key press in a variable.
b) capture second keypress in a temp variable.
c) if first and second keypresses are equal only write the first keypress.
d)else if the first and second keypresses are different, write both keypresses.

But then I go back to my previous problem, what if the user presses the key twice? If I use that method, I would only capture the first keypress, and ignore the second...which obviously isn't good. It also makes me wonder how they would get past this problem in the first place...maybe there are some hidden functions in the winapi i'm not familiar with? That's what i'm getting out of it so far..

But in any case, here's my code:

PHP Code:

//The catchKeyPress() function was taken from BMJ's code, and
//the KEY_DOWN and KEY_UP macros were taken from DIVX's
//code, I hope you don't mind guys :)
#include <windows.h>
#include <iostream>
#include <fstream>

Since the code tags messed up the formatting/display, I had to condense it, the real output is much longer and doesn't have line breaks or spaces in it. But you can get the idea of what it's really doing i'm sure.

I didn't expect the results of this program...but thenagain, i'm kind of new to the winapi. So this is kind of a learning experience for me ^_^

11-25-2004

anonytmouse

Code:

if(catchKeyPress(input) == false)

It looks like catchKeyPress() returns false on failure. So you want to test for success instead:

Code:

if(catchKeyPress(input))

11-25-2004

Hunter2

Quote:

what if the user presses the key twice? If I use that method, I would only capture the first keypress, and ignore the second...which obviously isn't good. It also makes me wonder how they would get past this problem in the first place...maybe there are some hidden functions in the winapi i'm not familiar with?

While I can't say whether it's the "best" way or not, the method that I usually use is:

-Keep a boolean variable for each game key
-On key-down, if the variable for the key is false, then set it to true and do the action; otherwise (if it's true), do nothing
-On key-up, set the variable for the key to false.

That way you won't get repeated keystrokes unless the key is released first. Also, it's nice because if you're capturing the key-up and key-down events using the WM_KEYDOWN and WM_KEYUP messages in your window procedure, then in other parts of the program you can simply check the state of the boolean variables to know whether the key is currently pressed or released. Also, you can add separate counter variables for each key, and each time you have a unique keystroke you increase the counter for that key, and rather than relying on checking the boolean variables you can just check if the counter for the key is greater than 0; if it is, then decrease it by one and perform the action.

Etc. etc. etc... There are endless possibilities, when you put your imagination to work :)

**EDIT**
Ah, I see you're doing a console program. In that case you can ignore the last paragraph, but I believe the method of simply keeping a bunch of 'key-state' variables (i.e. an array of them, 1 element for each ASCII character code) still applies.