I have a program which makes the mouse go crazy and point randomly on different places on screen.
it works just fine, but when i go to task manager or simply click ctrl + alt + delete, it stops functioning, even when im exiting task manager.
any help?
(the program still seems to run in the proccess list, but simply doesn't do anything)
the program:
from win32api import SetCursorPos,GetSystemMetrics
from time import sleep
from random import uniform
from win32console import GetConsoleWindow
from win32gui import ShowWindow
win = GetConsoleWindow()
ShowWindow(win,0)
def click(x,y):
SetCursorPos((x,y))
while True:
click((int)(uniform(GetSystemMetrics(0),10)),(int)(uniform(GetSystemMetrics(1),10)))
sleep(0.02)
Related
I know I can use time.sleep(), but I need something that would affect whole script.It is automatic test homework and aplication buttons are clicked almost instantly. It is a bit anoying because I cant see if everything is working as supposed(still learning).
import pyautogui
import time
from pywinauto.application import Application
app = Application(backend="uia").start(r"C:\Users\User\Desktop\WPF_RentACar_3maj\WPFRentACar\bin\Debug\WPFRentACar.exe")
pyautogui.FAILSAFE = True
#app.LoginWIndow.print_control_identifiers()
dlg =app.LoginWindow
dlg.MaximizeButton.click()
dlg.MinimizeButton.click()
dlg.MaximizeButton.click()
dlg.Restore.click()
try:
dlg.Edit1.type_keys("123")
dlg.Edit2.type_keys("123")
dlg.LoginButton.click()
dlg.Button1.click()
finally:
print("Cant login with wrong credentials!")
time.sleep(2)
dlg.Edit1.type_keys("'^a{BACKSPACE}")
dlg.Edit2.type_keys("'^a{BACKSPACE}")
dlg.LoginButton1.click()
time.sleep(5)
time.sleep() does stop the whole script. Or are you using threading or python 2? Also can you tell us what you are also trying to automate.
I am trying to make a shortcut to open google when I do the function normal it works but when I try to do it in a shortcut it doesn't work.
import pyautogui as pg
import keyboard
hotkey1 = "ctrl+alt+w"
def google():
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
while True:
if keyboard.is_pressed(hotkey1):
google()
You only need a pause until pg can take over. It so happens that when you press the hotkey combo, you might accidentally hit some other keys and therefore it doesn't work as intended. This should give it enough time for the key to be depressed before the automatic process starts.
import time
def google():
time.sleep(0.25)
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
I am a new python 3.9 user who's trying to step up my coding. I'm writing a program that uses a while loop and will call functions to press keys based on user input. My code basically looks like:
from pynput.keyboard import Key, Controller
from graphics import *
import time
def wack():
M = Controller()
for i in range(3):
M.press('a')
time.sleep(1) # This guy right here is the issue
M.release('a')
def main():
click = Win.checkMouse()
while not click(click, exit_box):
click = Win.checkMouse()
if *input* == *correct string*:
wack()
I have a graphics window with an exit button and when exit is clicked, it terminates the program and closes the window. The issue is that the time.sleep(1) doesn't pause the inputs or that point in the code, it pauses the entire program suspending the while loop which is not what I want it to do.
Searching about the time function hasn't provided any clear solutions since time.sleep() seems to be the most popular pausing function in python.
asyncio is an amazing feature added in python3.7 which allows you to wait for an object or function(callled a coroutine in the context of asynchronous python) to get a result while the rest of the program executes.
asyncio has a sleep method which paused only the asynchronous function(coroutine) and lets the rest of the code in the program execute
This is how:
from pynput.keyboard import Key, Controller
from graphics import *
import time
import asyncio
#This is the asnychronous funtion
async def wack():
M = Controller()
for i in range(3):
M.press('a')
await asyncio.sleep(1)#this sleeps the function but allows the rest of the program to work
M.release('a')
def main():
click = Win.checkMouse()
while not click(click, exit_box):
click = Win.checkMouse()
if *input* == *correct string*:
asyncio.run(wack()) #calling the asynchronous funtion
Here is a tutorial on understanding asyn io for python
I tried to use this script to prevent windows screen lock. The script works for moving the mouse, but it doesn't prevent windows 10 from locking.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
x = 1
while x == 1:
pyautogui.moveRel(1)
pyautogui.moveRel(-1)
time.sleep (300)
Yes it can. But sadly not by moving mouse which I don't know why and would like to know. So, my suggestion is to use pyautogui KEYBOARD EVENTS if possible. I have solved my problems by using VOLUME-UP & VOLUME-DOWN keys. Example code is provided below:
import pyautogui
import time
while True:
pyautogui.press('volumedown')
time.sleep(1)
pyautogui.press('volumeup')
time.sleep(5)
You can use any other keys if you want.
import ctypes
# prevent
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
# set back to normal
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
Tested on python 3.9.1, win 10 64bit
I created a short script using pyautogui module with the purpose of avoid a window 10 session to lock due to user but inactivity. But it's not working.
The script is moving the cursor a little each 120 sec.
Any idea why it's not working? Sometime ago I found on web a small program that was doing exactly the same thing and worked.
Let me just clarify that the script works (moving the mouse) but the windows 10 os locks anyway.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow() #minimize current window
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE) #minimize current window
x = 1
print("Application running...")
while x == 1:
pyautogui.moveRel(1) # move mouse 1 pixels down
time.sleep (120)
#end of script