我正在为某个游戏编程,并且我想在游戏全屏运行时有一个热键来启动/停止脚本,我希望该热键为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()
先谢谢您的帮助:)
我希望这是足够的细节
Pynput has an easy class which provide hotkey function called
GlobalHotKeys
.Reference here.不幸的是,如果只有python,我想如果要使其在游戏中运行就不能做更多的事情。
通常,游戏中还会有键盘侦听器线程。当您的python脚本与您的游戏一起使用时,会引起冲突。并且您的python脚本无法正常工作(并且游戏将始终采取某些措施来防止作弊。)
As far as I know,
AutoHotkey
script could work in game(at least it worked for me in the past).AutoHotkey Official document.尝试使用Pynput:
For keyboard combinations see this github issue. Hope that's helpful!
试试这个在我的脚本中对我有用的: