Mouse/Keyboard control scripting module.

Links to Python scripts.

Moderators: KDoiron, ChrJim, mawe, python

Mouse/Keyboard control scripting module.

Postby BlueKitties on Mon Aug 18, 2008 6:47 pm

I've searched the web high and low, and I haven't any mouse/keyboard control modules for Python. I found bits and pieces, but nothing that worked all at once. After much searching, I got a left click script, and keyboard control module. Using that managed to code left click holding / releasing, right clicks, middle clicks, and threw it all into a little wrapper.

I have several reasons for posting this here. I feel compelled to show this to someone. :lol: This is the first useful thing I've ever made in Python, even if no one cares I'd still like someone to see it. It was probably a waste of time making something instead of finding a good module, but still, it was fun.

Anyway... BlueKitties proudly presents the reinvented wheel:

Code: Select all
"""macro.py imports a SendKeys module which may be downloaded at:
http://www.rutherfurd.net/python/sendkeys/

It also defines the following functions:

click() -- calls left mouse click
hold() -- presses and holds left mouse button
release() -- releases left mouse button

rightclick() -- calls right mouse click
righthold() -- calls right mouse hold
rightrelease() -- calls right mouse release

middleclick() -- calls middle mouse click
middlehold() -- calls middle mouse hold   
middlerelease() -- calls middle mouse release


move(x,y) -- moves mouse to x/y coordinates (in pixels)

slide(x,y) -- slides mouse to x/y coodinates (in pixels)
              also supports optional speed='slow', speed='fast'

The imported SendKeys has many features, but the basics are as
follows: SendKeys("Text goes here",pause=0.5,with_spaces=True)
The first string is typed on screep with a 0.5 second pause.
with_spaces = True means to NOT ignore spaces.

SendKeys("{ENTER}",pause=0.1) ; "{ENTER}" (in curly brackets) is
not typed, but instead presses the enter button on the keyboard.

"""

from ctypes import*
from SendKeys import*
user32 = windll.user32

# START SENDINPUT TYPE DECLARATIONS
PUL = POINTER(c_ulong)
class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
             ("wScan", c_ushort),
             ("dwFlags", c_ulong),
             ("time", c_ulong),
             ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
             ("wParamL", c_short),
             ("wParamH", c_ushort)]

class MouseInput(Structure):
    _fields_ = [("dx", c_long),
             ("dy", c_long),
             ("mouseData", c_ulong),
             ("dwFlags", c_ulong),
             ("time",c_ulong),
             ("dwExtraInfo", PUL)]

class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
              ("mi", MouseInput),
              ("hi", HardwareInput)]

class Input(Structure):
    _fields_ = [("type", c_ulong),
             ("ii", Input_I)]

class POINT(Structure):
    _fields_ = [("x", c_ulong),
             ("y", c_ulong)]
# END SENDINPUT TYPE DECLARATIONS

  #  LEFTDOWN   = 0x00000002,
  #  LEFTUP     = 0x00000004,
  #  MIDDLEDOWN = 0x00000020,
  #  MIDDLEUP   = 0x00000040,
  #  MOVE       = 0x00000001,
  #  ABSOLUTE   = 0x00008000,
  #  RIGHTDOWN  = 0x00000008,
  #  RIGHTUP    = 0x00000010

MIDDLEDOWN = 0x00000020
MIDDLEUP   = 0x00000040
MOVE       = 0x00000001
ABSOLUTE   = 0x00008000
RIGHTDOWN  = 0x00000008
RIGHTUP    = 0x00000010


FInputs = Input * 2
extra = c_ulong(0)

click = Input_I()
click.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
release = Input_I()
release.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))

x = FInputs( (0, click), (0, release) )
#user32.SendInput(2, pointer(x), sizeof(x[0])) CLICK & RELEASE

x2 = FInputs( (0, click) )
#user32.SendInput(2, pointer(x2), sizeof(x2[0])) CLICK & HOLD

x3 = FInputs( (0, release) )
#user32.SendInput(2, pointer(x3), sizeof(x3[0])) RELEASE HOLD

from ctypes.wintypes import *
import time

def move(x,y):
    windll.user32.SetCursorPos(x,y)
def getpos():
    global pt
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return pt.x, pt.y
def slide(a,b,speed=0):
    while True:
        if speed == 'slow':
            time.sleep(0.005)
            Tspeed = 2
        if speed == 'fast':
            time.sleep(0.001)
            Tspeed = 5
        if speed == 0:
            time.sleep(0.001)
            Tspeed = 3
       
        x = getpos()[0]
        y = getpos()[1]
        if abs(x-a) < 5:
            if abs(y-b) < 5:
                break
           
        if a < x:
            x -= Tspeed
        if a > x:
            x += Tspeed
        if b < y:
            y -= Tspeed
        if b > y:
            y += Tspeed
        move(x,y)

