python - 基于 Tkinter 的最小化程序中的热键

标签 python python-3.x user-interface tkinter hotkeys

我正在对一个游戏进行 hack,我想在游戏全屏运行时使用 F7 热键启动/停止脚本。我尝试使用 root.bindpynput 来执行此操作,但它们都不起作用。

这是我的代码:

hack_running = False


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)

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

root.mainloop()

最佳答案

Pynput 有一个简单的类,它提供名为 GlobalHotKeys 的热键功能。 Reference here.


不幸的是,如果只有Python,我认为如果你想让它在游戏中工作它就不能做更多的事情。

一般情况下,游戏中也会有键盘监听线程,当你的python脚本和你的游戏一起工作时,就会产生冲突,导致你的python脚本无法正常工作。(并且游戏总会采取一些措施来防止作弊) .)

据我所知,AutoHotkey 脚本可以在游戏中运行(至少它过去对我有用)。 AutoHotkey Official document .在 macOS 上,refer this

关于python - 基于 Tkinter 的最小化程序中的热键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61696254/

相关文章:

python - 在 python 中写入文件后,文件处理程序不打印

python - 求和函数在python中的使用

linux - 为任意应用程序编写 I/O 包装器 (linux)

java - 如何使用从另一个线程收到的信息更新主 UI? (当线程仍在运行时)

python - 我在索引链中的什么位置?

python - flask 装饰器 : Can't pass a parameter from URL

python 类只读属性(在日期时间)

python - 如何使用python从git存储库下载单个文件

python - Python 3 中的相对导入

c++ - 如何检测从 qtablewidget 的单元格中单击的按钮