描述
我使用Kivy框架,并在Python3中运行一个脚本,该脚本从HID设备读取输入。该设备是一个条形码扫描仪,模拟键盘。
问题
脚本在后台同时打开一个弹出窗口和条形图读取过程。每次尝试读取条形码时,我都必须处于活动窗口中。在我的情况下,这是提示(控制台(。这意味着我必须点击控制台窗口,然后阅读器才能工作。无论我如何从主脚本(方法、线程、子进程、Clock.schedule_one(调用读取过程,都会发生这种情况。我如何在提示窗口之外获得输入,例如在控制台外的主kivy gui中?
这些其他帖子确实有所帮助,但没有提供答案:
- 如何使用evdev从Python中的HID设备获取字符串
- python:从USB HID设备读取
- 如何使用evdev从Python中的HID设备获取字符串
- 读取HID输入,同时窗口为"0";失焦";在Python中
代码
使用str = input("")
作为子流程:
#!/usr/bin/python3.5 python3.5
# -*- coding: utf-8 -*-
# This script reads the output from HID device e.g. bar/rfid scanner
import sys
import inspect
import csv
# return the current line number
def lineno():
return inspect.currentframe().f_back.f_lineno
str = input("Enter your input: ")
使用evdev.ecodes.EV_KEY
作为子流程:
import evdev
from evdev import InputDevice, categorize, ecodes
device = evdev.InputDevice('/dev/input/event20')
while True:
try:
for event in device.read_loop():
if event.type == evdev.ecodes.EV_KEY:
print(evdev.categorize(event))
except:
print("Keyboard Interrupt")
break
我在这里找到了一个解决方案https://khanhicetea.com/post/read_input_from_usb_keyboard_in_linux/.我将输入检测文件作为子进程调用。由于我没有在CODE_MAP_CHAR{}
中找到enter
密钥的快速替代品,所以我为enter
密钥添加了具有不同类型代码的scancodes{}
。要查找不同设备的名称和其他属性,请使用cat /proc/bus/input/devices
。以下是工作代码。
#!/usr/bin/python3.5 python3.5
# -*- coding: utf-8 -*-
# This script reads the output from HID device e.g. ECCO bar/rfid scanner
# Make sure evdev is installed or install it with: pip3 install evdev.
# The Kernel on the Pi creates an input device which resides in /dev/input/.
# You can find out the names and other attributes of different devices using:
# cat /proc/bus/input/devices
import sys
import inspect
import serial
import time
import random
import csv
import evdev
from evdev import InputDevice, categorize, ecodes, list_devices
device = evdev.InputDevice('/dev/input/event20')
print(device)
# reserve the device
device.grab()
#setup vars
eccoOut=[]
x = ''
caps = False
CODE_MAP_CHAR = {
'KEY_MINUS': "-",
'KEY_SPACE': " ",
'KEY_U': "U",
'KEY_W': "W",
'KEY_BACKSLASH': "\",
'KEY_GRAVE': "`",
'KEY_NUMERIC_STAR': "*",
'KEY_NUMERIC_3': "3",
'KEY_NUMERIC_2': "2",
'KEY_NUMERIC_5': "5",
'KEY_NUMERIC_4': "4",
'KEY_NUMERIC_7': "7",
'KEY_NUMERIC_6': "6",
'KEY_NUMERIC_9': "9",
'KEY_NUMERIC_8': "8",
'KEY_NUMERIC_1': "1",
'KEY_NUMERIC_0': "0",
'KEY_E': "E",
'KEY_D': "D",
'KEY_G': "G",
'KEY_F': "F",
'KEY_A': "A",
'KEY_C': "C",
'KEY_B': "B",
'KEY_M': "M",
'KEY_L': "L",
'KEY_O': "O",
'KEY_N': "N",
'KEY_I': "I",
'KEY_H': "H",
'KEY_K': "K",
'KEY_J': "J",
'KEY_Q': "Q",
'KEY_P': "P",
'KEY_S': "S",
'KEY_X': "X",
'KEY_Z': "Z",
'KEY_q': "q",
'KEY_w': "w",
'KEY_e': "e",
'KEY_r': "r",
'KEY_t': "t",
'KEY_z': "z",
'KEY_u': "u",
'KEY_i': "i",
'KEY_o': "o",
'KEY_p': "p",
'KEY_a': "a",
'KEY_s': "s",
'KEY_d': "d",
'KEY_f': "f",
'KEY_g': "g",
'KEY_h': "h",
'KEY_j': "k",
'KEY_l': "l",
'KEY_y': "y",
'KEY_x': "x",
'KEY_c': "c",
'KEY_v': "v",
'KEY_b': "b",
'KEY_n': "n",
'KEY_m': "m",
'KEY_KP4': "4",
'KEY_KP5': "5",
'KEY_KP6': "6",
'KEY_KP7': "7",
'KEY_KP0': "0",
'KEY_KP1': "1",
'KEY_KP2': "2",
'KEY_KP3': "3",
'KEY_KP8': "8",
'KEY_KP9': "9",
'KEY_5': "5",
'KEY_4': "4",
'KEY_7': "7",
'KEY_6': "6",
'KEY_1': "1",
'KEY_0': "0",
'KEY_3': "3",
'KEY_2': "2",
'KEY_9': "9",
'KEY_8': "8",
'KEY_LEFTBRACE': "[",
'KEY_RIGHTBRACE': "]",
'KEY_COMMA': ",",
'KEY_EQUAL': "=",
'KEY_SEMICOLON': ";",
'KEY_APOSTROPHE': "'",
'KEY_T': "T",
'KEY_V': "V",
'KEY_R': "R",
'KEY_Y': "Y",
'KEY_TAB': "t",
'KEY_DOT': ".",
'KEY_SLASH': "/",
}
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'
}
def parse_key_to_char(val):
return CODE_MAP_CHAR[val] if val in CODE_MAP_CHAR else ""
if __name__ == "__main__":
print("list of devices:")
devices = [InputDevice(fn) for fn in list_devices()]
for device in devices:
print("t{}t{}".format(device.fn, device.name))
event_id = 20 # check your events to set the correct value
exclusive_access = 1
device = InputDevice('/dev/input/event{}'.format(event_id))
if int(exclusive_access) == 1:
device.grab()
eccoBarcodeList = []
for event in device.read_loop():
if event.type == evdev.ecodes.EV_KEY:
e = categorize(event)
if e.keystate == e.key_up:
sys.stdout.write(parse_key_to_char(e.keycode))
cifra = parse_key_to_char(e.keycode)
eccoBarcodeList.append(cifra)
print(eccoBarcodeList)
sys.stdout.flush()
if e.scancode == 28:
print("return key detected, exiting...")
break