so I was trying to make an auto clicker, the other day and for some reason, when I try to activate it with the toggle_key ("t") the program doesn't start. I was following this tutorial https://www.youtube.com/watch?v=4hdK9Gey76E and it seems to work in the video. Can anyone explain why the program doesn't start when I turn it on?
import time
import threading
from pynput.mouse import Controller, Button
from pynput.keyboard import Listener, KeyCode
TOGGLE_KEY = KeyCode(char="t")
clicking = False
mouse = Controller()
def clicker():
while True:
if clicking:
mouse.click(Button.left, 1)
time.sleep(0.001)
def toggle_event(key):
if key == TOGGLE_KEY:
global clicking
clicking = not clicking
click_thread = threading.Thread(target=clicker())
click_thread.start()
with Listener(on_press=toggle_event) as Listener:
Listener.join()
Related
I'm trying to make it so when the left mouse button is held down the mouse moves down. I was able to get it to work but I cant seem to get a button to toggle on and off. (Note: Mouse must keep moving down until the left button on mouse is released)
I think I'm having a problem with the Global variables and my loops. I have been going at it for hours and cant find a solution.
I would like to implement the F8 key to Toggle on and off. If not possible to use the same key to toggle F7 to Turn on and F8 to turn off.
Here is my current code:
from pynput import mouse
from pynput.mouse import Button, Controller
import threading
import win32api, win32con
import time
running = False
print(running)
def process():
print('start')
while running:
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -1, 2)
time.sleep(20 / 950)
print('stop')
def on_click(x, y, button, pressed):
global running # to assing value to global variable (instead of local variable)
if button == mouse.Button.left:
if pressed:
if not running:
running = True
threading.Thread(target=process).start()
else:
running = False
with mouse.Listener(on_click=on_click) as listener:
listener.join()
The default trigger of failsafe pyautogui is drag mouse to up-left corner. if i do that then the program will stop. How to change trigger, instead drag mouse i want set the trigger with keyboard input. for example if i press P key then the program will stop (the failsafe executed)
There isnt a way using pyautogui but it has been done using pynput. here is an example (pay attention to def keypress specifiaclly)
from random import random
from threading import Thread
from time import sleep
from pynput.mouse import Controller, Button
from pynput.keyboard import KeyCode, Listener
import pyautogui
delay = float(pyautogui.confirm(text='Choose Clicking Speed Per Second', title='Spook AutoClicker', buttons=['0.5','0.001']))
mouse = Controller()
class AutoClicker(Thread):
clicking = False
def run(self):
while True:
if AutoClicker.clicking:
mouse.click(Button.left)
sleep(delay)
def keypress(key):
if key == KeyCode(char="p"):
AutoClicker.clicking = not AutoClicker.clicking
AutoClicker().start()
with Listener(on_press=keypress) as listener:
listener.join()
The while loop returns true even when I have let go of the left mouse button which would make pressed = false. I don't know how to break out of the loop to update the pressed value.
from pynput import keyboard
from pynput import mouse
from pynput.mouse import Button, Controller
control = Controller()
def on_click(x, y, button, pressed):
if button == mouse.Button.left:
while pressed == True:
print(pressed)
with mouse.Listener(
on_click=on_click) as listener:
listener.join()
Is there any way to update the loop so it knows when pressed = false.
If you really have to run some loop then you have to do it in separted thread because if you run it in on_click then you block listener and it can't run another on_click
on_click should start loop in thread and use global variable to control when it should stop.
from pynput import mouse
from pynput.mouse import Button, Controller
import threading
control = Controller()
running = False
def process():
print('start')
count = 0
while running:
print(count)
count += 1
print('stop')
def on_click(x, y, button, pressed):
global running # to assing value to global variable (instead of local variable)
if button == mouse.Button.left:
if pressed:
if not running: # to run only one `process`
running = True
threading.Thread(target=process).start()
else:
running = False
with mouse.Listener(on_click=on_click) as listener:
listener.join()
I want to find mouse click and key press is occur or not between start time and end time (starttime:9:30 and endtime:10:30) using Python script.
Python code is here:
from pynput.mouse import Listener
from pynput.keyboard import Key, Listener
def on_click(x, y, button, pressed):
if pressed:
print("Mouse clicked.")
def on_press(key):
print("key is pressed")
with Listener(on_click=on_click,on_press=on_press) as listener:
listener.join()
with this I am able to get the mouse click and key press, but I don't have idea for time-interval.
First of all your code didn't work for me.
I had to make some changes in order to test it.
The problem in my case were the events mouse and keyboard at the same time.
Here I post my changed code:
from pynput.keyboard import Key, Listener
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
if pressed:
print("Mouse clicked.")
def on_press(key):
print("key is pressed")
from pynput import keyboard
key_listener = keyboard.Listener(on_press=on_press)
key_listener.start()
with Listener(on_click=on_click) as listener:
listener.join()
(Source: Using Mouse and Keyboard Listeners Together in Python)
If you want to count seconds, minutes and so on, you can use time like #Ujjwal Dash said.
The Mouse events will be noticed if its between 1 and 10 seconds since the script was started.
import time
from pynput.keyboard import Key, Listener
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
delta_time = (int(time.time()-start_time))
if delta_time >=1 and delta_time <=10:
if pressed:
print("Mouse clicked.")
def on_press(key):
delta_time = (int(time.time()-start_time))
print(delta_time)
print("key is pressed")
start_time = time.time()
from pynput import keyboard
key_listener = keyboard.Listener(on_press=on_press)
key_listener.start()
with Listener(on_click=on_click) as listener:
listener.join()
delta_time ... time in seconds since script start
If you want it to listen to a specific time of the day, you can work with the unix time and convert it with the time module.
In this code the mouse will be noticed if it's between 7:00 and 10:00 localtime hours.
import time
from pynput.keyboard import Key, Listener
from pynput.mouse import Listener
def check_time():
t = time.localtime(time.time())
if t.tm_hour<= 10 and t.tm_hour>=7:
return True
else:
return False
def on_click(x, y, button, pressed):
if check_time():
if pressed:
print("Mouse clicked.")
else:
pass
def on_press(key):
print("key is pressed")
from pynput import keyboard
key_listener = keyboard.Listener(on_press=on_press)
key_listener.start()
with Listener(on_click=on_click) as listener:
listener.join()
I don't know much about it, but I think you could do use the function time from the module time. You can bind start = time.time() to your start button and end = time.time() to your end button. Then, for each interval use i = end - start.
I want to use mouse listener in Pynput Module to monitor the click event. Ant according to the event status, it will trigger one function. But it seems something wrong with the event status.
from pynput.keyboard import Controller, Key, Listener
from pynput import mouse
from pynput import keyboard
import pythoncom
from ctypes import *
flag = False
def on_right_click(x, y, button, pressed):
global flag
# boolFlag = False
button_name = ''
if button == mouse.Button.right:
user32 = windll.LoadLibrary('user32.dll')
if flag == True:
user32.BlockInput(False)
flag = False
else:
user32.BlockInput(True)
flag = True
print(flag)
button_name = 'Right Button'
elif button == mouse.Button.left:
button_name = 'Left Button'
if pressed:
print('{0} is pressed at {1}, {2}'.format(button_name, x, y))
if not pressed:
return False
while True:
with mouse.Listener(on_click=on_right_click) as listener:
listener.join()
Normally, Flag will change once and it will trigger the BlockInput function, but below is the real results:
True
Right Button is pressed at 3478, 303
False
So, why the flag is changed twice?
System creates two events
one when you start pressing button - offten called "press"
one when you stop pressing button - offten called "release".
In other programs "click" is run only with one events (I'm not sure but it can be "release") but here listener runs the same function for both events. So finally you can see two messages.
But for keyboard's listener uses separated method for press and release.
Both mouse's events are used to drag/move elements on screen.