python获取键盘输入_如何使用Python获取原始的USB键盘数据?

我正在Python中使用PyUSB,因为我将不得不监听USB端口以从电子卡中检索数据.目前,我必须通过读取连接到Raspberry-Pi的小键盘(USB连接)的直接输入来训练自己.当然,我不想读取键入的字符串,例如,我希望获得ASCII码.我只是不知道如何从USB键盘读取输入.

我已经找到了一些片段:

import usb.core

import usb.util

VENDOR_ID = 0x0922

PRODUCT_ID = 0x8003

# find the USB device

device = usb.core.find(idVendor=VENDOR_ID,

idProduct=PRODUCT_ID)

# use the first/default configuration

device.set_configuration()

# first endpoint

endpoint = device[0][(0,0)][0]

# read a data packet

attempts = 10

data = None

while data is None and attempts > 0:

try:

data = device.read(endpoint.bEndpointAddress,

endpoint.wMaxPacketSize)

except usb.core.USBError as e:

data = None

if e.args == ('Operation timed out',):

attempts -= 1

continue

print data

如果我取消注释以下行“ device.set_configuration()”,这会导致“设备正忙”异常,那么我会收到错误16“设备正忙”或根本不显示任何错误…(我确实用键盘的VENDOR_ID和PRODUCT_ID替换了ids)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值