Skip to main content

Collection of functions to control a Windows or Linux pc.

Project description

ControlHub python package

Read this page in Russian / Читать на русском

ControlHub is a Python automation library for Windows that provides simple APIs to control the desktop, simulate keyboard and mouse actions, and perform web-related tasks.

Installation

Install the library via pip:

pip install controlhub

Features

  • Open files and run programs
  • Simulate mouse clicks, movements, and drags
  • Simulate keyboard input and key combinations
  • Download files from the web
  • Open URLs in the default browser
  • Auto delay added to functions to prevent some errors
  • Run shell commands
  • Use context managers for holding keys
  • Manage data in a data.json file (ControlHub service)
  • Changing basic delay value by changing the environment variable CH_DELAY

[!NOTE] The basic delay is 0.2 seconds by default, but it can be changed by changing the environment variable CH_DELAY. For controlhub users, you can change this in .env file in program folder.

API Reference & Usage Examples

controlhub.desktop

open_file(path: str) -> None

Open a file with the default application.

from controlhub import open_file

open_file("C:\\Users\\User\\Documents\\file.txt")
open_file("example.pdf")
open_file("image.png")

cmd(command: str) -> None

Execute a shell command asynchronously.

from controlhub import cmd

cmd("notepad.exe")
cmd("dir")
cmd("echo Hello World")

run_program(program_name: str) -> None

Search for a program by name and run it.

from controlhub import run_program

run_program("notepad")
run_program("chrome")
run_program("word")

fullscreen(absolute: bool = False) -> None

Maximize the current window. If absolute=True, toggle fullscreen mode (F11).

from controlhub import fullscreen

fullscreen()
fullscreen(absolute=True)
fullscreen(absolute=False)

switch_to_next_window

Switches to next window (only Windows): Alt + Tab

switch_to_last_window

Switches to last window (only Windows): Alt + Shift + Tab

reload_window

Makes switch_to_next_window 2 times to make current window active

controlhub.keyboard

click(x: int = None, y: int = None, button: str = "left") -> None

Simulate a mouse click at the given coordinates or current position.

from controlhub import click

click()  # Click at current position
click(100, 200)  # Click at (100, 200)
click(300, 400, button="right")  # Right-click at (300, 400)

move(x: int = None, y: int = None) -> None

Move the mouse to the given coordinates.

from controlhub import move

move(500, 500)
move(0, 0)
move(1920, 1080)

drag(x: int = None, y: int = None, x1: int = None, y1: int = None, button: str = "left", duration: float = 0) -> None

Drag the mouse from start to end coordinates.

from controlhub import drag

drag(100, 100, 200, 200)
drag(300, 300, 400, 400, button="right")
drag(500, 500, 600, 600, duration=1.5)

get_position() -> tuple[int, int]

Get the current mouse position.

from controlhub import get_position

pos = get_position()
print(pos)

x, y = get_position()
print(f"Mouse is at ({x}, {y})")

press(*keys: Union[AnyKey, Iterable[AnyKey]]) -> None

Simulate pressing and releasing keys.

from controlhub import press

press(["ctrl", "c"])  # Copy
press(["ctrl", "v"])  # Paste

press(["ctrl", "c"], ["ctrl", "v"], "left") # Copy and paste in 1 line and press left arrow

hold(*keys: Union[str, Key])

Context manager to hold keys during a block.

from controlhub import hold, press

with hold("ctrl"):
    press("c")  # Ctrl+C

with hold("shift"):
    press("left")  # Select text

with hold(["ctrl", "alt"]):
    press("tab") # Ctrl+Alt+Tab, I

with hold("ctrl", "shift"):
    press("esc") # Ctrl+Shift+Escape

write(text: str) -> None

Type the given text. Also supports \n.

from controlhub import write

write("Hello, world!")
write("This is automated typing.")
write("ControlHub is awesome!")
write("from controlhub import write\nwrite(\"Hello, world\")")

controlhub.web

download(url: str, directory: str = "download") -> None

Download a file from a URL into a directory.

from controlhub import download

download("https://example.com/file.zip")
download("https://example.com/image.png", directory="images")
download("https://example.com/doc.pdf", directory="docs")

open_url(url: str) -> None

Open a URL in the default web browser.

from controlhub import open_url

open_url("https://www.google.com")
open_url("github.com")  # Will prepend http://
open_url("https://stackoverflow.com")

controlhub.json_storage

JSONFile(file_path: str)

Create a JSON-backed storage object to store and retrieve data, like a dictionary. All data is saved to a JSON file file_path.

from controlhub.json_storage import JSONFile

storage = JSONFile("mydata.json")
storage.set({"key": "value"})
print(storage.get())  # {"key": "value"}

JSONFile.get() -> dict

Read and return all data from the file.

print(storage.get())
# Output: {"key": "value"}

JSONFile.set(data: dict) -> None

Completely replace the file contents.

storage.set({"new_key": "new_value"})
print(storage.get())

JSONFile.merge(data: dict) -> dict

Merge new data into the file without losing existing fields.

storage.merge({"another_key": {"nested": "value"}})
print(storage.get())

JSONFile magic methods

You can also use JSONFile like a dictionary:

storage["name"] = "ControlHub"
print(storage["name"])  # Output: ControlHub

del storage["name"]

print("name" in storage)  # Output: False

for key in storage:
    print(key)

data

Default storage pointing to data.json. Mostly used for ControlHub service.

from controlhub import data

data["key"] = "value"
data["another_key"] = {"a": "b", "b": "new_value"}
print(data["key"])  # Output: "value"

data.merge({"another_key": {"b": "updated_value"}})
print(data["another_key"]["b"])  # Output: "updated_value"

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

controlhub-0.4.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

controlhub-0.4.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file controlhub-0.4.2.tar.gz.

File metadata

  • Download URL: controlhub-0.4.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for controlhub-0.4.2.tar.gz
Algorithm Hash digest
SHA256 32f08b41ebf861128bbfe0a468783ef328680ab5b334b33f3bfa241ccd17ba16
MD5 1f0bcc47c29387fd90f53827fe835164
BLAKE2b-256 85642e2676e80cade44a32a6193b3bb924f11e925881373aea3d6844cee6311c

See more details on using hashes here.

File details

Details for the file controlhub-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: controlhub-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for controlhub-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c94d7fa342f98b19bddabf55a9db887cb24b4469f6878e30620655d71a50fb9f
MD5 2d83edb387b2d977c1f8b7706fbe8d44
BLAKE2b-256 d19d9fe6c8786b2c349bb7043ed76b30cc8bcb09a1965b8fa17452051253940b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page