disrupter
asked on
listening to keyboard (not cin) c++
Hi, Im trying to create a gearbox simulator, its almost done.
How can I listen to the key that is being pressed in order to use this switch: (c++ console program)
void Keyboard(unsigned char key)
{
switch (key)
{
case 'u' :
//upgrade
break;
case 'd' :
//down grade
break;
case 'a' :
//accelerate
break;
case 'b' :
//break
break;
}
}
and also this one
void KeybUp(unsigned char key) //when the key is up
{
switch (key)
{
case 'a' :
is_acc = false;
break;
case 'b' :
is_break =false;
break;
}
}
sry for my english
How can I listen to the key that is being pressed in order to use this switch: (c++ console program)
void Keyboard(unsigned char key)
{
switch (key)
{
case 'u' :
//upgrade
break;
case 'd' :
//down grade
break;
case 'a' :
//accelerate
break;
case 'b' :
//break
break;
}
}
and also this one
void KeybUp(unsigned char key) //when the key is up
{
switch (key)
{
case 'a' :
is_acc = false;
break;
case 'b' :
is_break =false;
break;
}
}
sry for my english
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hmm, I found this question in the C++Builder area...
Please ask a page editor to move your question to Visual C++
Please ask a page editor to move your question to Visual C++
check out the API getKeyState()
that may be the wrong function. check GetAsyncKeyState.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/GetKeyState.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/GetKeyState.asp
Here's a short example of what you may want to do.
#include <stdio.h>
#include <conio.h>
void main(){
while(1){
if(_kbhit()){
printf("%c", _getch() );
}
//do the processing you want
}
}
_kbhit() just tests if a key has been pressed. You have to use _getch() after it though, to clear the true state of a key being hit. If you don't read the key with _getch() regardless of whether or not you use it, _kbhit() will remain true.
Tim
#include <stdio.h>
#include <conio.h>
void main(){
while(1){
if(_kbhit()){
printf("%c", _getch() );
}
//do the processing you want
}
}
_kbhit() just tests if a key has been pressed. You have to use _getch() after it though, to clear the true state of a key being hit. If you don't read the key with _getch() regardless of whether or not you use it, _kbhit() will remain true.
Tim
ASKER