-
Simple Question
I need to know if there is a function, or method of finding out if a key is pressed and what the pressed key is. I have tryed some stuff with getchar() but it's not working the way I want it to work... Does anyone know of a method or a function that will let me detect if a key is pressed and what key was pressed?
-
I'm not exactly sure if this will help you...but maybe you can store the pressed character into a char variable and then type cast it as an int to give you the ASCII value.
Code:
char c;
cout << "enter character: ";
cin >> c;
cout << (int)c;
....or other manipulation
axon
-
conio.h and getch()/kbhit( )
getch( ) gives you the key pressed as soon as it is hit, not waiting for the enter key
kbhit( ) tells you if a key has been pressed
-
Code:
int getkey(void)
{
// Get ASCII code of charactor hit
int key = getch();
// ASCII code of hit is 224 - get second getch() to retrieve code of other keys e.g. arrow keys
if (key == 224) {
key = 256; // Add 256 to this code to be able to differ from 'H' and Up arrow, etc.
key += getch();
}
return key;
}
Use keys.h for the ASCII codes for each key, which is what this function returns.
You'll need to add 256 to the keys like Up Arrow which are defined as 72 (same as H).