9

So for the up key on the keyboard, I get 27, surprisingly for the down key I also get 27. I need my program to behave differently on the up and down key, and I can't seem to figure it out. I am using Linux, and need it to work for Linux.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
    int c = getchar();

    if(c==27)
    {
        printf("UP");
    }

    if(c==28)
    {
        printf("DOWN");
    }

} 
NoNameY0
  • 612
  • 2
  • 8
  • 18

5 Answers5

15

The 27 implies that you're getting ANSI escape sequences for the arrows. They're going to be three-character sequences: 27, 91, and then 65, 66, 67, 68 (IIRC) for up, down, right, left. If you get a 27 from a call to getchar(), then call it twice more to get the 91 and the number that determines what arrow key was pressed.

As someone else mentioned, this is platform-specific, but you may not care.

Community
  • 1
  • 1
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • So what should my approach be? My C program needs to behave differently when the user presses up and different when he presses down in the console – NoNameY0 Mar 09 '13 at 02:33
  • 3
    @RichardMckenna: Use a library. Curses, or Readline, depending on what your program does. – Dietrich Epp Mar 09 '13 at 02:47
  • There are environments where you can't use a library like that, but I found a really portable way. Using `read(fileno(stdin), &rval, 1);` seems to work everywhere. – Charles Lohr Nov 26 '22 at 08:38
4

Here is the program , which is written to use ncurses library , and display the arrow keys pressed.

#include<ncurses.h>

int main()
{
int ch;

/* Curses Initialisations */
initscr();
raw();
keypad(stdscr, TRUE);
noecho();

printw("Press E to Exit\n");

while((ch = getch()) != 'E')
{
    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;
    default:    
                printw("\nThe pressed key is %c",ch);

    }
}

printw("\n\Exiting Now\n");
endwin();

return 0;
}

while compiling , you have to link to the ncurses library.

gcc main.c -lncurses

Here is a tutorial to help you get started with ncurses.

Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • why is it not compiling in Netbeans? – NoNameY0 Mar 09 '13 at 16:10
  • @RichardMckenna , this C programwas written for gcc compiler on linux (and its working ). Make the appropriate changes for netbeans and get it working – Barath Ravikumar Mar 09 '13 at 16:56
  • you have to link it with ncurses , while compiling it , "gcc main.c -lncurses" , but first you must make sure , you have ncurses library installed – Barath Ravikumar Mar 09 '13 at 18:06
  • is there an alternative way of determing up key and down key. This can't be this complicated – NoNameY0 Mar 09 '13 at 18:18
  • since arrow keys have no ascii values , using only c , there can no be such way to determine those keys , using support libraries like ncurses , may be the only option – Barath Ravikumar Mar 09 '13 at 18:22
  • can you message me, I need to talk to you about something important. – NoNameY0 Mar 09 '13 at 18:26
  • @RichardMckenna well buddy , i don't know when i will have free time in my hands , so cant have a chat with anyone , if you have a technical doubt , do post it ,i'll surely look up your questions and answer them. – Barath Ravikumar Mar 09 '13 at 18:29
1

Check out the ncurses library at http://www.gnu.org/software/ncurses/. Linking it to your program will enable you to do all sorts of neat stuff with key events. Read up on the documentation and see if you want to use it. It provides exactly the functionality you say you're looking for.

Happy coding!

RouteMapper
  • 2,484
  • 1
  • 26
  • 45
0

You are confusing keys you press with the characters they generate when pressed. Would you expect to get one character when the shift key is pressed? Try this program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
    do
    {
        int c = getchar();
        printf("c=%d\n", c);
    }
    while (1);
}

Try hitting the up arrow and then enter. Then try hitting the down arrow and then enter. You will see that the conversion of keys to characters is not simple.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    So what should my approach be? My C program needs to behave differently when the user presses up and different when he presses down in the console – NoNameY0 Mar 09 '13 at 02:32
  • 2
    Did you run my program. You do get different values, so you can behave differently. You mistake was just thinking there would be a one-to-one correspondence between keys and characters. If you wanted to react differently if a person entered `a` with a shift or without a shift, you can because you do get different characters, even though there's no "shift key" character. – David Schwartz Mar 09 '13 at 02:48
0

This answer is just for future references; use getch() instead of getchar().

I was having this same issue (getting an escape sequence when arrows were pressed) and doing this changed fixed things.

If you're using curses.h you'll have to add the following line before you do the reading:

keypad(stdscr, TRUE);
Ramon Orraca
  • 31
  • 1
  • 4