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++.