Created
October 26, 2017 18:08
-
-
Save Akronix/292d0133f85ca0fcc99d5d2933b209ee to your computer and use it in GitHub Desktop.
Small C snippet to get ascii code on keypress (for Unix systems)
This file contains 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 <stdio.h> | |
#include <termios.h> | |
#include <unistd.h> | |
// from https://stackoverflow.com/a/17271636/2904315 | |
#define clear() printf("\033[H\033[J") | |
// emulate getch() of conio.h | |
int getch() { | |
// from http://stackoverflow.com/a/23035044/15472 | |
struct termios oldattr, newattr; | |
int ch; | |
tcgetattr( STDIN_FILENO, &oldattr ); | |
newattr = oldattr; | |
newattr.c_lflag &= ~( ICANON | ECHO ); | |
tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); | |
ch = getchar(); | |
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); | |
return ch; | |
} | |
int main() | |
{ | |
int e = 0; | |
while( e != 4 ) { // while no EOT (End Of Transmission signal) | |
printf("\n Enter a character : "); | |
e=getch(); | |
if (e==12) { // FF signal, i.e. clear screen | |
clear(); | |
} | |
else { | |
printf("\n The ASCII value of the character is : %d",e); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment