How to Get Keystrokes in C++
Use the "cin >>" function in C++ to capture user input from the keyboard. You get the entered characters when the user presses the Enter key. Include the "conio.h" function library in your source code to access the "_getchar()" function to capture individual keystrokes as they are entered. Avoid using the older C "getchar()" function to capture individual keystrokes. This method is now deprecated and is not in compliance with ISO C++ standard.
Instructions
-
-
1
Include the console input/output function library in the header of your source code with the Include command:
#include <conio.h>
-
2
Declare a variable to hold the captured input with the statement:
int a;
-
-
3
Assign the variable a value based on the keystroke with the function:
a = _getch();
-
1
Tips & Warnings
The "_getch()" function does not echo input to the screen. View the input capture with this function using "_putch(a);" inside the loop used to capture keystrokes.
References
- Photo Credit Jupiterimages/Comstock/Getty Images