Is pynput capped at how fast it can run? - python

For example, with pyautogui, you can allow it to go as fast as you want with pyautogui.PAUSE = 0
Is there something similar for pynput? (Allow it to run as fast as my computer lets it?)

well I think not but u could theoretically try messing with while cycle so it would just press and release the mouse button (if it wont register the clicks u could try to import time and experiment which is the minimal delay of seconds to register those clicks)
it would look like something this:
while True:
mouse.press(Button.right)
time.sleep(0.001) #experiment with this number and see which works best
mouse.release(Button.right)

Related

How to speed up while loop with PyAutoGui?

I am making a very simple spambot for Discord just for pranking my friends. But the while True: command is very slow. Is there a faster alternative?
import PIL
import pyautogui, time
time.sleep(5)
pyautogui.FAILSAFE = True
while True:
pyautogui.hotkey("command", "v")
pyautogui.press("enter")
if (pyautogui.locateOnScreen("av.png")):
(pyautogui.click(pyautogui.locateCenterOnScreen("av.png")))
From the documentation:
Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, a bug in your program could make it go out of control. It’s hard to use the mouse to close a program if the mouse cursor is moving around on its own.
As a safety feature, a fail-safe feature is enabled by default. When a PyAutoGUI function is called, if the mouse is in any of the four corners of the primary monitor, they will raise a pyautogui.FailSafeException. There is a one-tenth second delay after calling every PyAutoGUI functions to give the user time to slam the mouse into a corner to trigger the fail safe.
You can disable this failsafe by setting pyautogui.FAILSAFE = False. I HIGHLY RECOMMEND YOU DO NOT DISABLE THE FAILSAFE.
The tenth-second delay is set by the pyautogui.PAUSE setting, which is 0.1 by default. You can change this value. There is also a pyautogui.DARWIN_CATCH_UP_TIME setting which adds an additional delay on macOS after keyboard and mouse events, since the operating system appears to need a delay after PyAutoGUI issues these events. It is set to 0.01 by default, adding an additional hundredth-second delay.
Therefore, if you want to "speed up" your loop, you can reduce the pyautogui.PAUSE value. However, keep in mind, this will prevent you from having time to activate the failsafe if you need it.

How to make python script more efficient while loop

I have a simple python script that listens to my keypress and executes a command upon keypress. In my code, I have an infinite while loop which is pretty harsh on the performance of my computer. I'm wondering what's a better way to achieve the same behavior with less impact on the performance. The objective is to print a text which says
You Pressed A Key!
Every time I press a certain key, in this case, 'h', I want to be able to press h multiple times not just once hence the while loop.
import keyboard
while True:
if keyboard.is_pressed('h'):
print('You Pressed A Key!')
You may want to make the process sleep for a while using:
import time
time.sleep(number_of_seconds)

Cannot click on game window in Python

I was trying to make a bot for a game, which you can find here. You have to play games to get a tiny amount of bitcoin, like satoshis. Anyway, I as going to make a bot, but when I tried to click on the game window, it didn't do anything. Here is my code:
clicker.py
import pyautogui
while True:
x, y = pyautogui.position()
pyautogui.click(x, y, button='left')
I don't know what's going on, is it my code, or is it the website html? If it is the html, what should i change?
What may be happening is that when you run the program, your mouse clicks so fast that the site does not recognize the clicks. But anyways, how the program is supposed to stop? Your mouse will be clicking everywhere and you may not be able to close the Python window. Do something like this:
import pyautogui
from time import sleep
from threading import Thread
def click_function():
while True:
x, y = pyautogui.position()
pyautogui.click(x, y, button='left')
sleep(0.5) # an acceptable waiting time between one click and another
t = Thread(target=click_function)
t.daemon = True
t.start()
sleep(60)
# Here you put the amount of time you want the program
# to run, in seconds. This will ensure that the program
# stops sometime and you don't get stuck.
You can try adding sleep(n) at the beginning of the code. That is, you run the program, enter the site normally and point your mouse at the specific part where you want to click. Change the value of n to a considerable amount of time in seconds you can perform these tasks. If this doesn't work, you can try the Pynput module, which works similarly to PyAutoGUI.

Executing code after waiting for time in background (Python)

I'm making a personal assistant like Google Assistant or Siri, and I want the user to be able to set reminders. For example, if they type "Remind me to wash the dishes at 5pm" I would like it to pop up later and remind them. However I also want code to be able to run while waiting, so you could set multiple reminders or check the weather.
time.sleep simply stops the program. I'm pretty sure there's a way to do it with threads but I'm not sure how. Please help!
Python threading has a Timer which does exactly what you ask for:
from datetime import datetime
from threading import Timer
def create_notification(time, name):
delay = (time - datetime.now()).total_seconds()
Timer(delay, show_notification, args=[name]).start()
def show_notification(name):
print(f'notification: {name}!')
create_notification(datetime(2034, 1, 1), 'Hello future!')
One thing to watch out for is this approach creates a single thread for each event (which doesn't scale well for lots of events). This also suffers from the problem that if the user closes your program, your program crashes, computer shuts down, power loss, etc. you lose all of your notifications. If you need to handle this, then save them to a file. If you need the notifications to show up even when your program isn't running look into solutions provided by the OS like cronjobs.

HALT (python 3) a music file?

The user has limited time to solve a problem. On the program, I run a music file at the same time as a GUI (tkinter) timer, and they both finish when the time is up. Some users may finish EARLY. I want to make the music stop when they close the timer window. 'winsound' does not accept 'stop()', and I am having no success with 'winsound.SND_PURGE'. This is more complicated than I thought. I tried this:
# python 3.5
winsound.PlaySound('The Countdown.wav', winsound.SND_FILENAME)
root = Tk() # IMAGINE this is a box with a number ticking down
root.mainloop()
root.protocol("WM_DELETE_WINDOW", winsound.SND_PURGE) # nothing happens :(
Is there a way of using flags? Perhaps an alternative audio-file player? Anything?
Let me know if you do. Thanks!
winsound.SND_PURGE is just an integer constant, that happens to have the value of 64 (and furthermore, is documented as "not supported on modern Windows platforms"). What were you expecting root.protocol() to do with it? You need to pass a function to be called when the window is closed. It looks like:
lambda: winsound.PlaySound(None, 0)
would be a suitable function for stopping sound playback, although I haven't actually tested it.
I think you're also going to need to add winsound.SND_ASYNC to your original PlaySound call's flags, or your window won't even open until the sound is finished.

Categories