Last active
July 21, 2024 10:07
-
-
Save mrkara/09cd56af4c44b60ba34598ffecd84921 to your computer and use it in GitHub Desktop.
[Read Key Press from Terminal under Linux with C++] #cpp #snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
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
how do I make it so that it terminates on Ctrl+C ?