Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22288

How to run different codes when different keys are pressed in C on ubuntu linux? [duplicate]

$
0
0

This question already has an answer here:

I am trying to execute different code pieces when some special keyboard keys are pressed in a Console application in C.

I tried the following method in Windows and it's working:

#include <stdio.h>
#include <conio.h>

int main(void)
{
    char c;
    while(1)
    {
        if(kbhit())
        {
           char c;
           c = getch();
           if((int)ch == 27){
              //some code
           }
        }
    }
}

As you know, conio.h is not supported in Linux, therefore, my code is not working. I just need to find out which key is pressed without asking the user to enter it as an input and read it using scanf etc.

Is there an alternative? I don't need getch(), my first priority is kbhit().

By the way, I am using C programming language, not C++.


Viewing all articles
Browse latest Browse all 22288

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>