修改键盘映射用xmodmap就okay了,配置文件如下:
保存为.Xmodmap,使用的话直接终端下输入xmodmap .Xmodmap就万事大吉了。
linux/input.h 中有定义,这个文件还定义了标准按键的编码等 struct input_event { struct timeval time//按键时间 __u16 type//类型,在下面有定义 __u16 code//要模拟成什么按键 __s32 value//是按下还是释放 }code: 事件的代码.如果事件的类型代码是EV_KEY,该代码code 为设备键盘代码.代码植0~127 为键盘上的按键代码,0x110~0x116 为鼠标上按键代码,其中0x110(BTN_ LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_ MIDDLE)为鼠标中键.其它代码含义请参看 include/linux/input.h 文件. 如果事件的类型代码是EV_REL,code 值表示轨迹的类型.如指示鼠标的X轴方向REL_X(代码为0x00),指示鼠标的Y 轴方向REL_Y(代码为0x01),指示鼠标中轮子方向 REL_WHEEL(代码为0x08). type: EV_KEY,键盘 EV_REL,相对坐标 EV_ABS,绝对坐标 value: 事件的值.如果事件的类型代码是EV_KEY,当按键按下时值为1,松开时值为0如果事件的类型代码是 EV_ REL,value 的正数值和负数值分别代表两个不同方向的值. /* * Event types */ #define EV_SYN 0x00 #define EV_KEY 0x01 //按键 #define EV_REL 0x02 //相对坐标(轨迹球) #define EV_ABS 0x03 //绝对坐标 #define EV_MSC 0x04 //其他 #define EV_SW 0x05 #define EV_LED 0x11 //LED #define EV_SND 0x12//声音 #define EV_REP 0x14//repeat #define EV_FF 0x15 #define EV_PWR 0x16 #define EV_FF_STATUS 0x17 #define EV_MAX 0x1f #define EV_CNT (EV_MAX+1) 1。模拟按键输入 //其中0 表示释放,1 按键按下,2 表示一直按下 //0 for EV_KEY for release, 1 for keypress and 2 for autorepeat. void simulate_key(int fd,int value) { struct input_event eventevent.type = EV_KEY//event.code = KEY_0//要模拟成什么按键 event.value = value//是按下还是释放按键或者重复 gettimeofday(&event.time,0)if(write(fd,&event,sizeof(event)) <0){ dprintk("simulate key error~~~\n")return } } 2。模拟鼠标输入(轨迹球) void simulate_mouse(int fd,char buf[4]) { int rel_x,rel_ystatic struct input_event event,ev//buf[0],buf[2],小于0 则为左移,大于0 则为右移 //buf[1],buf[3],小于0 则为下移,大于0 则为上移 dprintk("MOUSE TOUCH: x1=%d,y1=%d,x2=%d,y2=%d\n",buf[0],buf[1],buf[2],buf[3])rel_x = (buf[0] + buf[2]) /2rel_y = -(buf[1] + buf[3]) /2//和我们的鼠标是相反的方向,所以取反 event.type = EV_RELevent.code = REL_Xevent.value = rel_xgettimeofday(&event.time,0)if( write(fd,&event,sizeof(event))!=sizeof(event)) dprintk("rel_x error~~~:%s\n",strerror(errno))event.code = REL_Yevent.value = rel_ygettimeofday(&event.time,0)if( write(fd,&event,sizeof(event))!=sizeof(event)) dprintk("rel_y error~~~:%s\n",strerror(errno))//一定要刷新空的 write(fd,&ev,sizeof(ev))} 鼠标和键盘文件打开方法: int fd_kbd// /dev/input/event1 int fd_mouse//dev/input/mouse2 fd_kbd = open("/dev/input/event1",O_RDWR)if(fd_kbdkey.window = window->window//一定要设置为主窗口 event->key.keyval = keyval//FIXME:一定要加上这个,要不然容易出错 g_object_ref(event->key.window)gdk_threads_enter()//FIXME: 记得用这个来发送事件 gtk_main_do_event(event)gdk_threads_leave()gdk_event_free(event)} kernel 里input 模块 input_dev 结构: struct input_dev { void *privateconst char *nameconst char *physconst char *uniqstruct input_id id/* * 根据各种输入信号的类型来建立类型为unsigned long 的数组, * 数组的每1bit 代表一种信号类型, * 内核中会对其进行置位或清位 *** 作来表示时间的发生和被处理. */ unsigned long evbit[NBITS(EV_MAX)]unsigned long keybit[NBITS(KEY_MAX)]unsigned long relbit[NBITS(REL_MAX)]unsigned long absbit[NBITS(ABS_MAX)]unsigned long mscbit[NBITS(MSC_MAX)]unsigned long ledbit[NBITS(LED_MAX)]unsigned long sndbit[NBITS(SND_MAX)]unsigned long ffbit[NBITS(FF_MAX)]unsigned long swbit[NBITS(SW_MAX)]......................................... }/** * input_set_capability - mark device as capable of a certain event * @dev: device that is capable of emitting or accepting event * @type: type of the event (EV_KEY, EV_REL, etc...) * @code: event code * * In addition to setting up corresponding bit in appropriate capability * bitmap the function also adjusts dev->evbit. */ /* 记录本设备对于哪些事件感兴趣(对其进行处理)*/ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code) { switch (type) { case EV_KEY: __set_bit(code, dev->keybit)//比如按键,应该对哪些键值的按键进行处理(对于其它按键不予理睬) breakcase EV_REL: __set_bit(code, dev->relbit)breakcase EV_ABS: __set_bit(code, dev->absbit)breakcase EV_MSC: __set_bit(code, dev->mscbit)breakcase EV_SW: __set_bit(code, dev->swbit)breakcase EV_LED: __set_bit(code, dev->ledbit)breakcase EV_SND: __set_bit(code, dev->sndbit)breakcase EV_FF: __set_bit(code, dev->ffbit)breakdefault: printk(KERN_ERR "input_set_capability: unknown type %u (code %u)\n", type, code)dump_stack()return} __set_bit(type, dev->evbit)//感觉和前面重复了(前面一经配置过一次了) } EXPORT_SYMBOL(input_set_capability)static irqreturn_t gpio_keys_isr(int irq, void *dev_id) { int istruct platform_device *pdev = dev_idstruct gpio_keys_platform_data *pdata = pdev->dev.platform_datastruct input_dev *input = platform_get_drvdata(pdev)for (i = 0i <pdata->nbuttonsi++) { struct gpio_keys_button *button = &pdata->buttons[i]int gpio = button->gpioif (irq == gpio_to_irq(gpio)) {//判断哪个键被按了? unsigned int type = button->type ?: EV_KEYint state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low//记录按键状态 input_event(input, type, button->code, !!state)//汇报输入事件 input_sync(input)//等待输入事件处理完成 } } return IRQ_HANDLED} /* * input_event() - report new input event * @dev: device that generated the event * @type: type of the event * @code: event code * @value: value of the event * * This function should be used by drivers implementing various input devices * See also input_inject_event() */ void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { struct input_handle *handleif (type >EV_MAX || !test_bit(type, dev->evbit))//首先判断该事件类型是否有效且为该设备所接受 returnadd_input_randomness(type, code, value)switch (type) { case EV_SYN: switch (code) { case SYN_CONFIG: if (dev->event) dev->event(dev, type, code, value)breakcase SYN_REPORT: if (dev->sync) returndev->sync = 1break} breakcase EV_KEY: /* * 这里需要满足几个条件: * 1: 键值有效(不超出定义的键值的有效范围) * 2: 键值为设备所能接受(属于该设备所拥有的键值范围) * 3: 按键状态改变了 */ if (code >KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) returnif (value == 2) breakchange_bit(code, dev->key)//改变对应按键的状态 /* 如果你希望按键未释放的时候不断汇报按键事件的话需要以下这个(在简单的gpio_keys 驱动中不需要这个,暂时不去分析) */ if (test_bit(EV_REP, dev->evbit) &&dev->rep[REP_PERIOD] &&dev->rep[REP_DELAY] &&dev->timer.data &&value) { dev->repeat_key = codemod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]))} break........................................................ if (type != EV_SYN) dev->sync = 0if (dev->grab) dev->grab->handler->event(dev->grab, type, code, value)else /* * 循环调用所有处理该设备的handle(event,mouse,ts,joy 等), * 如果有进程打开了这些handle(进行读写),则调用其对应的event 接口向气汇报该输入事件. */ list_for_each_entry(handle, &dev->h_list, d_node) if (handle->open) handle->handler->event(handle, type, code, value)} EXPORT_SYMBOL(input_event)event 层对于input 层报告的这个键盘输入事件的处理: drivers/input/evdev.c: static struct input_handler evdev_handler = { .event = evdev_event, .connect = evdev_connect, .disconnect = evdev_disconnect, .fops = &evdev_fops, .minor = EVDEV_MINOR_BASE, .name = "evdev", .id_table = evdev_ids, }Linux 有自己的 input 子系统,可以统一管理鼠标和键盘事件。 基于输入子系统 实现的 uinput 可以方便的在用户空间模拟鼠标和键盘事件。 当然,也可以自己造轮子, 做一个字符设备接收用户输入,根据输入,投递 input 事件。 还有一种方式就是直接 往 evnent 里写入数据, 都可以达到控制鼠标键盘的功能。 本篇文章就是演示直接写入 event 的方法。 linux/input.h 中有定义,这个文件还定义了标准按键的编码等 struct input_event { struct timeval time//按键时间 __u16 type//类型,在下面有定义 __u16 code//要模拟成什么按键 __s32 value//是按下还是释放 }code: 事件的代码.如果事件的类型代码是EV_KEY,该代码code 为设备键盘代码.代码植0~127 为键盘上的按键代码, 0x110~0x116 为鼠标上按键代码,其中0x110(BTN_ LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_ MIDDLE)为鼠标中键.其它代码含义请参看 include/linux /input.h 文件. 如果事件的类型代码是EV_REL,code 值表示轨迹的类型.如指示鼠标的X轴方向 REL_X (代码为0x00),指示鼠标的Y 轴方向REL_Y(代码为0x01),指示鼠标中轮子方向 REL_WHEEL(代码为0x08). type: EV_KEY,键盘 EV_REL,相对坐标 EV_ABS,绝对坐标 value: 事件的值.如果事件的类型代码是EV_KEY,当按键按下时值为1,松开时值为0如果事件的类型代码是 EV_ REL,value 的正数值和负数值分别代表两个不同方向的值. /* * Event types */ #define EV_SYN 0x00 #define EV_KEY 0x01 //按键 #define EV_REL 0x02 //相对坐标(轨迹球) #define EV_ABS 0x03 //绝对坐标 #define EV_MSC 0x04 //其他 #define EV_SW 0x05 #define EV_LED 0x11 //LED #define EV_SND 0x12//声音 #define EV_REP 0x14//repeat #define EV_FF 0x15 #define EV_PWR 0x16 #define EV_FF_STATUS 0x17 #define EV_MAX 0x1f #define EV_CNT (EV_MAX+1) 下面是一个模拟鼠标和键盘输入的例子: #include #include #include #include #include #include #include #include #include #include #include void simulate_key(int fd,int kval) { struct input_event eventevent.type = EV_KEYevent.value = 1event.code = kvalgettimeofday(&event.time,0)write(fd,&event,sizeof(event)) event.type = EV_SYNevent.code = SYN_REPORTevent.value = 0write(fd, &event, sizeof(event))memset(&event, 0, sizeof(event))gettimeofday(&event.time, NULL)event.type = EV_KEYevent.code = kvalevent.value = 0write(fd, &event, sizeof(event))event.type = EV_SYNevent.code = SYN_REPORTevent.value = 0write(fd, &event, sizeof(event))} void simulate_mouse(int fd) { struct input_event eventmemset(&event, 0, sizeof(event))gettimeofday(&event.time, NULL)event.type = EV_RELevent.code = REL_Xevent.value = 10write(fd, &event, sizeof(event))event.type = EV_RELevent.code = REL_Yevent.value = 10write(fd, &event, sizeof(event))event.type = EV_SYNevent.code = SYN_REPORTevent.value = 0write(fd, &event, sizeof(event))} int main() { int fd_kbdint fd_mousefd_kbd = open("/dev/input/event1",O_RDWR)if(fd_kbd/** 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
}
注意:
#include<linux/input.h>
为内核源码的头文件,注意路径,一般为kernel/include/linux/input.h
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)