Hold screen to receive arrow keys
to use the arrow keys, you first need to save it to analyze it. This is why I am using scanf
to save it. But when I try to run this code and when I press a key it shows ^[[A
and when I press enter then it ^[[A
deletes and exits the program without printing a printf statement printf("%s",c).
andprintf("UP\n").
#include <stdio.h>
int main()
{
char c[50];
scanf("%s",&c);
printf("%s",c);
if (getch() == '\033'){ // if the first value is esc
getch();// skip the [
getch();// skip the [
switch(getch()) { // the real value
case 'A':
printf("UP\n");
break;
case 'B':
printf("DOWN\n");
break;
}
}
return 0;
}
+3
source to share
1 answer
It will be easy for you if you use the ncurses library. Just go through the documentation to see how to install it. After installation, read the part on Keyboard Interaction
Here is some sample code
#include <ncurses.h>
int main()
{
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
while(1)
{
ch = getch();
switch(ch)
{
case KEY_UP:
printw("\nUp Arrow");
break;
case KEY_DOWN:
printw("\nDown Arrow");
break;
case KEY_LEFT:
printw("\nLeft Arrow");
break;
case KEY_RIGHT:
printw("\nRight Arrow");
break;
}
if(ch == KEY_UP)
break;
}
endwin();
}
+1
source to share