Unable to click inside of a game window with pyautogui/win32api/pydirectinput - python

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.

Related

mouse drag and toggle with pynput

I'm trying to make it so when the left mouse button is held down the mouse moves down. I was able to get it to work but I cant seem to get a button to toggle on and off. (Note: Mouse must keep moving down until the left button on mouse is released)
I think I'm having a problem with the Global variables and my loops. I have been going at it for hours and cant find a solution.
I would like to implement the F8 key to Toggle on and off. If not possible to use the same key to toggle F7 to Turn on and F8 to turn off.
Here is my current code:
from pynput import mouse
from pynput.mouse import Button, Controller
import threading
import win32api, win32con
import time
running = False
print(running)
def process():
print('start')
while running:
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -1, 2)
time.sleep(20 / 950)
print('stop')
def on_click(x, y, button, pressed):
global running # to assing value to global variable (instead of local variable)
if button == mouse.Button.left:
if pressed:
if not running:
running = True
threading.Thread(target=process).start()
else:
running = False
with mouse.Listener(on_click=on_click) as listener:
listener.join()

Custom trigger to execute failsafe in pyautogui

The default trigger of failsafe pyautogui is drag mouse to up-left corner. if i do that then the program will stop. How to change trigger, instead drag mouse i want set the trigger with keyboard input. for example if i press P key then the program will stop (the failsafe executed)
There isnt a way using pyautogui but it has been done using pynput. here is an example (pay attention to def keypress specifiaclly)
from random import random
from threading import Thread
from time import sleep
from pynput.mouse import Controller, Button
from pynput.keyboard import KeyCode, Listener
import pyautogui
delay = float(pyautogui.confirm(text='Choose Clicking Speed Per Second', title='Spook AutoClicker', buttons=['0.5','0.001']))
mouse = Controller()
class AutoClicker(Thread):
clicking = False
def run(self):
while True:
if AutoClicker.clicking:
mouse.click(Button.left)
sleep(delay)
def keypress(key):
if key == KeyCode(char="p"):
AutoClicker.clicking = not AutoClicker.clicking
AutoClicker().start()
with Listener(on_press=keypress) as listener:
listener.join()

Python tkinter askopenfilename() not opening and responding

Python tkinter askopenfilename() is not opening and responding. I searched and tried everything but it didn't work.
What's wrong?
Code:
from tkinter.filedialog import askopenfilenames
import pygame, keyboard, time
import tkinter as tk
from tkinter.filedialog import askopenfilename
from pygame import key
pygame.init()
pygame.mixer.init()
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 450
BLACK = (0, 0, 0)
pyscreen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE)
def play():
tkscreen = tk.Tk()
tkscreen.iconbitmap("void.ico")
tkscreen.title("Select music file...")
print("b")
global music
tkscreen.update()
music = askopenfilename() # Not responding!!
tkscreen.update()
print("c")
try:
pygame.mixer.music.load(music)
print("d")
pygame.mixer.music.play()
print("e")
except:
print("e-1")
print(f"{music} is not a music file. ")
print("a")
play()
pausecooldown = True
paused = False
while True:
if keyboard.is_pressed("f9"):
if keyboard.is_pressed("space") and pausecooldown:
if paused:
pygame.mixer.music.unpause()
paused = False
time.sleep(0.1)
pausecooldown = False
elif not paused:
pygame.mixer.music.pause()
paused = True
time.sleep(0.1)
pausecooldown = False
pausecooldown = True
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
Version: Python 3.9.1
OS: Windows 10 Home
It prints 'b', and opens tkinter and pygame window, but 'Select file' window is not opening, and tkinter and pygame window is not responding.
I found an answer:
Every tkinter instance requires a mainloop() method to be run. This method essentially
monitors what's happening with the app and reacts to user inputs. Without it, Tkinter
just doesn't work. You can add tkscreen.mainloop() just before the first try block and
it should hopefully solve issues.
from above comment (by pavel)
I know and can understand your problem.
I was doing a Tkinter project once. Well there is no better solution to this. BUT I can help you with this anyways.
I have two solutions which I would like to share it with you.
1. Using Debugger
I used this solution for the program when I was just editing my program. If you use an IDE like VS Code or Pycharm. Run the code in Debugger mode. The program would work and respond.
It will work as wanted.
2. Using .exe file of the project
Use this link to understand how to do that. It will just make your python program run as an application and as per me when I did this, the problem you mentioned didn't come up.
https://datatofish.com/executable-pyinstaller/
If you still get any problem, please comment below and ask anything you want.

Bring window to focus with python periodically

import win32gui
import time
def windowEnumerationHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
if __name__ == "__main__":
top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
for i in top_windows:
print(i)
if "zoom" in i[1].lower():
print(i, 'is found')
while True:
win32gui.ShowWindow(i[0],5)
win32gui.SetForegroundWindow(i[0])
time.sleep(1)
I've heard that zoom monitors whether the window is not in focus for more than 30 seconds, so I've been working on a way to repetitively throw it to the front while I work on other projects. The problem is the code raises an exception
0, 'SetForegroundWindow', 'No error message is available'
and the window just flashes yellow. Same problem with chrome as well. Would appreciate some help here :)
I had the same problem while I was trying to SetForegroundWindow(hwnd). The icon on the taskbar was just flashing, but the program stayed in the background. As you can read here:
https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setforegroundwindow?redirectedfrom=MSDN
"An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user."
For me helped:
import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(hwnd)

Clicking on-screen keyboard with pyautogui

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.

Categories

Resources