我正在对一个游戏进行 hack,我想在游戏全屏运行时使用 F7 热键启动/停止脚本。我尝试使用 root.bind
和 pynput
来执行此操作,但它们都不起作用。
这是我的代码:
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/