What are the ascii codes of the directional arrows on the keyboard?

1

I'm working on a C project 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 when I click on the directional keys ↑ I did not succeed ... I use the ASCII code to get the click on these keys? If so, what are they?

    
asked by anonymous 26.04.2017 / 16:06

1 answer

2

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

Well, only the current keyboard has 104 keys - mostly modifiable with shift and / or alt - and the shift and alt itself does not match this table.

What happens is that the keyboard sends a hardware code for each key pressed, 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 what system call you are making.

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

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

Printing 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's no use investing a long 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 any thing: (1) power users are accustomed to terminal programs that work only from top to bottom - preferably consuming command line parameters, and with occasional data entry; Non-power users 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 web-based). Even if your program has parts that are better to do in C (for example, a calculation sequence where it is better to have full control of the processor), you can call isolated C functions from the program done in those other languages. p>     

27.04.2017 / 13:21