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

本文提供了一种在Linux系统中读取键盘事件的方法,并附带示例代码,演示了如何使用C语言通过/dev/input/event设备文件来监听和解析键盘输入事件。

如何读取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 <faith@redhat.com>
*
*/

#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;
}



<think>我们将在Vue2中使用自定义指令实现el-input只能输入小数(保留指定位数)的功能。根据引用[3]的用法,我们可以设计一个灵活的指令,允许传入配置对象(如最大最小值、小数位数等)。同时参考引用[1]和引用[2]的指令结构。 ### 实现步骤 1. **创建自定义指令文件**:例如`src/directives/decimalInput.js` 2. **指令功能设计**: - 支持参数配置:`{ min, max, places }`(最小值、最大值、小数位数,默认小数位数为2) - 输入时过滤非数字、非小数点、非负号字符 - 处理多个小数点(只保留第一个) - 处理负号位置(只能在开头) - 限制小数位数 - 失焦时格式化(补0、范围限制等) 3. **注册指令**:在`main.js`中全局注册 ### 代码实现 ```javascript // src/directives/decimalInput.js const DecimalInput = { bind(el, binding, vnode) { const input = el.tagName === 'INPUT' ? el : el.querySelector('input'); if (!input) return; // 获取配置参数,默认小数位数2 const config = binding.value || {}; const places = config.places !== undefined ? config.places : 2; const min = config.min; const max = config.max; // 输入事件处理 input.addEventListener('input', function(e) { let value = e.target.value; let cursorPos = e.target.selectionStart; // 保存光标位置 // 正则过滤(允许负号、数字、小数点) let newValue = value .replace(/[^\d.-]/g, '') // 移除非数字、非小数点、非负号 .replace(/(?!^)-/g, '') // 负号只能在开头 .replace(/(\..*)\./g, '$1'); // 只保留第一个小数点 // 处理小数点开头的情况:0. if (newValue === '.') { newValue = '0.'; } else if (newValue.startsWith('-.')) { newValue = '-0.'; } // 限制小数位数 const decimalIndex = newValue.indexOf('.'); if (decimalIndex !== -1) { // 截取小数部分,并限制位数 const decimalPart = newValue.substring(decimalIndex + 1); if (decimalPart.length > places) { newValue = newValue.substring(0, decimalIndex + 1 + places); } } // 更新值 if (value !== newValue) { e.target.value = newValue; // 触发v-model更新 vnode.context.$nextTick(() => { // 重新设置光标位置(因为值改变会重置光标到末尾) e.target.setSelectionRange(cursorPos, cursorPos); }); } }); // 失焦事件处理:格式化、范围限制 input.addEventListener('blur', function(e) { let value = e.target.value; let numValue = parseFloat(value); // 如果输入不合法,则置为空或默认值 if (isNaN(numValue)) { e.target.value = ''; vnode.data.model.callback(''); return; } // 范围限制 if (min !== undefined && numValue < min) { numValue = min; } else if (max !== undefined && numValue > max) { numValue = max; } // 格式化小数位数 let fixedValue = numValue.toFixed(places); if (e.target.value !== fixedValue) { e.target.value = fixedValue; // 触发v-model更新 vnode.data.model.callback(fixedValue); } }); } }; export default { install(Vue) { Vue.directive('decimal-input', DecimalInput); } }; ``` ### 注册指令 ```javascript // main.js import Vue from 'vue' import App from './App.vue' import DecimalInput from './directives/decimalInput' Vue.use(DecimalInput) new Vue({ render: h => h(App) }).$mount('#app') ``` ### 使用示例 ```html <template> <div> <!-- 默认配置(小数位数2) --> <el-input v-decimal-input v-model="value1"></el-input> <!-- 自定义配置:小数位数3,最小值0,最大值100 --> <el-input v-decimal-input="{ places: 3, min: 0, max: 100 }" v-model="value2"></el-input> </div> </template> <script> export default { data() { return { value1: '', value2: '' } } } </script> ``` ### 指令解析 1. **输入时过滤**:通过正则表达式实时过滤非法字符,并确保格式正确。 2. **光标位置处理**:在输入过程中改变值时,保持光标在原来的位置(避免跳到最后)。 3. **失焦格式化**:在输入框失去焦点时,进行范围限制和小数位数格式化(补0)。 4. **配置参数**:通过指令值(binding.value)传入配置对象,实现灵活性。 > 注意:由于Element UI的el-input内部封装了input,所以指令中需要先获取内部的input元素(通过`el.querySelector('input')`)[^1][^2][^3]。 ### 相关问题 1. 如何实现整数输入限制? 2. 自定义指令中如何处理动态参数(比如小数位数动态变化)? 3. 如何在自定义指令中触发Element UI的表单验证? </think>在 Vue2 中,使用 `Vue.directive` 自定义指令实现 `el-input` 只能输入小数的完整解决方案如下: ### 自定义指令实现代码 ```javascript // src/directives/decimalInput.js export default { install(Vue) { Vue.directive('decimal-input', { bind(el, binding, vnode) { const input = el.querySelector('input'); const config = binding.value || {}; // 默认配置 const defaultConfig = { min: -Infinity, // 最小值 max: Infinity, // 最大值 places: 2, // 小数位数 allowNegative: true // 是否允许负数 }; // 合并配置 const { min, max, places, allowNegative } = Object.assign({}, defaultConfig, config); input.addEventListener('input', function(e) { let value = e.target.value; const cursorPos = e.target.selectionStart; // 保存光标位置 // 过滤非法字符 value = value .replace(/[^0-9.-]/g, '') // 只保留数字、小数点和负号 .replace(/(\..*)\./g, '$1') // 只能有一个小数点 .replace(/(?!^)-/g, ''); // 负号只能在开头 // 处理开头的小数点 if (value.startsWith('.')) value = '0' + value; if (value.startsWith('-.')) value = '-0' + value.substr(1); // 限制小数位数 const dotIndex = value.indexOf('.'); if (dotIndex !== -1) { const decimalPart = value.substring(dotIndex + 1); if (decimalPart.length > places) { value = value.substring(0, dotIndex + 1 + places); } } // 更新值 if (e.target.value !== value) { e.target.value = value; // 触发v-model更新 vnode.context.$nextTick(() => { // 恢复光标位置 e.target.setSelectionRange(cursorPos, cursorPos); // 触发 Element UI 的表单验证 input.dispatchEvent(new Event('input')); }); } }); // 失焦时格式化 input.addEventListener('blur', function(e) { let value = e.target.value; if (value === '' || value === '-') { e.target.value = ''; return; } // 转换为数字并格式化 let numValue = parseFloat(value); if (isNaN(numValue)) { e.target.value = ''; return; } // 范围限制 if (numValue < min) numValue = min; if (numValue > max) numValue = max; // 格式化小数位数 e.target.value = numValue.toFixed(places); // 触发v-model更新 vnode.data.model.callback(e.target.value); }); } }); } }; ``` ### 注册自定义指令 ```javascript // main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import decimalInput from '@/directives/decimalInput' // 导入指令 Vue.use(ElementUI) Vue.use(decimalInput) // 注册全局指令 new Vue({ render: h => h(App) }).$mount('#app') ``` ### 在组件中使用 ```html <template> <div> <!-- 默认配置(小数位数2,允许负数) --> <el-input v-decimal-input v-model="price1"></el-input> <!-- 自定义配置(小数位数3,范围0-100,禁止负数) --> <el-input v-decimal-input="{ places: 3, min: 0, max: 100, allowNegative: false }" v-model="price2" ></el-input> </div> </template> <script> export default { data() { return { price1: '', price2: '' } } } </script> ``` ### 功能说明 1. **输入过滤**: - 自动过滤非数字字符(只保留数字、小数点和负号) - 限制只能有一个小数点 - 负号只能在开头 - 自动修正开头的小数点(如 `.5` → `0.5`) 2. **小数位数控制**: - 默认保留2位小数 - 可通过 `places` 参数自定义小数位数 - 自动截断超出位数的小数 3. **范围限制**: - 通过 `min` 和 `max` 参数设置数值范围 - 失焦时自动修正超出范围的值 4. **负数控制**: - 默认允许负数 - 可通过 `allowNegative: false` 禁止负数 5. **光标位置处理**: - 输入过程中保持光标位置不变 - 避免因值改变导致光标跳转问题 6. **与 Element UI 集成**: - 自动触发 Element UI 的表单验证 - 兼容 `v-model` 双向绑定 ### 使用场景示例 ```html <!-- 价格输入(2位小数,0-9999范围) --> <el-input v-decimal-input="{ min: 0, max: 9999 }" v-model="productPrice" ></el-input> <!-- 百分比输入(1位小数,0-100范围) --> <el-input v-decimal-input="{ places: 1, min: 0, max: 100 }" v-model="discountRate" ></el-input> <!-- 温度输入(允许负数,1位小数) --> <el-input v-decimal-input="{ places: 1 }" v-model="temperature" ></el-input> ``` > **提示**:对于复杂场景(如动态小数位数、实时计算等),可在自定义指令中扩展更多功能[^1][^2][^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值