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>
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
bool catchKeyPress(char &Key);
int main(int argc, char *argv[])
{
std::cout << "Keyboard Input Prog";
std::ofstream loggingFile;
loggingFile.open("log.txt", std::ios::app);
char input;
while(!KEY_DOWN(0x51))//0x51 = q
{
if(catchKeyPress(input) == false)
loggingFile << input;
}
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
loggingFile.close();
return 0;
}
bool catchKeyPress(char &Key)
{
INPUT_RECORD Event;
DWORD NumberOfEvents, EventsRead, EventCounter;
GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE), &NumberOfEvents);
if (NumberOfEvents == 0)
return false;
for (EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++)
{
PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event, 1, &EventsRead);
if ((Event.EventType == KEY_EVENT) && ((Event.Event.KeyEvent.bKeyDown)))
{
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event, 1, &EventsRead);
Key = Event.Event.KeyEvent.wVirtualKeyCode;
if (!(FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE))))
exit(0);
return true;
}
else
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event, 1, &EventsRead);
}
return false;
}
Sample output:
Code:
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
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 ^_^