How do I detect keystrokes that aren't characters, such as cursor keys or shift keys?
How do I detect keystrokes that aren't characters, such as cursor keys or shift keys?
---Rainer
Digital pimp, hard at work
>How do I detect keystrokes that aren't characters, such as cursor keys or shift keys?
It depends on your operating system. Windows gives you a nice way to detect keystrokes with virtual keys. Or you could just grab the scancodes and detect keystrokes that way:
Naturally, this solution requires you to be able to get raw input such as with getch.Code:#include <iostream> #include <conio.h> using namespace std; int keycode ( bool& extended ) { int c; c = getch(); if ( c == 0 || c == 224 ) { extended = true; c = 256 + getch(); } return c; } int main() { int c; bool extended; while ( ( c = keycode ( extended ) ) != 0x1B ) { if ( extended ) cout<<"Extended: "; else cout<<"Not Extended: "; cout<< c <<endl; } }
My best code is written with the delete key.
thanks for the help...i guess
---Rainer
Digital pimp, hard at work
>...i guess
If it wasn't any help you can clarify your question, such as what compiler/platform you use and why my answer was not helpful. That way I have a better idea of exactly what you want and can direct you toward a solution more accurately.
My best code is written with the delete key.
all i need to know (for now) is how to use this code to detect up arrow, down arrow, and enter.
EDIT:
my compiler: djgpp with rhide IDE
my os: windows 98 (the program is a dos program, though)
Last edited by Rainer; 01-19-2004 at 05:50 PM.
---Rainer
Digital pimp, hard at work
>all i need to know (for now) is how to use this code to detect up arrow, down arrow, and enter.
Use the program I gave you. When it runs, hit the up arrow, then the down arrow, then enter. Write down the values that are printed, those are the scancodes that you can use to detect the keystrokes. Then hit esc to end the program.
My best code is written with the delete key.
I see... you used int c, instead of char c. I didn't notice that before.
---Rainer
Digital pimp, hard at work
I find the following functions very useful for things like that.
NB: 1. Check an ASCII table to see a listing of these characters.
2. You need to use the ctype.h library
iscntrl() - returns true if it is a cntrl character
The ASCII control characters are all the values below space (decimal 32) and includes the delete character.
isspace() - returns true if it is a whitespace
Checks for whitespace (horizontal tab, line feed, vertical tab, form feed and carriage return)
isgraph() - returns true if it is a graphic character
Checks for a graphic character. These are all of the ASCII values greater than the space (decimal 32) and less than the delete (decimal 127)
Hope these helps
i believe anything on the keyboard has an ascii value
see the post from prelude.
you can use that value and a getch() to write a function that reads and tests the interpreted value entered.
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu