#include#include using namespace std; int main() { char key; while (true) { if (kbhit()) // check if a key was pressed { key = getch(); // get the key that was pressed if (key == 'q') { cout << "Exiting program..." << endl; break; } else { cout << "You pressed: " << key << endl; } } } return 0; }
#includeThis example demonstrates how to use arrow keys to move a character on the console screen. Package/library: conio.h#include using namespace std; int main() { int x = 0, y = 0; while (true) { if (kbhit()) // check if a key was pressed { char key = getch(); // get the key // move character depending on arrow key pressed if (key == 75) //left arrow { x--; } else if (key == 77) //right arrow { x++; } else if (key == 72) //up arrow { y--; } else if (key == 80) //down arrow { y++; } // output character on new position system("CLS"); //clear console screen cout << endl << string(y, '\n') << string(x, 'X') << endl; } } return 0; }