if(GetAsyncKeyState(VK_UP))
{
printf("The Up Arrow Has Been Pressed\n");
}
At the time of exectuing this if statement, if the up arrow is being pressed in, it will say so - put this in a loop if you want to constantly read the key state (pressed (1), not pressed (0)).
If you want the keys to pass to the function, I believe they can be found in "winuser.h" on Windows, (on my include file they start at line 1738 with VK_LBUTTON 1).
Hope that helps a little more.
Yes - because the while(true) loops constantly without any break in it or pause, it uses as much CPU as it can & every millisecond or so that the key is held down, counter increases - I get this problem a lot as well.
There is a useful function in windows.h called Sleep(x) where it pauses program execution for (x) milliseconds.
Maybe try this:
Now it works great , and if I press 5 times on escape - the output is :
Button pressed !
Button pressed !
Button pressed !
Button pressed !
Button pressed !
But the problem is , the file is always empty.
I need to hold the escape button a lot of time , and then the file is filled a bit , but always he contains
Escape : 1Escape : 2Escape : 3Escape : 4Escape : 5Escape : 6Escape : ...42Escape
Well, what I've found from some testing is:..
I imagine the way you close the program is just exitting the process before it's had time to finish the file properly.
What I've done here is added it so that by pressing F1 it closes the file handle properly:
I've also changed counter to an unsigned int, as I hope you cannot press Enter negative times... ;)
If you do not want to have to press F1 to stop this, I've done the same piece of code, except with the C standard input/output library, that re-opens the file for ammending so that it is updated properly.
This example only responds to key release events. Chances are you will want to respond to key press events for your game (or whatever you are making). Un-negate line 40 to do that. ;-)
The solution you have given is very useful. Iam trying it in Windows mobile in C++ (win32 console application) to check the key state of all virtual key codes, where I got problem is Iam not able to detect state of VK_HOME,VK_F3(TALK), and also when I try to write the virtual key code in to file Iam getting repeatedly . Though I used Sleep()with different values from 100... 1000.Iam not able to fix the problem. Code snippet is,
int _tmain(int argc, _TCHAR* argv[])
{
INT vkIndex,index=0;
FILE* write,*read;
write=fopen("UserKeyEvents.txt","w+b");
INT events[MAX_EVENTS_BUFF];
memset(&events,0,sizeof(events));
while(true)
{
for(vkIndex=VK_LBUTTON;vkIndex<=VK_DBE_ENTERDLGCONVERSIONMODE;vkIndex++)
{
if(GetAsyncKeyState(vkIndex))
{
events[index]=vkIndex;
index++;
break;
}
}
if(vkIndex==115)//virtual key code of END key
{
fwrite(events,sizeof(events),1,write);
fclose(write);
read=fopen("UserKeyEvents.txt","r+b");
fread(events1,sizeof(events1),1,read);//to find what are the keycodes there in the file
fclose(read);
return 0;
}
Sleep(100);
}
return 0;
}
Can anybody kindly tell me what am I doing wrong and correct it.
Thanks in advance for any help.
If you want to use GetAsyncKeyState but are only concerned with handling the actual transitions, then you have to keep track of the transitions yourself.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int counter=0;
ofstream myfile;
short prev_escape = 0, curr_escape = 0;
myfile.open("c:\\example.txt");
while(true)
{
//This if statement is required to ignore key repeat, which causes the return value to increment
if(GetAsyncKeyState(VK_ESCAPE))
curr_escape = 1;
else
curr_escape = 0;
//Here we only process on the actual transition
//IE when the current value has changed from the previous value
if(prev_escape != curr_escape)
{
//Could move counter to the if statement below to only increment on either the press or release
counter++;
//Is this a key press, or a key release?
if(curr_escape){
myfile <<"Escape pressed : " << counter << endl;
cout<<"Escape pressed !" << endl;
}
else{
myfile <<"Escape released : " << counter << endl;
cout<<"Escape released !" << endl;
}
prev_escape = curr_escape;
}
}
//Close the file on exit, and whatever is buffered gets written to file before the application exits
myfile.close();
return 0;
}
That code only processes on the key transitions, and ignores everything else. No need for any sleeps at all, unless you want to reduce CPU usage.