不久前,我买了一个小型 12 键(+2 个旋转开关)宏键盘,可以使用各种按键进行编程(https://github.com/jonnytest1/minikeyboard)
现在我想使用该键盘来控制随机应用程序,但不会干扰“普通键盘”所做的任何事情(通过使用可以在 Windows 或其他应用程序上执行某些操作的任何按键) 为了实现这一目标,我的想法是:
我最初的尝试(有效): 使用 pywinusb 打开设备
VENDOR_ID = 0x1189
PRODUCT_ID = 0x8890
然后设置一个
device.set_raw_data_handler(devicebbound_handler(device))
半有效:
raw_data_handler
- 活动我目前的直觉是我必须这样做
或
不幸的是,HID 文档非常稀疏,所以我不知道我必须发送什么样的报告才能获得
(我很确定我必须使用带有usagePage: 12的键盘设备,这似乎至少存在)
应用程序的当前状态:
deviceList: Any = hid.HidDeviceFilter(vendor_id=VENDOR_ID,
product_id=PRODUCT_ID,).get_devices()
devices: list["hid.HidDevice"] = deviceList
def devicebbound_handler(device: hid.HidDevice):
def sample_handler(data):
print(device.product_name+" "+str(device.product_id))
print(data)
pass
return sample_handler
def isPLugged():
pl = False
for device in devices:
pl = pl | device.is_plugged()
return pl
for device in devices:
device.open()
device.set_raw_data_handler(devicebbound_handler(device))
while isPLugged():
sleep(0.01)
想要将宏键盘用作应用程序控制板而不干扰正常的键盘功能。
您可以尝试在应用程序级别拦截键盘事件,而不是覆盖驱动程序:这将允许与普通键盘分开处理宏键盘输入。
当您从宏键盘收到事件时,请检查它是媒体键还是常规键。如果它是常规密钥,请在您的应用程序中处理它并防止它被发送到其他应用程序。
按下宏键盘上的按键应触发应用程序中的预定义操作:为此,为每个宏键定义到应用程序中特定功能的映射。
import hid
from time import sleep
VENDOR_ID = 0x1189
PRODUCT_ID = 0x8890
# Function to handle device events
def device_bound_handler(device):
def sample_handler(data):
# Define your key mappings and app-specific actions here
if data[2] == 1: # Example: If the first key is pressed
# Perform specific action for key 1
print("Key 1 pressed on", device.product_name)
# Add more conditions for other keys
return sample_handler
# Check if device is plugged in
def is_plugged(devices):
return any(device.is_plugged() for device in devices)
# Main function to start the app
def start_app():
device_list = hid.HidDeviceFilter(vendor_id=VENDOR_ID, product_id=PRODUCT_ID).get_devices()
devices = [device for device in device_list]
for device in devices:
device.open()
device.set_raw_data_handler(device_bound_handler(device))
while is_plugged(devices):
sleep(0.01)
# Run the app
start_app()
这将拦截来自宏键盘的键盘事件并在应用程序中处理它们。您需要根据应用程序的要求定义每个键的具体操作。
Macro Keyboard ┌────────────────────────────────────────────────────┐
(12 keys, 2 │ - Python app to intercept and handle events │
rotary toggles)│ - Vendor ID: 0x1189, Product ID: 0x8890 │
│ - Mapping macro keys to app-specific functions │
└────────────────────────────────────────────────────┘
│
│ Intercepts and handles
│ macro keyboard events
▼
┌────────────────────┐
│ Application Logic │
│ - Custom actions │
│ for macro keys │
└────────────────────┘
这应该可以帮助您将宏键盘用作应用程序控制板,而不会干扰正常的键盘功能。