Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hello, I am writing a program that waits for a user to press a key on the keyboard, and then verify the scancode (...or Ascii code) generated by the keystroke.
So an example run may be:
Please press ESC key: (press key)
Esc key was pressed and is OK.
End program.I have tried capture keyboard data in Ada95, and believe I will run into the same problem in C++ in that I will not be able to get the control characters (Tab, Shift, Control...ect). How do I interface into the OS's (windows or Linux...Linux prefered..:) ) "virtual keyboard" buffer to obtain the keys pressed during the run of my program? Any help...thnx

#include
#includevoid main(void)
{
while (1) {
if (kbhit()) {
key = getch();
if (key == 0) // get extended keycode
key = getch();
printf("%d %x\n", key, key);
if (key == 27) return;
}
}
}Works under DOS (& Windows) and Netware, linux might be different. Prints the decimal and hex values for keystrokes. Also check http://linux.nf/kbhit.html

Those two include statements probably should have been:
#include [iostream.h]
#include [conio.h]replace the [] with greater-than and less-than signs.

Hi,
Its long i used this command, i remember it remotely. If u r using Borland C++(Turbo C++), then there is one function KEYBRDREAD(), it returns a unique number ( i guess it is long) number for every key pressed, even for ESC, space, F1, F2 etc. Do some R&D for it.Check the help, IT IS THERE IN IT. Just patiently check for the function, i do not remember the exact name of the function. But belive me it is very perfect. If u r using VC++, then let me know i will help u out.
Plz let me know when u have found out it.
A word of caution it is a blocking call i.e, it does not come out until a key is pressed.
Venkat Raghavulu.

This is a program for wich ASCII code belongs to wich key. The program was origanally in Dutch but i changed the interface to English.
When you strike a key like F1 you get a code like this: 0.59 This is not the code you must use when you write your program. You first have to detect the first code '0'. Immediatly after that you "catch" the second code '59' and then it knows you pressed F1. If you need an example with comment of this you can e-mail me. BTW there is an example in this program.#include
#include
#includeint Menu(char keuze);
void ToetsChar();
void AlleChars();void main()
{
char K;
char keuze;do {
K = Menu(keuze);switch (K)
{
case '1' : ToetsChar(); break;
case '2' : AlleChars(); break;
case '3' : break;
}
} while (K!='3');
}int Menu(char keuze)
{
clrscr();
cout > keuze;return keuze;
}void ToetsChar()
{
clrscr();
int toets,tel;
char toets2;cout to return to the menu\n\n";
for (; toets!=27;)
{
toets=getch();
toets2=toets;
if (toets == 0)
{
cout \n";
getch();for (karak=27,tel=1; karak<=255; karak++,tel++)
{symb=karak;
cout << karak << " = "<< symb << endl;
delay(10);if (tel == Eind_Scherm)
{
cout << "Press a key";
getch();
clrscr();
tel=1;
Eind_Scherm = 25;
}
}
cout << "Press a key to return to the menu";
getch();
}

Is there a way to constantly track keys pressed, even when the C++ program is not the active form? I mean that the program is running but is in the background. So can a person be using Word and there still be a way to extract the keys they pressed from the buffer? Please email me or post code. Thanks a bunch.

![]() |
VB Hyperlink
|
win32 console programming
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |