Skip to content

Instantly share code, notes, and snippets.

@mrkara
Last active March 18, 2023 18:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkara/09cd56af4c44b60ba34598ffecd84921 to your computer and use it in GitHub Desktop.
Save mrkara/09cd56af4c44b60ba34598ffecd84921 to your computer and use it in GitHub Desktop.
[Read Key Press from Terminal under Linux with C++] #cpp #snippet
#include<iostream>
int main() {
char c;
// Set the terminal to raw mode
while(1) {
system("stty raw");
c = getchar();
// terminate when "." is pressed
system("stty cooked");
system("clear");
std::cout << c << " was pressed."<< std::endl;
if(c == '.') {
system("stty cooked");
exit(0);
}
}
}
//Adapted from https://www.tutorialspoint.com/Read-a-character-from-standard-input-without-waiting-for-a-newline-in-Cplusplus
@meutzitzu
Copy link

how do I make it so that it terminates on Ctrl+C ?

@Red-exe-Engineer
Copy link

Red-exe-Engineer commented Apr 23, 2022

how do I make it so that it terminates on Ctrl+C ?

Press the . key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment