play defined area without delaying loop - python

from pyautogui import *
from threading import Timer
import pyautogui
import pydirectinput
import time
import keyboard
import random
import win32api, win32con
start = time.time()
def b()
time.sleep(0.30)
pydirectinput.press('b')
print("i see it")
while True:
if pyautogui.locateOnScreen('b.png', region=(1717,1127,150,150), grayscale=True, confidence=0.8):
b()
else:
print("cant see anything")
I'm still quite new to programming and even newer to python but I tried to get the script to detect an image, once it detects the image to play def b() which would press the b key after a few seconds. The only problem is, I'm trying to get the code to still search for the image while the def b() is still playing and to play it again if it detects that image again during that short time frame.

Related

Problem with keyboard library, exactly with stopping .record() in function

import keyboard
import time
import pyautogui
import mouse
def call_record():
event = keyboard.record(until='shift')
print("ended record")
def call_playrecord():
print('playing...')
keyboard.play(event)
keyboard.add_hotkey('l', call_record)
keyboard.add_hotkey('n+m', call_playrecord)
keyboard.wait('esc')
This is a code and a problem about that, is that record wont stop when i press 'shift'
(nevermind that i dont have global set on event i will make this later)
I tried to make a record and play system with hotkeys but i cant stop the record

Python: PyautoGui Mouse Click Not working neither locateScreen based on screenshot

Below code stops after printing hello and nothing happens.
Tried different option as pyautogui.leftClick(993, 578) but no luck.
In below code if I don't use subprocess but keep zoom GUI open then click is working fine.
import subprocess
import pyautogui
import time
import os
import pandas as pd
from datetime import datetime
print("hello")
subprocess.call("C:\\Users\xyz\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
time.sleep(5)
pyautogui.moveTo(993, 578, 0)
pyautogui.leftClick()
print(1)
The simple problem i see with your code is this line:
subprocess.call("C:\\Users\xyz\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
You've missed a \ before the xyz.
The following works perfectly for me on Python 3.6+:
import pyautogui
import time
import os
print("begun")
os.system(r"C:\Users\xyz\AppData\Roaming\Zoom\bin\Zoom.exe")
time.sleep(5)
pyautogui.moveTo(pyautogui.locateCenterOnScreen('my image'))
pyautogui.click()
print("dun")
If this doesn't work, try the pydirectinput module too, it's the updated version of pyautogui with much the same functions.

How to avoid lock screen under Win10 via Python code? [duplicate]

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

Python Prevent the screen saver

I need to run a code for hours, and the computer I am working with has a (forced and unchangeable) screensaver policy. (It locks after 10 minutes). What can I do to prevent it? Is there any line of code to prevent that?
This worked for me. I'll just leave it here so people can use it.
import ctypes
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) #this will prevent the screen saver or sleep.
## your code and operations
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000) #set the setting back to normal
You can prevent screen saver by moving mouse cursor on a fixed period of time, below function can handle cursor moving.
import win32api
import random
def wakeup():
win32api.SetCursorPos((random.choice(range(100)),random.choice(range(100))))
This is a coded solution that you can place in your program (also works for Mac users):
pip3 install pyautogui
https://pypi.org/project/PyAutoGUI/ (Reference)
import pyautogui
import time
def mouse_move():
while True:
pyautogui.moveTo(100, 100, duration = 1) # move the mouse
time.sleep(60) # Every 1 min
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()
Or, without the while loop, run it when required if your program is already within a while loop:
def mouse_move():
pyautogui.moveTo(50, 100, duration = 1) # move the mouse
mouse_move()

How to print out 'Live' mouse position coordinates using pyautogui?

I used lots of different source codes, and even copied and pasted but I keep getting random symbols that shift when i move my mouse over them
here is my code...
import pyautogui, time, sys
print('Press Ctrl-C to quit.')
try:
while True:
CurserPos = pyautogui.position()
print('\b' * len(CurserPos), end='\r')
sys.stdout.flush()
I will show the output as an image.
I am rather new to Python and would really appreciate some expert advice.
Thanks
Code :
import pyautogui
pyautogui.displayMousePosition()
Here is some output :
Press Ctrl-C to quit.
X: 0 Y: 1143 RGB: ( 38, 38, 38)
Here is the video where this is being demonstrated https://youtu.be/dZLyfbSQPXI?t=809
This code will print the live position of your mouse after every one second.
import pyautogui as py #Import pyautogui
import time #Import Time
while True: #Start loop
print (py.position())
time.sleep(1)
Pyautogui can programmatically control the mouse & keyboard.
More information about it can be found here https://pypi.org/project/PyAutoGUI/
Use pyautogui.displayMousePosition() instead of pyautogui.position()
If you want the coordinates of displayMousePosition stored in a variable, try this:
import pyautogui
def getMousePosition():
pyautogui.displayMousePosition()
coords = pyautogui.position()
return coords

Categories