I'm trying to build create a script that does a few action inside an DirectX game.
I've got everything working exept for moving the mouse.
Is there any module avalable that can move the mouse, for windows (python 3)
Thanks!
I used pynput once in Python 2.7. I just checked under Python 3.7 and it moves the cursor alright. Sample code from linked source:
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position
mouse.move(5, -5)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)
Edit: Just successfully tested in a DirectX game.
Use a freeware App called sikulix. It will do wonders for you
Related
I searched for the answer but nothing comes up. I want to make a script for the game, but i also want to make my mouse movement more unpredictable so i want to record my own mouse movement. Or record macro mouse movement in another program and run it via python maybe? How can i do it?
There is a library called pyautogui which you can download using the pip command in your terminal pip install PyAutoGUI
import pyautogui
a = pyautogui.position()
print(a)
>>> Point(x=1290, y=342)
you can run pyautoqui.position() anytime you want to get the mouse coordinates on the screen or create a loop which repeatedly gets the position of the mouse. In the loop below I added a sleep(0.5) so that it waits a small second jsut to show taht it works since without the sleep function it would run so fast I didn't have time to move my mouse and show that it worked.
import pyautogui
from time import sleep
for i in range(10):
print(pyautogui.position())
sleep(0.5)
>>>Point(x=689, y=540)
>>>Point(x=820, y=503)
>>>Point(x=1122, y=698)
>>>Point(x=528, y=608)
>>>Point(x=1316, y=301)
>>>Point(x=937, y=288)
>>>Point(x=734, y=529)
>>>Point(x=1055, y=310)
>>>Point(x=584, y=763)
>>>Point(x=740, y=298)
To move the mouse use:
>>> pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
>>> pyautogui.moveTo(None, 500) # moves mouse to X of 100, Y of 500.
>>> pyautogui.moveTo(600, None) # moves mouse to X of 600, Y of 500.
Here none means to keep the x or y the same.
To move gradually over time use this:
>>> pyautogui.moveTo(100, 200, 2) # moves mouse to X of 100, Y of 200 over 2 seconds
use moveTo to move to a coordinate, you can change moveTo to move and set the pixels to how many pixels you want to move from the current location.
I wanted to create an autoclicker that would "remember" current position of my mouse pointer, move to specific location on the desktop, perform a double click and then come back to where it was and will do this randomly every 1 to 4 seconds. This way I wanted to achieve an autoclick in a specific place and more or less be able to use my mouse to browse other stuff.
What I want to click is in a different window, it is a program that I leave open visible on one half of my desktop and on the other half I want to do other things. The problem is that auto clicker does not make the program an active window and the click does not work.
import pyautogui
import threading
import random
def makro():
z = random.randint(1,4) #timer set to random value between 1 and 4 seconds
(x, y) = pyautogui.position() #remember current position of mouse pointer
threading.Timer(z, makro).start()
pyautogui.doubleClick(1516, 141) #perform a double click in this location (this clicks do not make the window active and clicks do not work)
pyautogui.moveTo(x, y) #come back to original mouse pointer location
makro()
Thank you for help
I think adding
pyautogui.click(1516, 141) before pyautogui.doubleClick(1516, 141) could activate the window.
I have a pygame window embedded in a a frame in tkinter. In another frame I have a button which calls the following function when clicked:
def setStart():
global start
# set start position
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
start = event.pos
print("start",start)
break
I am intending for the program to print the position of the place where the user clicked on the pygame surface after the button is clicked. However on the first click of the button and the following click of the pygame surface there is no output. It is on the second click of the button before the corresponding second click on the pygame surface that python prints out an output like :
('start', (166, 115))
How can I get it to give me a result right after the click on the pygame surface? I had the same problem when I had two seperate tkinter and pygame windows so the embedding of pygame into tkinter is unlikely to be the cause of the problem.
EDIT: after further testing it appears that if the button is pressed and then the pygame surface is clicked on multiple times, upon a second click of the button the coordinates of all of these clicks are printed out as a batch.
In the most basic form here's how you do it in pygame:
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP: # or MOUSEBUTTONDOWN depending on what you want.
print(event.pos)
elif event.type == pygame.QUIT:
quit()
pygame.display.update()
More information on how to handle pygame events can be found in the docs.
And here's how you do it in tkinter:
try:
import tkinter as tk # Python 3
except ImportError:
import Tkinter as tk # Python 2
root = tk.Tk()
def print_pos(event):
print(event.x, event.y)
root.bind("<Button-1>", print_pos)
root.mainloop()
More information on tkinter events can be found in effbots documentation.
I would suggest not putting break in an event loop in pygame. Doing so makes all other events go un-handled, meaning that it's possible for the program to not respond to certain events.
Your "EDIT: [...]" is unfortunately wrong, given the code you've given us. I had no problem with the code; it printed the position of where I released the mouse button and always printed just one position. So there have to be a logical error somewhere else in your code.
First I have to say that I don't know anything about pygame. However, if you are already using Tkinter, I could help you maybe: I would define a new function (let's call it mouse_click). In the button-click-function I would bind the new function to the game's surface. In the new function I print out the current mouse position:
def button_click(self):
game_surface.bind("<Button-1>", self.mouse_click)
def mouse_click(self, event):
print "X:", event.x
print "Y:", event.y
I hope this is helpful. Please notice that you should modify this code to make it work in your program (using the correct widget names and so on).
By the way, "Button-1" is the event identifier of the left mouse button.
I have the very simple program as followed: Python 3.3.2
from graphics import *
def main():
print("Click your mouse to exit")
win = GraphWin()
# Draw graphics here
win.getMouse()
win.close()
main()
This is obviously a very simple program. All I am trying to do is create a function that opens up the graphics window, draws the graphics, and then tells the user in the shell to "click the mouse to exit". However, the problem is that I can't get the print statement to show up in the shell until after I click the mouse and the graphics window closes. So, is there a way to be able to print a statement while using getMouse()?
I created a small Python script using win32api to use on the popular game Cookie Clicker (a game where you have to click on a Big Cookie to gain points) just for fun. It has a function called "auto_clicker" that do just that: keeps clicking on the screen on the point the user defined. This is the script:
# -*- coding: utf-8 -*-
import win32con
import win32api
def clicker(x,y):
"""Clicks on given position x,y
Input:
x -- Horizontal position in pixels, starts from top-left position
y -- Vertical position in pixels, start from top-left position
"""
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def auto_clicker(x = -1,y = -1):
"""Keep clicking on position x,y. If no input is given, gets from actual
mouse position.
"""
if x == -1 | y == -1:
x,y = win32api.GetCursorPos()
while True:
clicker(x,y)
It works nicely, but I want to make some improvements:
How can I get the cursor position only when the user clicks instead when the function is called? I would prefer to not add another module
since win32api seems to contain everything I needed. Tried this
method without success.
How can I detect a keypress like "Escape", so I can exit from my program without the ugly hack I am using now (Ctrl+Alt+Del seems to give SetCursorPos denied access, so Python throws a error and exit the program).
Can I make this program portable? Seems like I can do using Tkinter and generating a invisible Tk window, but I tried to write something without success.
I don't think with win32api you can listen to clicks you can just generate them (not sure though). However, try using pyHook, it's a simple api easy to use and can be found here http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=Main_Page. With pyhook you can create a listener to listen to a mouse event and upon a mouse click you can do whatever you want, the example in the link shows you how. As for key press, you can use the same api for that too, also an example is provided, good luck!
use pynput . It can control mouse, keyboard, etc.
examples:
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position
mouse.move(5, -5)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)