Linux under the input event event, you can use the background to intercept the keyboard press the key value, the background intercept data ... input.h

Source: Internet
Author: User
Tags 04x sprintf

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

Here first a piece of code, you get the Linux system to run, pay attention to the code KEYS_FD = open ("/dev/input/event2", o_rdonly); Here go to your/dev/input/event2 event, go to check your keyboard event is that one, if it is event3 then you change to Event3 good. Here's an order for you to see cat/proc/bus/usb/devices

http://www.jb51.net/LINUXjishu/30628.html if it is still not found please see this link.  Run the following code in a terminal, then open another button in the press, and then look at the output information in the first terminal you know what's going on, haha, is it the key that gets you pressed the keyboard???  Oh yeah! Recommended to look at the Linux input subsystem introduction and look at the Input.h file, a lot of online, you will find out what is going on.

#include <stdio.h>  #include <stdlib.h>  #include <unistd.h>  #include <string.h>  #include <sys/types.h>  #include <fcntl.h>  #include <errno.h>  #include < time.h>  #include <linux/input.h>  int main () {int Keys_fd;char ret[2];struct input_event t;keys_fd = Open ("/dev/input/event2", o_rdonly), if (keys_fd <= 0) {printf ("Open/dev/input/event2 device error!\n"); return 0;} while (1) {if (read (KEYS_FD, &t, sizeof (t)) * = sizeof (t)) {if (T.type = = Ev_key) if (T.value = = 0 | | t.value = = 1) {Prin TF ("Key%d%s\n", T.code, (t.value)? "Pressed": "released"); if (T.code = = Key_esc) break;}}} Close (KEYS_FD); return 0;}
Well, here's a heavyweight code, which is more complete than the above. Take it and run it and analyze it and know what 's going on ....

/** Copyright 2002 Red Hat Inc., Durham, North carolina.** All rights reserved.** Permission are hereby granted, free of CH Arge, to obtaining* a copy of this software and associated documentation files (the* "Software"), to deal in th e software without restriction, including* without limitation on the rights to use, copy, modify, merge,* publish, Distrib Ute, sublicense, and/or sell copies of the software,* and to permit persons to whom the software are furnished to do so,* s Ubject to the following conditions:** the above copyright notice and this permission notice (including the* next paragraph ) shall be included in all copies or substantial* portions of the software.** of the software are provided "as is", without WA Rranty of any kind,* EXPRESS OR implied, including and not LIMITED to the warranties of* merchantability, FITNESS for A PA  Rticular PURPOSE and* non-infringement. In NO EVENT shall RED HAT and/or their suppliers* being liable for any CLAIM, damages OR other liability, WHEther in an* ACTION of contract, TORT or OTHERWISE, arising from, out of or in* CONNECTION with the software or the use or  Other dealings in the* software.**-a simple test program, reads from/dev/input/event*,* decoding events into  A human readable form.*//** authors:* Rickard E. (Rik) Faith <[email protected]>**/#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include & lt;fcntl.h> #include <errno.h> #include <time.h> #include <linux/input.h> struct input_event even           T;int Main (int argc, char **argv) {char name[64];  /* Rats:use OK, but could is 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; #defin e test_bit (bit) (mask[(bit)/8] & (1 << (bit)%8))) for (i = 0; I < 32; i++) {sprintf (name, "/dev/input/event%d", I), if (fd = open (name, o_rdonly, 0)) >= 0) {IOCTL (FD, Eviocgversion, &ve rsion); IOCTL (FD, Eviocgname (sizeof (BUF)), buf), IOCTL (FD, eviocgbit (0, sizeof), mask);p rintf ("%s\n", name);    printf ("Evdev version:%d.%d.%d\n", Version >>, (version >> 8) & 0xFF, Version & 0xFF);p rintf (" Name:%s\n ", buf);p rintf (" Features: "); for (j = 0; J < Ev_max; J + +) {if (Test_bit (j)) {const char *type = "Unknown"; switch (j) {Case ev_key:type = "keys/buttons"; Break;case ev_rel:ty     PE = "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.value? "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;}



Linux under the input event event, you can use the background to intercept the keyboard press the key value, the background intercept data ... input.h

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.