I am developing a bot to click the on-screen keyboard with pyautogui
The following is the code I am currently using to click 'a' on the keyboard.
import pyautogui
osk_filepath = os.path.abspath("assets")
osk_icon = pyautogui.locateCenterOnScreen(os.path.join(osk_filepath, "OSK_ICON.png"))
if not osk_icon:
sys.exit("Unable to detect On-Screen Keyboard")
OSK_LOCATION = (osk_icon[0] - 25, osk_icon[1], 1000, 500)
a = pyautogui.locateCenterOnScreen(os.path.join(osk_filepath, "a.png"), region=OSK_LOCATION, grayscale=True)
pyautogui.click(a)
It moves the mouse to the position of the 'a' key but does not press down for it to output an 'a'.
This issue can be solved by running the IDE as admin.
Related
Trying to locate the Spotify pause button on my screen using pyautogui just to develop my python knowledge. I run the script below and a screenshot of the pause button is saved, but the mouse moves to the bottom right of the screen, miles away from where the button is.
import pyautogui
input("Move to target button") #move mouse to pause button
target = pyautogui.position()
local = pyautogui.locateCenterOnScreen("spotify.png", confidence=0.9, grayscale=False)#spotify.png is a screenshot of the pause button
pyautogui.moveTo(local)
print(f"Found: {local}")
print(f"Target: {target}")
#sloppy but wanted to ensure the xy coordinates were correct
x = list(local)
x.append(100)
x.append(100)
im = pyautogui.screenshot('test.png', region=x)
Example output:
Move to target button
Found: Point(x=1487, y=780)
Target: Point(x=746, y=392)
Screensize is 1440 x 900
There is no matching image at the coordinates the mouse is directed to, but from the screenshots it seems one has been found? How do I fix this so my mouse goes to the pause button, not the bottom far right of the screen?
import pyautogui
import time
while True:
time.sleep(1) #prevents lag and waits for new image
spotify = pyautogui.locateOnScreen('spotify.PNG', confidence=.7,grayscale=False) #image location
if spotify: #if the image is found
print('found image clicking...')
pyautogui.click(spotify)
I can click on the window, but it doesn't move my character, or interact with anything in game. I've tried moving the mouse around, i've tried doing keyboard inputs, full screen, windowed, etc. I've also tried using screenshots with pyautogui, but no luck. The game i'm trying to use it with was initially released in 2000. Non coding wise i've tried running it as admin, running in windows xp sp 2-3 compatibility mode, disabling desktop composition, etc.
win32api code:
import win32api, win32con
import time
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
# click(573, 841)
# time.sleep(1)
# click(289, 342)
# time.sleep(1)
time.sleep(5)
click(319, 399)
x = win32api.GetCursorPos()
print(x)
error:
win32api.SetCursorPos((x,y)) pywintypes.error: (0, 'SetCursorPos', 'No error message is available')
pyautogui/pydirect input:
import pyautogui
import pydirectinput as p
import time
icon = pyautogui.locateCenterOnScreen('./icon.png', confidence=0.9)
p.click(icon[0], icon[1])
time.sleep(2)
p.press('enter')
this code doesn't throw an error, it completes normally without actually clicking in the game window
First, make sure you are running your script as admin, sometimes if you don't Windows will prevent mouse movement.
Also, try doing this:
def click(x,y):
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
time.sleep(.01)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0
You have to give it a little bit of time to click or else Python will do it too quickly and the game won't register it.
I am creating a pyautogui automation that connect to a W3270 terminal (really old :))
this terminal is expecting pause key to be pressed,
Apart pyautogui, I also tried Keyboard library, but i am unable to send pause
import pyautogui
import keyboard
import constants as const
locateOnScreen(const.IPAIRE_TERMINAL_CONNECTED)
command = '/FOR SIGNON'
pause = '\u0019'
pyautogui.write(command)
time.sleep(1)
keyboard.send('pause')
Am I suppose to use keyboard to simulate 'pause' button?
I found a solution using pynput
import pyautogui
import constants as const
from pynput.keyboard import Key, Controller
locateOnScreen(const.IPAIRE_TERMINAL_CONNECTED)
command = '/FOR SIGNON'
pyautogui.write(command)
time.sleep(1)
keyboard = Controller()
keyboard.press(Key.pause)
keyboard.release(Key.pause)
I have a button on a dialog box which when it is clicked, pops up a confirm popup window.
Whenever I perform a click on that button using pywinauto, the click does occur in the UI, however the click() function does not return without throwing this error:
COMError: (-2147220991, 'An event was unable to invoke any of the subscribers', (None, None, None, 0, None))
The code to click the button is very simple:
readerDlg = mainDlg.window(title=READER_WINDOW_TITLE)
readerDlg.Skip.click()
Connection snippet:
sw = Application(backend='uia').connect(title=APPLICATION_TITLE)
mainDlg = sw.window(title=MAIN_WINDOW_TITLE)
Switching the backend from uia to win32 fixed my issue:
sw = Application(backend='win32').connect(title=APPLICATION_TITLE)
It also made the automation much faster (from ~5 sec/op to < 1 sec/op.
I am new in Python and I want to write a script that listens to the left, right or middle(mouse-wheel) button click event. When the left or right mouse button is clicked it should do nothing. In other words, the script should block the left and right mouse clicks.
But when the middle mouse button is clicked the script should press the escape button.
I already found a library for listening to mouse events: Pynput. The problem is that the script have to run on a Windows XP machine so I have to use Python 3.4.4 and I cannot find a version of Pynput for this Python version.
My question now: How can I listen to mouse clicks and send keyboard events in Python 3.4.4 for WindowsXP?
UPDATE:
Now Pynput is installed correctly and is working. I wrote this:
from pynput import mouse
from pynput.keyboard import Key, Controller
keyboard = Controller()
def on_click(x, y, button, pressed):
if button == mouse.Button.middle:
keyboard.press(Key.esc)
keyboard.release(Key.esc)
#after pressing the esc-key: stopPropagation(middle mouse click should not be forwarded to windows)
if button == mouse.Button.left:
print("left")
#stopPropagation(left mouse click should not be forwarded to windows)
if button == mouse.Button.right:
print("right")
#stopPropagation(right mouse click should not be forwarded to windows)
# Collect events until released
with mouse.Listener(
on_click=on_click) as listener:
listener.join()
How can I accomplish what I wrote in the # comments? Is it even possible to do such things like prevent the mouse click in windows with Python? Also what I don't understand is when I run the program and I want to quit it (with ctrl+c) I have to press it like 100 times and then it shows this:
Traceback (most recent call last):
File "C:\Users\path\hello.py", line 22, in <module>
File "C:\Python34\lib\site-packages\pynput-1.3.10-py3.6.egg\pynput\_util\__init__.py", line 178, in join
File "C:\Python34\lib\threading.py", line 1060, in join
self._wait_for_tstate_lock()
File "C:\Python34\lib\threading.py", line 1076, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
Why? And how can I solve this?