How to read Linux key values, input subsystem, KEY,DEV/INPUT/EVENT,DEV/EVENT,C language keyboard

Source: Internet
Author: User
Tags 04x reserved sprintf

I believe you use embedded all want to read the key value directly, especially the chip manufacturers have provided input-driven cases, such as GPIO or scan the type of keyboard. So how do you get the key value in the application layer through the C language?

To the brothers a heavyweight source code, see below, we take to compile and run to know what is going on, of course, you can use Select instead of while () to read a little better, leaving you to imagine:

Attention:

#include <linux/input.h>

For the core source of the header file, note the path, generally for kernel/include/linux/input.h

EV.C:

* * Copyright 2002 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated Docum Entation Files (The * "Software"), to deal in the Software without restriction, including * without limitation on the RI  Ghts to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit Persons to whom the Software are furnished to does so, * subject to the following conditions: * * The above copyright not Ice and this permission notice (including the * next paragraph) shall is included in all copies or substantial * portion
 S of the Software. * * The SOFTWARE is provided ' as is ', without WARRANTY of any KIND, * EXPRESS OR implied, including BUT don't LIMITED to T  He warranties of the * merchantability, FITNESS for A particular purpose and * non-infringement. In NO EVENT SHALL RED HAT and/or their suppliers * is liable for Any CLAIM, damages or liability, WHETHER in a * ACTION of CONTRACT, TORT OR Otherwise, arising from, out of or in
 * CONNECTION with the SOFTWARE or, or other dealings, in the * SOFTWARE.
 * * This is the simple test program "reads from/dev/input/event*, * decoding events into a human readable form." * * * Authors: * Rickard E. (Rik) Faith <faith@redhat.com> * * */#include <stdio.h> #include <stdl ib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <fcntl.h> #

Include <errno.h> #include <time.h> #include <linux/input.h> struct input_event event;           int main (int argc, char **argv) {char name[64];  /* Rats:use OK, but could be better */char buf[256] = {0,}; /* rats:use OK */unsigned char MASK[EV_MAX/8 + 1];
    /* rats:use OK */int version;
    int fd = 0;
    int RC;
    int I, J; ChaR *tmp; #define TEST_BIT (bit) (mask[(bit)/8] & (1 << ((bit)%8)) for (i = 0; i < i++) {sprintf (name
        , "/dev/input/event%d", I);
            if (fd = open (name, o_rdonly, 0)) >= 0) {IOCTL (FD, eviocgversion, &version);
            IOCTL (FD, Eviocgname (sizeof (BUF)), buf);
            IOCTL (FD, eviocgbit (0, sizeof (mask)), mask);
            printf ("%s\n", name); printf ("Evdev version:%d.%d.%d\n", Version >>, (version >> 8) & 0xFF, Version & Amp
            0xFF);
            printf ("Name:%s\n", buf);
            printf ("Features:");
                    for (j = 0; J < Ev_max; J + +) {if (Test_bit (j)) {const char *type = "Unknown";
                    Switch (j) {Case ev_key:type = "keys/buttons";     Case ev_rel:type = "relative";
                    Break     Case ev_abs:type = "absolute";
    Break                Case ev_msc:type = "reserved";
                    Break         Case Ev_led:type = "LEDs";
                    Break        Case ev_snd:type = "sound";
                    Break       Case Ev_rep:type = "repeat";
                    Break     Case Ev_ff:type = "Feedback";
                    Break
                printf ("%s", type);
            printf ("\ n");
        Close (FD);
        } if (argc > 1) {sprintf (name, "/dev/input/event%d", Atoi (argv[1));
            if (fd = open (name, O_RDWR, 0) >= 0) {printf ("%s:open, FD =%d\n", name, FD);
                for (i = 0; i < Led_max. i++) {event.time.tv_sec = time (0);
                event.time.tv_usec = 0;
                Event.type = ev_led;
                Event.code = i;
                Event.value = 0;
            Write (fd, &event, sizeof (event));
            
 }           while (rc = Read (FD, &event, sizeof (event)) > 0) {printf ("%-24.24s.%0 6lu type 0x%04x; Code 0x%04x; "" Value 0x%08x;
                       ", CTime (&event.time.tv_sec), Event.time.tv_usec,
                Event.type, Event.code, Event.value);
                        Switch (event.type) {case ev_key:if (Event.code > Btn_misc) { printf ("button%d%s", Event.code & 0xFF, Event.val Ue?
                    "Press": "Release");
                               else {printf ("Key%d (0x%x)%s", Event.code & 0xFF, Event.code & 0xFF, Event.value?
                    "Press": "Release");
                } break;
 Case Ev_rel:switch (Event.code) {                   Case rel_x:tmp = "X";
                    Break       Case rel_y:tmp = "Y";
                    Break  Case rel_hwheel:tmp = "Hwheel";
                    Break    Case rel_dial:tmp = "DIAL";
                    Break   Case rel_wheel:tmp = "WHEEL";
                    Break    Case rel_misc:tmp = "MISC";
                    Break default:tmp = "UNKNOWN";
                    Break
                    printf ("Relative%s%d", TMP, Event.value);
                Break        Case Ev_abs:switch (event.code) {case abs_x:tmp = "X";
                    Break        Case abs_y:tmp = "Y";
                    Break        Case abs_z:tmp = "Z";
                    Break       Case abs_rx:tmp = "RX";
                    Break       Case abs_ry:tmp = "RY";
                    Break       Case abs_rz:tmp = "RZ";
      Break              Case abs_throttle:tmp = "throttle";
                    Break   Case abs_rudder:tmp = "rudder";
                    Break    Case abs_wheel:tmp = "WHEEL";
                    Break      Case abs_gas:tmp = "gas";
                    Break    Case abs_brake:tmp = "BRAKE";
                    Break    Case abs_hat0x:tmp = "hat0x";
                    Break    Case abs_hat0y:tmp = "hat0y";
                    Break    Case abs_hat1x:tmp = "hat1x";
                    Break    Case abs_hat1y:tmp = "hat1y";
                    Break    Case abs_hat2x:tmp = "hat2x";
                    Break    Case abs_hat2y:tmp = "hat2y";
                    Break    Case abs_hat3x:tmp = "hat3x";
                    Break    Case abs_hat3y:tmp = "hat3y";
                    Break Case abs_pressure:tmp = "pressure";
                    Break Case abs_distance:tmp = "DISTANCE";
                    Break Case abs_tilt_x:tmp = "Tilt_x ";
                    Break   Case abs_tilt_y:tmp = "tilt_y";
                    Break     Case abs_misc:tmp = "MISC";
                    Break  default:tmp = "UNKNOWN";
                    Break
                    printf ("Absolute%s%d", TMP, Event.value);
                Break Case ev_msc:printf ("Misc");
                Break  Case ev_led:printf ("LED");
                Break  Case ev_snd:printf ("SND");
                Break  Case ev_rep:printf ("REP");
                Break   Case ev_ff:printf ("FF");
                    Break
                Break
            printf ("\ n");
            printf ("rc =%d, (%s) \ n", RC, Strerror (errno));
        Close (FD);
} return 0; }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.