def click():
    user32.SendInput(2,pointer(x),sizeof(x[0]))
def hold():
    user32.SendInput(2, pointer(x2), sizeof(x2[0]))
def release():
    user32.SendInput(2, pointer(x3), sizeof(x3[0]))
   

def rightclick():
    windll.user32.mouse_event(RIGHTDOWN,0,0,0,0)
    windll.user32.mouse_event(RIGHTUP,0,0,0,0)
def righthold():
    windll.user32.mouse_event(RIGHTDOWN,0,0,0,0)
def rightrelease():
    windll.user32.mouse_event(RIGHTUP,0,0,0,0)

def middleclick():
    windll.user32.mouse_event(MIDDLEDOWN,0,0,0,0)
    windll.user32.mouse_event(MIDDLEUP,0,0,0,0)
def middledown():
    windll.user32.mouse_event(MIDDLEDOWN,0,0,0,0)
def middleup():
    windll.user32.mouse_event(MIDDLEUP,0,0,0,0)

def move(x,y):
    user32.SetCursorPos(x,y)


The module "macro" imports a third party module called SendKeys. I thought about throwing the SendKeys source code in, but I decided it'd be better to let the module simply import it. And of course, since it doesn't install itself you have to save it in your Lib folder or put it in your PYTHONPATH settings to import from anywhere (if you so choose.)
'les charmes enchanteux de cette sublime science ne se decelent dans toute leur beaute qu'a ceux qui ont le courage de l'approfondir.'
User avatar
BlueKitties
Python Heavy Programmer
Python Heavy Programmer
 
Posts: 242
Joined: Mon Jul 21, 2008 12:09 pm
Location: Jacksonville, TX (USA)

Re: Mouse/Keyboard control scripting module.

Postby JThundley on Sun Mar 22, 2009 11:16 am

Very useful module, thanks for sharing!
JThundley
Python Fan
Python Fan
 
Posts: 2
Joined: Sun Mar 22, 2009 11:15 am

Re: Mouse/Keyboard control scripting module.

Postby EneUran on Sun Mar 22, 2009 4:08 pm

Very nice work!
Call me for my phone number.
User avatar
EneUran
Python Super User
Python Super User
 
Posts: 199
Joined: Thu Mar 22, 2007 3:51 pm
Location: Burbank

Re: Mouse/Keyboard control scripting module.

Postby JThundley on Sun Apr 05, 2009 12:23 pm

I made a few minor changes to your module:

I fixed the middle mouse button function names: You described them as middlehold() and middlerelease() which is in line with the rest of the functions, but the actual function names were middleup() and middledown()

I documented getpos() in the module docstring.

You had move() defined twice, I got rid of one.

You defined
user32 = windll.user32
but then only used it in a few places, so I got rid of it.

The next is to add the line:
__all__ = ['click', 'hold', 'release', 'rightclick', 'righthold', 'rightrelease', 'middleclick', 'middlehold', 'middlerelease', 'move', 'slide', 'getpos']
This defines the names that should be imported from your module. So if you do:
from mousemacro.py import *
only the functions listed in __all__ are imported, not all the windows and ctypes stuff.

The next thing I did was get rid of SendKeys. I love SendKeys and used it before I found your module, but I don't see the point in going through your module to use it, I think it's cleaner to keep them separate.

The last thing I did was group all the imports together at the top and space out some functions.

Code: Select all
"""mousemacro.py defines the following functions:

click() -- calls left mouse click
hold() -- presses and holds left mouse button
release() -- releases left mouse button

rightclick() -- calls right mouse click
righthold() -- calls right mouse hold
rightrelease() -- calls right mouse release

middleclick() -- calls middle mouse click
middlehold() -- calls middle mouse hold
middlerelease() -- calls middle mouse release

move(x,y) -- moves mouse to x/y coordinates (in pixels)
getpos() -- returns mouse x/y coordinates (in pixels)
slide(x,y) -- slides mouse to x/y coodinates (in pixels)
              also supports optional speed='slow', speed='fast'
"""

from ctypes import*
from ctypes.wintypes import *
from time import sleep

__all__ = ['click', 'hold', 'release', 'rightclick', 'righthold', 'rightrelease', 'middleclick', 'middlehold', 'middlerelease', 'move', 'slide', 'getpos']

# START SENDINPUT TYPE DECLARATIONS
PUL = POINTER(c_ulong)

