What are the ascii codes of the arrow keys on the keyboard?

Asked

Viewed 3,084 times

1

I am working on a project in C and came across the following method

static void keyEvent( unsigned char key, int x, int y)
{
    /* quit if the ESC key is pressed */
    if( key == 0x1c ) {
        printf("*** %f (frame/sec)\n", (double)count/arUtilTimer());
        cleanup();
        exit(0);
    }
}

I need to perform some functions by clicking the directional keys , I tried to printar on the console but I did not succeed... I use ASCII code p/ catch the click on these keys? If yes, which are?

  • I was going to say p/ you printar and see what the screens are but you said you couldn’t, what happened?

  • http://stackoverflow.com/a/2877857/6101515

  • @Paulohdsousa gave error and simply closed the Prog, perhaps because of the variable type, I tried printf(key); inside the keyEvento, before the first if.

  • You may find on the net the ASCII value p/ these keys but it is not reliable, the best is you pick up yourself, these values change depending on the OS.

  • @Juniornunes worth, I had already seen this post, but anyway, I found nothing here in Sopt regarding this, I saw in this same post that using Opengl and GLUT exists for example GLUT_KEY_UP, maybe that’s the way...

1 answer

2


These keys do not have "ASCII" code. The ASCII table consists of 128 characters, send 32 control and 96 printable - and the control ones contain only "digitable" keys - type "enter", "tab" - and some others for which the current PC keyboards do not have "beep" keys, "form feed", etc...

Well, only the current keyboard has 104 keys - mostly modifiable with shift and/or alt - and the shift and alt themselves have no match in this table.

What happens is that the keyboard sends a hardware code to each keystroke, and the operating system translates and makes these signals available to programs - but this is done in different layers, and what code you get for which key depends a lot on which call to the system you’re making.

The operating system method you are using is used by the C library function you use to read the keyboard- as you did not put which is the effective call to get the key code, it is not possible to expand this answer with a survey about what codes would be available to you.

The tip is to realemten print the incoming code -and that, some read functions, for those keys that do not have a direct ASCII code, the S.O. provides a multi-byte code, and returns one byte at a time to your program.

Print the code on the screen is the best solution for you to try to isolate them:

static void keyEvent( unsigned char key, int x, int y) { 
    printf("Code: %02x\n", key);
}

Now, pay attention to another tip: It is useless to invest a lot of time in a super-interface for your program if it will only work in Windows CMD: most of these functions depend not only on the operating system and some will depend on the type of terminal.
If the program is realemten useful for renting thing: (1) power users are accustomed to terminal programs that work only "top to bottom"- preferably consuming command linah parameters, and with one or another occasional data entry; Users "no power" expect a graphical interface anyway.

Alias, if you want to have a legal interface,the tip is to make the program in another language - like Python, Java , Ruby and choose a library for graphical interfaces (or even make your program be web-based). Even if your program has parts that are best done in C (for example, a calculation sequence where it is better to have all the processor control), you can call isolated functions of C from the program done in these other languages.

  • Thank you very much for the answer and, mainly, for the tips.

  • 1

    So - I saw in your comment that you tried printf(key); - You’re really gonna suck it. C is a language that gives you all the control of the computer, but it’s all on your own - the functions have no polymorphism or "Duck Typing" which are mechanisms that allow them to accept different types of parameters. The first parameter for printf has to be a string, always. (And a character is a "scalar" data - whereas a string is always accessed as a memory address). The function called in C has no way of knowing if Voce pass the. wrong pair. (But it must have given Warning)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.