Read this page in Russian / Читать на русском
This 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.
Install the library via pip:
pip install controlhub
- 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
- Changing basic delay value by changing the environment variable
CH_DELAY
- Interact with controlhub's data right from script
Note
The basic delay is 0.2 seconds by default, but it can be changed by changing the environment variable CH_DELAY
. In controlhub core scripts CH_DELAY
is 0.8 by default.
Module to interact with windows pc via functions
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")
Execute a shell command asynchronously.
from controlhub import cmd
cmd("notepad.exe")
cmd("dir")
cmd("echo Hello World")
Search for a program by name and run it. Returns path to the program's link (on Windows otherwise None).
from controlhub import run_program
run_program("notepad")
run_program("chrome")
run_program("word")
run_program("cmd")
run_program("vscode")
Find's and kills process by its fragment
from controlhub import kill_process
print(kill_process("notepad", kill=False))
print(kill_process("chrome"))
print(kill_process("roblox"))
Maximize the current window. If absolute=True
, toggle fullscreen mode (F11).
from controlhub import fullscreen
fullscreen()
fullscreen(absolute=True)
fullscreen(absolute=False)
Switches to next window (only Windows): Alt + Tab
Switches to last window (only Windows): Alt + Shift + Tab
Makes switch_to_next_window
2 times to make current window active
Module to interact with keyboard and mouse via functions
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 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 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})")
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
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
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\")")
Module to interact with internet via functions
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 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")
Module to interact with pocketbase computer and execution records via functions
Important
Functions from this module can be executed only in controlhub scripts
Returns execution from database
from controlhub import get_execution
print(get_execution())
Updates execution in database and returns it
from controlhub import update_execution
new_execution = {
"status": "3",
"duration": 20
}
print(update_execution(new_execution))
Returns computer from database
from controlhub import get_computer
print(get_computer())
Returns computer from local env variable COMPUTER_JSON
from controlhub import get_offline_computer
print(get_offline_computer())
Updates computer in database and returns it
from controlhub import update_computer
new_computer = {
"data": {
"weather": "clear",
"mood": "good",
"vscode installed": True
}
}
print(update_computer(new_computer))
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"}
Read and return all data from the file.
print(storage.get())
# Output: {"key": "value"}
Completely replace the file contents.
storage.set({"new_key": "new_value"})
print(storage.get())
Merge new data into the file without losing existing fields.
storage.merge({"another_key": {"nested": "value"}})
print(storage.get())
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)
This project is licensed under the MIT License.