Reputation:
I am making a hack for a game, and I want to start/stop the script with the F7 hotkey while the game is running at full screen. I've tried to use root.bind
and pynput
to do it, but none of them worked.
Here is my code:
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()
Upvotes: 2
Views: 588
Reputation: 12672
Pynput has an easy class which provide hotkey function called GlobalHotKeys
.Reference here.
Unfortunately,if there is only python,I think it couldn't do more if you want to make it work in game.
Normally, there are also keyboard listener thread in game.When your python script work with your game together,they will cause conflict.And your python script couldn't work normally.(And game will always take some measures to prevent cheating.)
As far as I know,AutoHotkey
script could work in game(at least it worked for me in the past).AutoHotkey Official document.On macOS
,refer this
Upvotes: 2
Reputation: 1390
Try using pynput:
import pynput
def run():
print('f7') # your code
def press(key):
if key == pynput.keyboard.Key.f7:
run()
pynput.keyboard.Listener(on_press=press).run()
For keyboard combinations see this github issue. Hope that's helpful!
Upvotes: 1