Read and Write to a Keyboard device in Linux using C++
来源:互联网 发布:碇真嗣喜欢谁知乎 编辑:程序博客网 时间:2024/06/15 13:40
I had to write some userspace code to read and write to a keyboard device in Linux using C++. I thought it would be as simple and open(), read() and write(). I was close but was missing a couple key items: scan codes and input_event.
Open Device
Opening the keyboard device is the same as opening any other device in linux
char *device = "/dev/input/event1";
if( (fd= open(device, O_RDWR))< 0 )
{
// Read or Write to device
}
Read Keyboard Device
Read from the keyboard device. Instead of passing a char array as the buffer, pass an input_event struct to store the results of the read. KEY_UP or any other scan code numeric value is specific to your Linux key mapping. KEY_UP is defined for your Linux setup so do not forget to include linux/input.h. Use the definition for the key scan code and not the actual scan code numeric value.
#define EV_PRESSED 1
#define EV_RELEASED 0
#define EV_REPEAT 2
struct input_event event;
unsigned int scan_code = 0;
int num_bytes = read(fd,&event, sizeof(struct input_event));
if(event.type!= EV_KEY)
return; // Keyboard events are always of type EV_KEY
if(event.value== EV_RELEASED)
{
scan_code = event.code;
if(scan_code == KEY_UP)
// Do something if keyboard up arrow pressed
}
Write to Keyboard Device
To write to the device, you populate the input_event struct with the key scan code. You also set the type to EV_KEY and the value to either “press” or “release”. To represent a keyboard, you must do a press and a release because some code may be looking for a key press or a key released.
// Press the key down
event.type = EV_KEY;
event.value = EV_PRESSED;
event.code = KEY_UP;
write(fd, &event, sizeof(struct input_event));
// Release the key
event.value = EV_RELEASED;
event.code = KEY_UP;
write(fd, &event, sizeof(struct input_event));
- Read and Write to a Keyboard device in Linux using C++
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- BasicExcel - A Class to Read and Write to Microsoft Excel
- How to read and write JSON files in Java(Gson)
- QueryBuilder: A Simple Way to Read and Write Query Strings
- How to write a script in Linux
- How to write a Linux USB Device Driver
- how to read directory name using std c in the linux
- Read and write raw PCM using GStreamer
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- How to read/write files within a Linux kernel module?
- write and read mtd device(轉載自http://stackoverflow.com/questions/15336285/write-on-a-mtd-block-devic)
- CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet
- How to Reset USB Device in Linux–using libusb
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- CRM 4.0: How to Write and Deploy a Plug-In
- VI配置
- log4j学习记录 log4j配置文件的配置
- hibernate 中createQuery与createSQLQuery
- Flex Bindable的用法
- Java抽取Word,PDF格式文件的四种武器
- Read and Write to a Keyboard device in Linux using C++
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- 单链表的实现
- 使用git获得meego源码(zt)
- hibernate执行sql语句的总结
- NameValueCollection详解
- exchange2003邮件追踪出错的解决之道
- 一个简单的日期校验示例
- Android 之 ListView (一)