class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
             ("wScan", c_ushort),
             ("dwFlags", c_ulong),
             ("time", c_ulong),
             ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
             ("wParamL", c_short),
             ("wParamH", c_ushort)]

class MouseInput(Structure):
    _fields_ = [("dx", c_long),
             ("dy", c_long),
             ("mouseData", c_ulong),
             ("dwFlags", c_ulong),
             ("time",c_ulong),
             ("dwExtraInfo", PUL)]

class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
              ("mi", MouseInput),
              ("hi", HardwareInput)]

class Input(Structure):
    _fields_ = [("type", c_ulong),
             ("ii", Input_I)]

class POINT(Structure):
    _fields_ = [("x", c_ulong),
             ("y", c_ulong)]
# END SENDINPUT TYPE DECLARATIONS

  #  LEFTDOWN   = 0x00000002,
  #  LEFTUP     = 0x00000004,
  #  MIDDLEDOWN = 0x00000020,
  #  MIDDLEUP   = 0x00000040,
  #  MOVE       = 0x00000001,
  #  ABSOLUTE   = 0x00008000,
  #  RIGHTDOWN  = 0x00000008,
  #  RIGHTUP    = 0x00000010

MIDDLEDOWN = 0x00000020
MIDDLEUP   = 0x00000040
MOVE       = 0x00000001
ABSOLUTE   = 0x00008000
RIGHTDOWN  = 0x00000008
RIGHTUP    = 0x00000010


FInputs = Input * 2
extra = c_ulong(0)

click = Input_I()
click.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
release = Input_I()
release.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))

x = FInputs( (0, click), (0, release) )
#user32.SendInput(2, pointer(x), sizeof(x[0])) CLICK & RELEASE

x2 = FInputs( (0, click) )
#user32.SendInput(2, pointer(x2), sizeof(x2[0])) CLICK & HOLD

x3 = FInputs( (0, release) )
#user32.SendInput(2, pointer(x3), sizeof(x3[0])) RELEASE HOLD


def move(x,y):
    windll.user32.SetCursorPos(x,y)

def getpos():
    global pt
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return pt.x, pt.y

def slide(a,b,speed=0):
    while True:
        if speed == 'slow':
            sleep(0.005)
            Tspeed = 2
        if speed == 'fast':
            sleep(0.001)
            Tspeed = 5
        if speed == 0:
            sleep(0.001)
            Tspeed = 3

        x = getpos()[0]
        y = getpos()[1]
        if abs(x-a) < 5:
            if abs(y-b) < 5:
                break

        if a < x:
            x -= Tspeed
        if a > x:
            x += Tspeed
        if b < y:
            y -= Tspeed
        if b > y:
            y += Tspeed
        move(x,y)


def click():
    windll.user32.SendInput(2,pointer(x),sizeof(x[0]))

def hold():
    windll.user32.SendInput(2, pointer(x2), sizeof(x2[0]))

def release():
    windll.user32.SendInput(2, pointer(x3), sizeof(x3[0]))


def rightclick():
    windll.user32.mouse_event(RIGHTDOWN,0,0,0,0)
    windll.user32.mouse_event(RIGHTUP,0,0,0,0)

def righthold():
    windll.user32.mouse_event(RIGHTDOWN,0,0,0,0)

def rightrelease():
    windll.user32.mouse_event(RIGHTUP,0,0,0,0)


def middleclick():
    windll.user32.mouse_event(MIDDLEDOWN,0,0,0,0)
    windll.user32.mouse_event(MIDDLEUP,0,0,0,0)

def middlehold():
    windll.user32.mouse_event(MIDDLEDOWN,0,0,0,0)

def middlerelease():
    windll.user32.mouse_event(MIDDLEUP,0,0,0,0)
JThundley
Python Fan
Python Fan
 
Posts: 2
Joined: Sun Mar 22, 2009 11:15 am

Re: Mouse/Keyboard control scripting module.

Postby virtualjim on Fri Sep 18, 2009 11:54 am

