如何使用evdev从Python中的HID设备获取字符串? [英] How can I get a String from HID device in Python with evdev?

查看:210
本文介绍了如何使用evdev从Python中的HID设备获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,但是有使用HID设备和evdev的经验.我有一个2D条形码扫描仪,它可以作为HID设备连接.目的是从QR码获取字符串.我能够识别Linux中的扫描仪,甚至可以在/dev/input中找到它的位置.

I am new to python but have experience with HID devices and evdev. I have a 2D barcode scanner which interfaces as HID device. The goal is to get the string from a QR code. I am able to recognize the scanner in Linux and even found its location in /dev/input.

我找到了evdev,并使用扫描仪实现了以下示例.这只是他们网站上的默认代码.它读取值,但会打印上下起伏的长事件代码.我看不到将其转换为字符串的简单方法.我要做的只是从Python的HID扫描器中读取一个字符串.任何帮助或指示,将不胜感激(也许evdev不是答案).

I found evdev and have implemented the example below with my scanner. This is just the default code on their site. It reads the values but it prints long event codes with downs and ups. I can't see an easy way to turn this into string. All I want to do is read in a string from the HID scanner in Python. Any help or direction would be appreciated (maybe evdev isnt the answer).

这是我当前的python代码,并带有一些示例输出:

Here is my current python code with some example output:

from evdev import *
dev = InputDevice('/dev/input/event1')

print(dev)

for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        print(categorize(event))

这是一些条形码的输出:

Here is the output from some barcodes:

key event at 1383327570.147000, 2 (KEY_1), down
key event at 1383327570.147990, 2 (KEY_1), up
key event at 1383327570.148997, 3 (KEY_2), down
key event at 1383327570.150010, 3 (KEY_2), up
key event at 1383327570.151009, 29 (KEY_LEFTCTRL), down
key event at 1383327570.151009, 42 (KEY_LEFTSHIFT), down
key event at 1383327570.152017, 36 (KEY_J), down
key event at 1383327570.153005, 36 (KEY_J), up
key event at 1383327570.154004, 29 (KEY_LEFTCTRL), up
key event at 1383327570.155005, 32 (KEY_D), down
key event at 1383327570.155993, 32 (KEY_D), up
key event at 1383327570.157002, 48 (KEY_B), down
key event at 1383327570.158015, 48 (KEY_B), up
key event at 1383327570.158997, 48 (KEY_B), down
key event at 1383327570.282002, 18 (KEY_E), up
key event at 1383327570.283004, 49 (KEY_N), down
key event at 1383327570.284005, 49 (KEY_N), up
key event at 1383327570.284968, 18 (KEY_E), down

非常感谢!

推荐答案

这里缺少一个转换步骤.您的输出已经是漂亮的格式,所以我将帮助您进一步分解:

There's a conversion step you're missing here. Your output is already in a pretty format, so i'll help you break it down a little more:

             Timestamp        , scancode, keycode, keystate
key event at 1383327570.147000, 2         (KEY_1), down
key event at 1383327570.147990, 2         (KEY_1), up

要对此有所帮助,您需要做几件事:

To make any useful sense of this, you need to do a couple of things:

  1. 仅通过过滤器侦听key_down类型的事件,仅针对特定类型的键状态(向下= 1,向上= 0)
  2. 将扫描代码转换为ASCII代码,该代码可能随设备而异,也取决于其映射到系统的方式!

但是有一种简单的方法来映射它们.使用在线服务生成具有所有可用字符的已知条形码,然后扫描该条形码并将每个输出的扫描码映射到扫描仪的正确字母/数字.您可以使用以下经过稍微修改的代码来更好地控制输出:

There's a simple-ish way to map them however. Generate a known barcode with all useable characters using an online service, then scan that barcode and map each scancode outputted to the correct letter/number for your scanner. You can use the following slightly modified piece of code to take better control of the output:

import evdev
from evdev import InputDevice, categorize  # import * is evil :)
dev = InputDevice('/dev/input/event1')

# Provided as an example taken from my own keyboard attached to a Centos 6 box:
scancodes = {
    # Scancode: ASCIICode
    0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
    10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
    20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
    30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
    40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
    50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}
for event in dev.read_loop():
    if event.type == evdev.ecodes.EV_KEY:
        data = evdev.categorize(event)  # Save the event temporarily to introspect it
        if data.keystate == 1:  # Down events only
            key_lookup = scancodes.get(data.scancode) or u'UNKNOWN:{}'.format(data.scancode)  # Lookup or return UNKNOWN:XX
            print u'You Pressed the {} key!'.format(key_lookup)  # Print it all out!

这是该脚本的一些示例输出

Here's some sample output from this script for me

You Pressed the A key!
You Pressed the B key!
You Pressed the C key!
You Pressed the UNKNOWN:99 key!

在线生成条形码后,您将知道哪个扫描码将映射到哪个值!建立自己的桌子并获利!

Once you generate some barcodes online, you'll know which scancode gets mapped to which value! Build your own table and profit!

HTH

这篇关于如何使用evdev从Python中的HID设备获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