I'm new to python and I'm working on a small autoclicker that you can turn on & off using the letter 's', however when I press it the autoclicker starts but I can't turn it off unless I use the letter 'k' which is used to stop the program completely, this is what my code looks like:
from pynput.mouse import Controller, Button
import keyboard as kb
import time
import multiprocessing
def click():
isToggled = False
mouse = Controller()
while True:
if isToggled:
mouse.click(Button.left)
time.sleep(0.01)
if kb.is_pressed('s'):
if isToggled:
isToggled = False
else:
isToggled = True
elif kb.is_pressed('k'):
break
if __name__ == '__main__':
p = multiprocessing.Process(target=click)
p.start()
p.join()
This happens because as long as
sis held down,kb.is_pressed('s')will evaluate toTrue. SoisToggledwill rapidly toggle betweenTrueandFalseuntil you release the key, and it's anyone's guess what value it will be.One way you could solve this is to basically lock
isToggledfrom being set more than once untilsis released. Something like this: