Skip to main content

PyWinController

Overview

PyWinController is a Python module that provides an interface for interacting with Windows applications using pywinauto. It enables window manipulation, keyboard inputs, and mouse interactions.

Requirements

  • Python 3.x
  • pywinauto
  • pynput (optional)

Installation

pip install pywincontroller

Classes

COORDS

Represents a coordinate with x and y values.

Attributes:

  • x (int): X-coordinate.
  • y (int): Y-coordinate.

RECT

Represents a rectangular area in a window.

Attributes:

  • left, top, right, bottom (int): Define the position and boundaries.
  • width, height (int): Computed if not explicitly set.
  • middle (COORDS): Midpoint of the rectangle.

Methods:

  • width(): Returns the rectangle's width.
  • height(): Returns the rectangle's height.
  • mid_point(): Computes the midpoint coordinates.

WinController

Controls a Windows application window.

Attributes:

  • main_process (str): Path of the main executable.
  • process_name (str): Name of the window process.
  • input_per_sec (int): Amount of inputs per second / refresh rate between 2 actions.
  • actions (dict): Mapping of action names to key bindings.

Methods:

  • update_window(): Updates window dimensions.
  • to_image(): Captures a screenshot.
  • stop(): Stops interactions and the frame update.
  • focus(): Sets focus to the application window.
  • type_keys(keys, ...): Simulates keyboard input.
  • press(button, action): Simulates key press.
  • release(button, action): Releases a key.
  • release_all(): Releases all pressed keys.
  • move_cursor(x, y, key_pressed): Moves the cursor.
  • drag_cursor(x, y, button, key_pressed): Drags the cursor.
  • click(button, double): Clicks the mouse button.
  • press_cursor(button, coords, key_pressed): Presses a mouse button at given coordinates.
  • release_cursor(button, coords, key_pressed): Releases a mouse button at given coordinates.
  • do(actions, buttons): Performs a series of actions.
  • undo(actions, buttons): Reverts performed actions.

Callback / Decorator :

  • on_update(callback): Registers an update callback function.
  • on_input(callback, keyboard: bool = True, mouse: bool = False, core: str = "pywinauto") # core="pynput"

Usage Example

from pywincontroller import WinController, RECT


class MarioBros(WinController):
    def __init__(self, input_per_sec: int = 3):
        WinController.__init__(self, main_process="Mesen", process_name="Mesen - Super Mario Bros. (Europe) (Rev A)", input_per_sec=input_per_sec)

        self.actions: dict = {
            "JUMP": "k",
            "FIRE": "j",

            "START": "i",
            "SELECT": "u",

            "UP": "z",
            "DOWN": "s",
            "LEFT": "q",
            "RIGHT": "d",
        }

    def to_image(self):
        top_crop: int = 120
        return self.main_win.capture_as_image(RECT(left=self.left+10, top=self.top+top_crop, width=self.width-30, height=self.height-top_crop-20))

    def get_timer(self):
        return self.main_win.capture_as_image(RECT(left=self.left+422, top=self.top+101, width=50, height=18))

    def get_score(self):
        return self.main_win.capture_as_image(RECT(left=self.left+52, top=self.top+101, width=102, height=18))

    def get_coins(self):
        return self.main_win.capture_as_image(RECT(left=self.left+212, top=self.top+101, width=38, height=18))



mb = MarioBros(input_per_sec=3)
mb.focus()
iter = 0


@mb.on_update()
async def main():
    global iter

    if iter % 5:
        # to do an action
        mb.do(["JUMP"])

    else:
        # to do nothing
        mb.do("")

    iter += 1

    if iter >= 100:
        mb.stop()

    print(iter)

Metadata

Release files for pywincontroller 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pywincontroller 1.3.1
File Size Uploaded
pywincontroller-1.3.1.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pywincontroller 1.3.1
File Interpreter ABI Platform
pywincontroller-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.9 kB

Release files / pywincontroller-1.3.1.tar.gz

Download URL pywincontroller-1.3.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e3c44679455b1a097f7ba1f4d1a208b0bbf8a34c7a67fdd04a29c27e6dcc6a37
BLAKE2b-256 checksum
How to use checksums
de2a1c16901a5b73435dc81cd719ca192395e97046b4870c380b26d8e3b83899
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / pywincontroller-1.3.1-py3-none-any.whl

Download URL pywincontroller-1.3.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec8376912a98a3fb5d9429439fdb5818ca0f68b3bb7f2410f1f3d701d3bad3a7
BLAKE2b-256 checksum
How to use checksums
67aecd1b76d307fe9e4f1a6835c7c819fbb9da95cf186346f33e9ff9c6ba2267
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.1.2

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page