当窗口“不在焦点上"时,读取HID输入.在Python中 [英] Read HID input while window is "out of focus" in Python

查看:262
本文介绍了当窗口“不在焦点上"时,读取HID输入.在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在努力解决这个问题,但无法正常工作. 我刚开始使用python,现在已经遇到了在此项目中将要遇到的最大问题.

I'm struggling with an issue for some days and I can't get it working. I've just started with python and I'm now already facing the biggest problem which I will face in this project.

在这种情况下:我必须编写一个扫描条形码的程序.将此与在线服务进行通信并打印PDF.一切正常,但是当窗口不清晰"时,我也想扫描条形码.因此,我希望允许人们在最小化窗口的同时使用条形码扫描仪进行扫描.

Here's the situation: I have to make a program which scans a barcode. Communicates this to a online service and prints a PDF. This all works fine, but I also want to scan the barcode when the window is "out of focus". So I want to allow people to scan with the barcode scanner while they have the window minimized.

我正在运行Windows 8,并且正在使用python 3.3.条形码扫描仪是一种HID设备,可将自身显示为键盘.我已经尝试过将该项目转换为python 3.3,但是它不起作用. -> http://learn.adafruit.com/barcode-scanner/overview 我最终会出现各种错误,但我仍然无法完全理解.

I'm running windows 8 and I'm working with python 3.3. The barcode scanner is a HID device and it presents itself as a keyboard. I already tried to convert this project to python 3.3, but it does not work. --> http://learn.adafruit.com/barcode-scanner/overview I end up with all kinds of errors, which I still not completly understand.

今天我尝试使用pywinusb,但是此模块似乎在查找任何HID设备方面存在问题,因为它也找不到我的键盘和鼠标.

Today I tried to use pywinusb, but this module seems to have issues regarding to finding any HID device because it also can't find my keyboard and mouse.

也许这是一个复杂的问题,但是希望有人对此有经验并且知道如何使它起作用.

Maybe it's a complicated question, but hopefully someone has expirience with this and knows how to get this working.

推荐答案

如果条形码扫描仪以键盘的形式出现,那么您基本上不是想要在后台运行的键盘记录仪吗? 搜索它时,是最早的Google结果"python键盘记录程序"-根据消息来源,它需要 pyWin32 pyHook .我删除了日志记录以将代码示例减少到最低限度,只需将处理代码放入OnKeyboardEvent中即可.我对此进行了测试,并且可以在Windows 7上的Python 2.7安装中使用,但是这些模块应该与Python 3.3兼容.

If the barcode scanner presents itself as a keyboard, isn't what you want basically a key logger that runs in the background? Searching for it, this was among the first google results for "python keylogger" - according to the source, it needs pyWin32 and pyHook. I removed the logging to reduce the code sample to a minimum, just put the handling code inOnKeyboardEvent. I tested this and it works with my Python 2.7 installation on Windows 7, but the modules should be compatible with Python 3.3.

import pythoncom, pyHook, sys, logging

def OnKeyboardEvent(event):
    print "Key: ", chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

这篇关于当窗口“不在焦点上"时,读取HID输入.在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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