linux下input event事件,可以用后台截取键盘按下的键值,后台截取数据.....input.h

如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘?

这里先上一段代码,自己拿到linux系统运行就可以,注意代码中keys_fd = open("/dev/input/event2", O_RDONLY);      这里去你的/dev/input/event2的event事件,去查看你的键盘事件是那一个,如果是event3那么你改成event3好了。这里给一个命令你去查看 cat
 /proc/bus/usb/devices

http://www.jb51.net/LINUXjishu/30628.html如果还是查找不到请看这个链接。   在一个终端中运行下面的代码,再打开另开一个中按下按键,然后查看第一个终端中输出的信息你就知道是怎么回事了,哈哈,是不是获取你按下键盘的键值了???
 oh yeah!  建议再去查看一下linux下input 子系统的介绍和查看一下input.h文件,网上一大堆,你一查你就知道了是怎么回事了。

#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)
			{
				printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");
				if (t.code == KEY_ESC)
					break;
			}
		}
	}

	close(keys_fd);

	return 0;
}

好了,这里给出一个重量级的代码,比上面的更完整.,自己拿去运行好好分析一下就知道怎么回事了....

/*
* 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 documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation on the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject 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.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
* BE 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.
*
* This is a simple test program that 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 <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 < 32; 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 >> 16, (version >> 8) & 0xff, version & 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"; break;
					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.%06lu 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;
}
时间: 2024-10-07 18:31:31

linux下input event事件,可以用后台截取键盘按下的键值,后台截取数据.....input.h的相关文章

BOM DOM Event事件笔记....

js//获取文件标题 document.body //body document.title //网页标题 document.doctype//文档对象 document.url//路径 //服务器相关 //align document.domain//域名 //---操作文本节点--- //获取父节点 var arrayObj = document.getElementsByTagName('div'); for(var i = 0 in arrayObj){ alert(arrayObj[i

对Android 软键盘向下的监听

因为业务需要,有时候我们好监听软键盘向下的动作,当我们按下向下的按钮时,可以进行监听,从而执行相应的动作. 于是我们写下下面的代码 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK){ Log.i(TAG, "onKeyDown"); } return super.onKeyDown(keyCode, event); } 我们按下向

TCP协议下的服务端并发,GIL全局解释器锁,死锁,信号量,event事件,线程q

TCP协议下的服务端并发,GIL全局解释器锁,死锁,信号量,event事件,线程q 一.TCP协议下的服务端并发 ''' 将不同的功能尽量拆分成不同的函数,拆分出来的功能可以被多个地方使用 TCP服务端实现并发 1.将连接循环和通信循环拆分成不同的函数 2.将通信循环做成多线程 ''' # 服务端 import socket from threading import Thread ''' 服务端 要有固定的IP和PORT 24小时不间断提供服务 能够支持并发 ''' server = sock

Event事件

# 事件驱动模型 #JavaScript 使我们有能力创建动态页面.事件是可以被 JavaScript 侦测到的行为.网页中的每个元素都可以产生某些可以触发 JavaScript 函数的事件.比方说,我们可以在用户点击某按钮时产生一个 onClick 事件来触发某个函数.###1. 事件驱动要素###**1.1 事件源**触发事件的元素**1.2 事件**被 JavaScript 侦测到的行为 >事件举例:- 鼠标点击- 鼠标悬浮于页面的某个热点之上- 键盘按键- 确认表单- 在表单中选取输入框

JavaScript中的Event事件对象详解

JavaScript事件[event] 在JavaScript中对于事件来讲,首先,我们需要了解这样几个概念:事件:事件处理程序:事件类型:事件流:事件冒泡:事件捕获:事件对象:浏览器内核:事件绑定:事件方面的性能优化(事件委托.移除事件处理程序):常见的浏览器兼容问题等. 什么是事件event: JavaScript事件是:浏览器.文档(document)窗口中的发生的特定的交互瞬间:而JavaScript和HTML之间的交互行为就是通过事件来触发的. 事件处理程序: 事件处理程序:我们用户在

JS(event事件)

常用的event事件: 属性 此事件发生在何时... onabort 图像的加载被中断. onblur 元素失去焦点. onchange 域的内容被改变. onclick 当用户点击某个对象时调用的事件句柄. ondblclick 当用户双击某个对象时调用的事件句柄. onerror 在加载文档或图像时发生错误. onfocus 元素获得焦点. onkeydown 某个键盘按键被按下. onkeypress 某个键盘按键被按下并松开. onkeyup 某个键盘按键被松开. onload 一张页面

CDI(Weld)高级&lt;4&gt; Event(事件) (转)

目录[-] 1. Event payload(事件的有效载入) 2. Event observers(event的观察者) 3. Event producers(event生产者) 4.AnnotationLiteral动态注入对应事件 5.Conditional observer methods 6.Event qualifiers with members 7.Multiple event qualifiers 8.事务性处理的transactional observers DEMO A:

javascript中window.event事件用法详解

转自http://www.jb51.net/article/32564.htm描述 event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有意义.比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义. 例子下面的例子检查鼠标是否在链接上单击,并且,如果shift键被按下,就取消链接的跳转. 复制代码代码如下: <

jQuery文本框(input textare)事件绑定方法教程

jquery 的事件绑定已经用on替换了原来的bind,接下来为大家分享下bind的使用方法及input textare事件.目前1.7以上,jquery?的事件绑定已经用on替换了原来的bind,接下来为大家介绍下bind的使用方法及input textare事件,感兴趣的朋友可以参考下 (1)jquery 绑定事件 目前1.7以上,jquery的事件绑定已经用on替换了原来的bind: 区别:(个人理解)bind是一次绑定事件到每一个子节点:on是只绑定到父节点,然后冒泡到各个子节点: 用法