简体   繁体   中英

How to detect a keyboard hit on Linux distribution [on hold]

I need a method to detect the keyboard hit whenever I hit a key, do a block of code and exit if I press ESC. I try using library conio.h but it does not work on Ubuntu OS. Any idea would be appreciate. Many thanks.

I would recommend placing a getchar() before your code to initialize your code. And why not use Ctrl+C to exit the code which raises a SIGINT to the process. You can even modify the default signal handler if you would like to display any particular message on exit.

Try this. Well i didn't understood your issue.What i got was, You want to exit if user enter ESC key from keyboard. ESC has value of 27 in ASCII. So just input the char and do comparison.I'm writting whole part pick out what ever you want.

char ch;
cout<<"Enter any character -";
cin>>ch;
if((ch>=65)&&(ch<=90))        
     printf("Capital Letter Entered\n"); 
else if((ch>=97)&&(ch<=122))
      printf("Small Letter\n");
else if((ch>=48)&&(ch<=57))
        printf("Number\n");
else if(((ch>=0)&&(ch<=47))||((ch>=58)&&(ch<=64))||((ch>=91)&&(ch<=96))||((ch>=123)&&(ch<=127)))
    printf("Special Character Entered.\n");

or // try this if you are just looking for ESC.
if(ch == 27)
{ cout<<"ESC pressed.";
   exit(0); }

Comment out if you need further help.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM