Simulating mouse clicks in World of Warcraft with python, but in background to specific coordinates

573 Views Asked by At

I am trying to simulate right mouse click to WoW, I managed to send keyboard presses and the mouse click to minimized window without activating it, but for the mouse clicks it just doesn't click where I want. If I do it in activated window the cursors position is 959, 519, but if I try it with

win32api.PostMessage(hwndMain, win32con.WM_RBUTTONDOWN, 0, 34014143) 

or

win32api.PostMessage(hwndMain, win32con.WM_RBUTTONDOWN, 959, 519)

it just wont work, it does click only within WoW but it still uses the cursor real position.

I am fairly new to Python, would u know a solution?

Below whole code I use

from pynput.keyboard import Key, Controller
import time
import random
from tkinter import *
import win32api
import win32con
from window_list import window_list

keyboard = Controller()
looping = 0
hwndMain = window_list["World of Warcraft"]

time.sleep(1)

def inter_attack():
    for key in range(6):    
        time.sleep(random.uniform(0.5, 0.7))
        
        win32api.PostMessage(hwndMain, win32con.WM_KEYDOWN, 0x46, 0) 
        win32api.PostMessage(hwndMain, win32con.WM_KEYUP, 0x46, 0)

        time.sleep(random.uniform(2.5, 3.5))
        
        win32api.PostMessage(hwndMain, win32con.WM_KEYDOWN, 0x32, 0) 
        win32api.PostMessage(hwndMain, win32con.WM_KEYUP, 0x32, 0)
       
#infinite loop
while looping == 0:     
    #time.sleep(1) 
    #win32api.SetCursorPos((959, 519)) # just to find if coordinates work and where to put the mouse click
    time.sleep(0.1)
   
    win32api.PostMessage(hwndMain, win32con.WM_RBUTTONDOWN, 0, 34014143)  
    win32api.PostMessage(hwndMain, win32con.WM_RBUTTONUP, 0, 34014143)

    time.sleep(1.5)   
    
    win32api.PostMessage(hwndMain, win32con.WM_KEYDOWN, 0x35, 0) 
    win32api.PostMessage(hwndMain, win32con.WM_KEYUP, 0x35, 0)

    inter_attack()
1

There are 1 best solutions below

2
Hchap On

I would recommend using the keyboard and mouse modules - I'm sure win32api is more powerful, but keyboard and mouse are simpler.

pip install keyboard
pip install mouse

You can check out the docs for more info, but you can do keyboard.send('w') for example.

For mouse, it's mouse.click('right').

The reason you can't get the mouse to click at the right coordinates is because, for some reason, simulating mouse movements goes for position on the screen. Therefore, it's not a drag since the game centers the mouse. (It doesn't have a movement input, it just teleports the mouse.)