Listing 10. Determining a Device's Global Key and Button

State
uint8_t key_b[KEY_MAX/8 + 1];

memset(key_b, 0, sizeof(key_b));
ioctl(fd, EVIOCGKEY(sizeof(key_b)), key_b);

for (yalv = 0; yalv < KEY_MAX; yalv++) {
    if (test_bit(yalv, key_b)) {
        /* the bit is set in the key state */
        printf("  Key 0x%02x ", yalv);
        switch ( yalv)
            {
            case KEY_RESERVED :
                printf(" (Reserved)\n");
                break;
            case KEY_ESC :
                printf(" (Escape)\n");
                break;
            /* other keys / buttons not shown */
            case BTN_STYLUS2 :
                printf(" (2nd Stylus Button )\n");
                break;
            default:
                printf(" (Unknown key)\n");
            }
    }
}