Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hotkey to restart code?
#1
Im looking to add f2 as a hotkey to go to a certain point in the code. This would allow a user to reconfigure the settings.
This is the full code:

from pynput.keyboard import Key, Controller
from pynput import keyboard
import pynput
import time
import os

COMBINATIONS = [
    {keyboard.Key.f8},
]

current = set()
keyboard = pynput.keyboard.Controller()

print('Spammer 2.1\n')

#configuration
while True:
    response = input('Enter Delay (Milliseconds): ')
    try:
        number = int(response)
        break 
    except ValueError:
        print('Invalid Input. Only Use Whole Numbers.\n')
        
delay = (number/100)

print('Delay Set to:', delay,'Seconds\n')
text = (input('Enter Message to be Sent: '))
print('Message Set to:', text,'\n')
print('Setup Complete.')
print('Press F8 to Start Spamming.')
#end configuration


def on_press(key):
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.add(key)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
            execute()
            
def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.remove(key)

def look_for_stop(key):
    if key == Key.esc:
        return False
            
def execute():
    x = 0
    countdown = ["3","2","1"]
    print('\nSpamming in:(Press ESC to Stop)')
    for i in countdown:
        time.sleep(1)
        print(i)
    time.sleep(1)
    print('\nSpamming...')
    start = time.time()
    with pynput.keyboard.Listener(on_press=look_for_stop) as listener:
        while True:
            x = x+1
            time.sleep(delay)
            keyboard.type(text)
            keyboard.press(Key.enter)
            keyboard.release(Key.enter)
            print('Messages Sent:',x)
            if not listener.running:
                print('\nSpam Stopped')
                stop = time.time()
                print('Time Elapsed:', stop-start,'Seconds')
                print('\nPress F8 to Continue Spamming.\nPress F2 to Reconfigure setup.')
                break

with pynput.keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
Reply
#2
The most straightforward way would be add a keydown event check, like you do with "enter", and when it is hit run a function that is an options menu.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to make a hotkey for text adventure game myn2018 2 1,920 Jan-06-2021, 10:39 PM
Last Post: myn2018
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,740 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  Using a button to kill and restart a script duckredbeard 3 3,247 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  PySide2 Hotkey works only once in Maya LalaGhost 0 1,755 May-19-2020, 05:05 PM
Last Post: LalaGhost
  Kernel needs to restart ErnestTBass 0 2,325 May-06-2020, 08:37 PM
Last Post: ErnestTBass
  How to restart Python after input change ozstar 3 2,322 Apr-29-2020, 03:16 AM
Last Post: ozstar
  Restart Error when using code from lesson book Kathleen57 2 2,216 Mar-13-2020, 09:18 PM
Last Post: Kathleen57
  Terminate a process when hotkey is pressed 66Gramms 0 2,210 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  While loop won't restart program :( TheDovah77 4 4,155 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  pyautogui hotkey Rolfi05 0 3,239 Dec-27-2018, 04:28 PM
Last Post: Rolfi05

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020