-
GetAsyncKeyState help
hi guys
well ive been learning C++ the last few days and have started to create an address book app :p
but ....
if you have a look at the code : http://x-kane.com/programming/addressbookmain.cpp
this one sort of works (so far)
the problem i have is in the switch statement i have had to change the VK_RETURN to VK_SPACE.
the reason ... well after the switch called the "addEntries" function, it worked but then after i pressed escape in the addEntries it seemed to recall itself :confused: .. like it would go bak to the main menu (could see it flash on) but then went straight back to addEntries.
hard to explain but if you change the switch like i suggested above and enter data then try and press escape ..you will see what i mean.
its like its keeping the VK_RETURN in memory ... do i have to "clear" it or something?
and one more question ... the while loops i have in the program use 100% cpu ... what other options is there if these loops do this?
thanks alot
Kane
-
GetAsyncKeyState will let you know what is being press when you call the function. Your code goes by so fast, by the time the user lets the key go, it already told the program that it's still being pressed. A common solution is to have a flag that keeps track of the status of the return key (up or down) and use that in your checks as well.
Code:
bool returnDown = true;
if(GetAsyncKeyState(VK_RETURN) & 0x80 && returnDown)
{
//do whatever you want
returnDown = false;
}
else
{
if(!GetAsyncKeyState(VK_RETURN) & 0x80)
returnDown = true;
}
This is a quick and dirty way of handling buffered input. The CPU problem shouldn't be much of an issue, unless it freezes the system. How else will you keep the program running (assuming you want to keep this a console app)?
-
thanks alot that worked. :D
and yes i was talking about console apps
-
right i have that fixed sort of now ....
but i have another problem (instead of creating a new thread)
http://x-kane.com/programming/addressbookmain.cpp
this is the updated code.
if you run it and go straight into add entry then it works fine.
but if yu first go into view entries then into add ...it looks like enter has already been pressed on the first value. so i tried a cin.ignore();
but that didnt work.
and if you keep going into view then into add it will complete the entries the same amount of times you entered the view entries :confused:
but i cant see how as no variables like that are passed into the addentries function
Kane
-
got it working fine at last
2 days .... (im such a newbie) :p
i used getch() for the return and escape keys. and then just getasynckeystate for the up/down keys.
thanks
Kane