程序最小化时的Tkinter热键

我正在为某个游戏编程,并且我想在游戏全屏运行时有一个热键来启动/停止脚本,我希望该热键为F7键。我尝试使用Bind.root和pynput来执行此操作,但是没有一种方法有效 这是我的代码:

import tkinter as tk
import ctypes
import time
import pynput

HEIGHT = 125
WIDTH = 200

hack_running = False

SendInput = ctypes.windll.user32.SendInput

PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]


def PressKeyPynput(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = pynput._util.win32.INPUT_union()
    ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))
    x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKeyPynput(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = pynput._util.win32.INPUT_union()
    ii_.ki = pynput._util.win32.KEYBDINPUT(0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.cast(ctypes.pointer(extra), ctypes.c_void_p))
    x = pynput._util.win32.INPUT(ctypes.c_ulong(1), ii_)
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def hack():
    if  hack_running:
        PressKeyPynput(0x02)
        time.sleep(0.08)
        ReleaseKeyPynput(0x02)
        PressKeyPynput(0x11)
        time.sleep(0.5)
        ReleaseKeyPynput(0x11)
        PressKeyPynput(0x1F)
        time.sleep(0.6)
        ReleaseKeyPynput(0x1F)
        PressKeyPynput(0x02)
        time.sleep(0.08)
        ReleaseKeyPynput(0x02)
        root.after(900000, hack)

def Start_stop():
    global hack_running
    if Startk['text'] == 'Start':
        hack_running = True
        hack()
        Startk.config(text='Stop')
    else:
        hack_running = False
        Startk.config(text='Start')



root = tk.Tk()

root.resizable(False, False)

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame = tk.Frame(root, bg='black')
frame.place(relwidth=1, relheight=1)

Tittel = tk.Message(frame, text="BOXING SIMULATOR 2 HACK", bg='black', fg='white', font=("Calibri", 14), width='190', justify='center')
Tittel.place(relwidth='1')

Startk = tk.Button(frame, text='Start', font=("Calibri", 10), command=Start_stop)
Startk.pack(side='top', pady='50')



root.mainloop()

先谢谢您的帮助:)

我希望这是足够的细节