I'm trying to simulate holding down a DPad button on a controller using Python evdev.
So far I've managed to successfully press a button like so:
import os
import time
from evdev import uinput, ecodes as e, list_devices, InputDevice, ff
dev = InputDevice(str(os.path.realpath("/dev/input/by-id/usb-Sony_Interactive_Entertainment_Wireless_Controller-if03-event-joystick")))
dev.write(e.EV_ABS, e.ABS_HAT0X, 1)
dev.write(e.EV_ABS, e.ABS_HAT0X, 0)
dev.write(e.EV_SYN, 0, 0)
but haven't been able to successfully have the application I'm simulating the input for detect the button held for any amount of time. What I've tried is this (and a couple variations on this)
...
dev.write(e.EV_ABS, e.ABS_HAT0X, 2) # evdev docs say 2 is for holding
dev.write(e.EV_SYN, 0, 0)
time.sleep(2)
dev.write(e.EV_ABS, e.ABS_HAT0X, 0)
dev.write(e.EV_SYN, 0, 0)
What am I doing wrong?
Related
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'm trying to make a python program to Automate the setup of a new computer. I used pyAutoGui to click on the Install button when prompted but it wont click on the button. It detects the image "Install.png" because it breaks the while loop when Adobe prompts to install. I tested it in paint and if I change the Install.png image to something on paint I can visually see it clicks. I tried the mouse library, moving it to the coordinates and then clicking, double- tripleClick, MouseDown -> sleep -> MouseUp, just MouseDown -> MouseUp, sending enter with pyAutogui, sending enter with shell, using subprocess and some other stuff. Nothing seems to work on the popup window Adobe gives. I printed the coordinates and they come out fine (I used DisplayMouseInfo to see of the coordinates were correct) I even hard coded the coordinates but it doesnt click install. Any answers would be appreciated, here is how it looks.
import os
import shutil
import subprocess
import psutil
import pyautogui as pg
import time
global path
path = os.getcwd()
def adobe_install():
adobe_setup_path = os.path.join(path, "Adobe.exe")
adobe_cmd = str("start " + adobe_setup_path)
os.system(adobe_cmd)
while pg.locateCenterOnScreen('Install.png', confidence=0.9) == None:
wait
time.sleep(2)
clk= pg.locateCenterOnScreen('Install.png', confidence=0.9)
pg.click(clk.x, clk.y)
EDIT
I tried the following
Install is just the a screenshot of the install button and the coordinates it prints (clk.x ; clk.y) are correct
import os
import shutil
import subprocess
import pyautogui as pg
import time
import pathlib
import winreg
import win32con
import win32gui
def adobe_install():
adobe_setup_path = os.path.join(path, "Adobe.exe")
adobe_cmd = str("start " + adobe_setup_path)
os.system(adobe_cmd)
while pg.locateCenterOnScreen('Install.png', confidence=0.9) == None:
wait
results = []
top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
for i in top_windows:
if "Adobe Acrobat Reader DC (Continuous) - Setup" in i[1]:
print(i)
win32gui.ShowWindow(i[0],5)
win32gui.SetForegroundWindow(i[0])
bring_to_front(i[0])
break
time.sleep(2)
clk= pg.locateCenterOnScreen('Install.png', confidence=0.9)
print(clk.x)
print(clk.y)
pg.click(clk.x, clk.y)
pg.leftClick(x=clk.x, y=clk.y)
pg.typewrite(['enter'], interval=1)
adobe_install()
I tried clicking, left clicking, and pressing enter. I tested the in other windows like Paint and word and the clicking and they all did indeed click or send "Enter"
This is the error I recieved
File "C:\Users\***\documents\ict\software\New_comp.py", line 77, in adobe_install
bring_to_front(i[0])
File "C:\Users\***\documents\ict\software\New_comp.py", line 41, in bring_to_front
win32gui.SetWindowPos(HWND, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
pywintypes.error: (5, 'SetWindowPos', 'Access is denied.')
Try bringing the window to front & activate it. But you need to put in the window handler
import win32gui
import win32con
def bringTOfront(HWND):
win32gui.ShowWindow(HWND, win32con.SW_RESTORE)
win32gui.SetWindowPos(HWND, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
win32gui.SetWindowPos(HWND, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
win32gui.SetWindowPos(HWND, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW + win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
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'm trying to create a method to select text content of a notepad file using the following keys combination: CTRL+END then SHIFT+HOME
I have the following code to press, hold and release a sequence of keys:
import win32api
import win32con
import win32gui
import time
def keyboard_press_hold_release(*keys):
for k in keys:
win32api.keybd_event(VK_CODE[k], 0, 0, 0)
time.sleep(.05)
for k in keys:
win32api.keybd_event(VK_CODE[k], 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(.1)
and call it to combine CTRL+END and SHIFT+HOME:
keyboard_press_hold_release('ctrl', 'end')
keyboard_press_hold_release('shift', 'home')
As result the CTRL+END command is working but the SHIFT+HOME don't select the text, just move the cursor to the begin of line.
What is wrong?
You can use the keyboard module for this purpose. Here is an example:
import keyboard
import time
time.sleep(5) #You need some wait to switch to your window after executing
keyboard.press_and_release("ctrl+end")
keyboard.press_and_release("shift+home")
If Shift+Home doesn't work for you than you can use select the whole text in a single click using Ctrl+A.
keyboard.press_and_release("ctrl+a")
I am trying to get this piece of python code to work on windows:
import win32process, win32con, win32gui, win32api, time
HWND = win32gui.GetActiveWindow()
win32api.PostMessage(HWND, win32con.WM_KEYDOWN, win32con.VK_SPACE, 0)
time.sleep(6)
win32api.PostMessage(HWND, win32con.WM_KEYUP, win32con.VK_SPACE, 0)
It is supposed to simulate someone holding down the space key, but it does not work and does not provide any sort of error message. I believe I may be doing something wrong with HWND, but I am not sure if that is the case.. I would like the event to be sent to any active window that I select to choose.
Can you help?
Solved my problem by using ctypes and the user32.keybd_event method.
ctypes.windll.user32.keybd_event(hexx[key], 0, 0, 0) #Key is down
ctypes.windll.user32.keybd_event(hexx[key], 0, 0x0002, 0) #Key is up