Python pynput keyboard.press()导致系统挂接

Python pynput keyboard.press()导致系统挂接,python,keyboard,pynput,Python,Keyboard,Pynput,我正在尝试使用pynput模块为我的Logitech G600创建一个键映射工具。Windows上存在用于此目的的软件,但不适用于Linux。当我要调用正确的映射键时,我的整个系统会挂断一秒钟左右。由于对问题(或线程)了解不多,我决定将键盘.press()调用插入线程,希望hitch将自身定位到线程,而不是系统(或其他东西)。是的,我需要学习更多关于线程的知识。不过,现在是否有一个简单的搭车修复方法,或者这仅仅是pynput模块固有的吗 Python==3.8.5,Pynput==1.7.3 有

我正在尝试使用pynput模块为我的Logitech G600创建一个键映射工具。Windows上存在用于此目的的软件,但不适用于Linux。当我要调用正确的映射键时,我的整个系统会挂断一秒钟左右。由于对问题(或线程)了解不多,我决定将键盘.press()调用插入线程,希望hitch将自身定位到线程,而不是系统(或其他东西)。是的,我需要学习更多关于线程的知识。不过,现在是否有一个简单的搭车修复方法,或者这仅仅是pynput模块固有的吗

Python==3.8.5,Pynput==1.7.3

有关守则:

if button == 79:
    print("G9 clicked!")
    x = threading.Thread(target=send_key, args=(Key.esc,), daemon=True)
    x.start()
调用函数:

def send_key(_key):
    global keyboard

    print("Thread run")
    keyboard.press(_key)
    keyboard.release(_key)
编辑:有趣的是,将pynput/threading调用简单地替换为“subprocess.call('xdool key Escape',shell=True)”或其“os.system(command)”等效项会导致相同的挂接问题

如果有帮助,我正在从鼠标的位置“/dev/input/by_id/…mouse_file…”读取。虽然我相当确定错误是由上述代码引起的,但如果代码有用,下面是完整的代码:

import os
import struct
from pynput.keyboard import Key, Controller
import threading


keyboard = Controller()

def get_mouse(_path, _file_start, _file_end):

    files = os.listdir(_path)
    for file in files:
        if file.startswith(_file_start) and file.endswith(_file_end):
            return _path + file

def send_key(_key):
    global keyboard

    print("Thread run")
    keyboard.press(_key)
    keyboard.release(_key)

path = "/dev/input/by-id/"
file_start = "usb-Logitech_Gaming_Mouse_G600_"
file_end = "-if01-event-kbd"
file = get_mouse(path, file_start, file_end)

with open(file, 'rb') as mouse_events:
    while True:
        events = mouse_events.read(3)
        button, x, y = struct.unpack('3b', events)
        release = False
        if button == 79:
            print("G9 clicked!")
            x = threading.Thread(target=send_key, args=(Key.esc,), daemon=True)
            x.start()
        if button == 80:
            print("G10 clicked!")
        if button == 81:
            print("G11 clicked!")
        if button == 75:
            print("G12 clicked!")
            x = threading.Thread(target=send_key, args=(Key.f5,), daemon=True)
            x.start()
        if button == 76:
            print("G13 clicked!")
        if button == 77:
            print("G14 clicked!")
        if button == 71:
            print("G15 clicked!")
        if button == 72:
            print("G16 clicked!")
        if button == 73:
            print("G17 clicked!")
        if button == 82:
            print("G18 clicked!")
        if button == 74:
            print("G19 clicked!")
        if button == 78:
            print("G20 clicked!")


为什么要使用/dev/input而不是pynput.mouse.Listener来侦听鼠标事件?我想避免重新映射实际键盘,只需重新映射G600一侧的小数字键盘。据我所知,pynput对待真实键盘上的数字就像对待鼠标侧面的数字键盘一样。这意味着,如果我监听鼠标按钮的默认设置(例如,“1”键),它也会响应我在键盘上单击同一键的时间。我想避免这样。