Very nice. I was looking for something that would let me use a 6DOF-tracked wand in a virtual reality room (http://www.isl.uiuc.edu/Labs/CUBE/CUBE.html--its got a PC running each screen) as a mouse, this looks like it'll do the trick. Thanks!
virtualjim
Python Fan
Python Fan
 
Posts: 1
Joined: Fri Sep 18, 2009 11:31 am

Re: Mouse/Keyboard control scripting module.

Postby sarahlish18 on Sun Nov 29, 2009 11:34 am

Hi,

I would like to automate an application. Since the app is proprietary and does not have any mechanism to extend its features. So I was thinking of writing a program that will move the mouse to click a particular button/menu/etc in the app.

Can this script accomplish that? Are there other ways of achieving this?

I am not familiar with Python but I am willing to learn. My primary language is VBA. Thanks BlueKitties for uploading the script, I find it to be a valuable learning experience.
sarahlish18
Python Fan
Python Fan
 
Posts: 1
Joined: Sun Nov 29, 2009 11:12 am

Re: Mouse/Keyboard control scripting module.

Postby pierocks on Wed Mar 10, 2010 2:59 pm

Thanks! I used your module with pygame to make a program that lets you use your joystick as a mouse!
pierocks
New Python User
New Python User
 
Posts: 28
Joined: Sun Sep 27, 2009 4:48 pm

Re: Mouse/Keyboard control scripting module.

Postby aliahlewis on Thu Mar 25, 2010 5:34 am

Very nifty solution. Thanks for the module. I will try it out tomorrow and pm you if I find any difficulty. If you have some tutorial on development of modules keyboard drivers then please post them here or mail me.
aliahlewis
Python Fan
Python Fan
 
Posts: 3
Joined: Tue Mar 23, 2010 2:03 am

Re: Mouse/Keyboard control scripting module.

Postby pepijn on Fri Apr 09, 2010 9:06 am

@pierocks
I was thinking about doing the same thing with PyMouse, which I wrote to do this stuff on Mac and Linux to. It's based on this code for the Windows part though. Can I get your code somewhere?

@BlueKitties
As I just said, I largely based the Windows code for PyMouse on this snippet.
Since PyMouse 1.0 it supports receiving events.
I noticed that when I generate a click that the event system sees 2 clicks while there is only one when I click myself.
Any idea what could cause this?

PyMouse is at http://github.com/pepijndevos/PyMouse by the way.
Pleas correct my English, otherwise I'll never learn it. And I'm sorry if I managed to insult you: http://weblogs3.nrc.nl/denglish/
User avatar
pepijn
Python Heavy Programmer
Python Heavy Programmer
 
Posts: 218
Joined: Sat Mar 21, 2009 7:34 am

Re: Mouse/Keyboard control scripting module.

Postby somanayr on Fri Apr 23, 2010 7:52 pm

I'm sorry. I'm pretty new to python. I want to make a bot for a game, but I'm not sure where to put this file or how to use it. I use ubuntu 9.04. Can I get some help, please?
somanayr
Python Fan
Python Fan
 
Posts: 4
Joined: Wed Aug 19, 2009 4:02 pm

Re: Mouse/Keyboard control scripting module.

Postby Micseydel on Fri Apr 23, 2010 7:58 pm

somanayr wrote:I'm sorry. I'm pretty new to python. I want to make a bot for a game, but I'm not sure where to put this file or how to use it. I use ubuntu 9.04. Can I get some help, please?

You're new? Have you checked out the Beginner's Forum? viewforum.php?f=3
Python 2.7 on Mac OS X Snow Leopard, Ubuntu Lucid Lynx, and Windows 7 triple boot.
User avatar
Micseydel
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1478
Joined: Sun Aug 26, 2007 2:25 pm
Location: Redondo Beach, California

Re: Mouse/Keyboard control scripting module.

Postby somanayr on Sat Apr 24, 2010 8:51 am

Alright, will do. I came here because I found it on a google search. I've been looking for a while on how to control the mouse with c++, but I haven't found much.
Thanks!
somanayr
Python Fan
Python Fan
 
Posts: 4
Joined: Wed Aug 19, 2009 4:02 pm

Re: Mouse/Keyboard control scripting module.

Postby nathancolorado on Sat Apr 24, 2010 6:34 pm

mousemacro.py is great and did most of what I needed, however I needed to get the color under the mouse, so I stole code from hereand added a getcolor() def:

Code: Select all
def getcolor(x=-1,y=-1,max_colors=256):
        #returns a tuple with the RGB value of the screen color
        #if no x,y position input, returns color under mouse at current location
        #if x,y input, then color at that location"""
        import ImageGrab
        if x == -1 and y == -1:
            x,y = getpos() # get current mouse position
        bbox = [x,y,x+1,y+1] # left, upper, right, lower - just 1 pixel
        img=ImageGrab.grab(bbox)
        color = img.getcolors(max_colors)
        #print color
        return color


It requires the PIL package - not sure if that's a standard package. Just thought I'd share in case it's useful. (BTW, I'm extremely new to python, so sorry if there is something retarded in the code)
nathancolorado
Python Fan
Python Fan
 
Posts: 1
Joined: Sat Apr 24, 2010 6:21 pm


Return to Python Scripts

Who is online

Users browsing this forum: No registered users and 3 guests


Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.