使用宏键盘作为应用程序控制板

问题描述 投票:0回答:1

不久前,我买了一个小型 12 键(+2 个旋转开关)宏键盘,可以使用各种按键进行编程(https://github.com/jonnytest1/minikeyboard

现在我想使用该键盘来控制随机应用程序,但不会干扰“普通键盘”所做的任何事情(通过使用可以在 Windows 或其他应用程序上执行某些操作的任何按键) 为了实现这一目标,我的想法是:

  1. 将设备管理器中的“键盘驱动程序”覆盖为通用 HID 设备
  2. 在应用程序内编写我自己的键盘驱动程序
  3. 处理我想要它做的任何事情

我最初的尝试(有效): 使用 pywinusb 打开设备

VENDOR_ID = 0x1189
PRODUCT_ID = 0x8890

然后设置一个

device.set_raw_data_handler(devicebbound_handler(device))

半有效:

  • 它适用于媒体键(我收到这些键的事件,但它们也有 Windows 效果,所以它可能不起作用)
  • 如果我将其设置为任何“普通键”,例如 A B C 或 1 2 3,我不会得到任何
    raw_data_handler
    - 活动

我目前的直觉是我必须这样做

  • A)手动查询按键情况

  • B) 发送某种报告来启动键盘发送中断信号

不幸的是,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)
python keyboard usb hid pywinusb
1个回答
0
投票

想要将宏键盘用作应用程序控制板而不干扰正常的键盘功能。

您可以尝试在应用程序级别拦截键盘事件,而不是覆盖驱动程序:这将允许与普通键盘分开处理宏键盘输入。

当您从宏键盘收到事件时,请检查它是媒体键还是常规键。如果它是常规密钥,请在您的应用程序中处理它并防止它被发送到其他应用程序。

按下宏键盘上的按键应触发应用程序中的预定义操作:为此,为每个宏键定义到应用程序中特定功能的映射。

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   │
           └────────────────────┘

这应该可以帮助您将宏键盘用作应用程序控制板,而不会干扰正常的键盘功能。

© www.soinside.com 2019 - 2024. All rights reserved.