Skip to main content

ydotool client implemented in Python

Project description

pydotool

A ydotool client implemented in Python. The aim of pydotool is to write automation scripts in an easy way.

Usage

  • click event
  • key event
  • mouse move event
  • type characters

How to Use

  • Install ydotoold. you may install it from your package manager, or build it from source.

  • Install pydotool. pip install python-ydotool

  • Write your automation script. Call init once in your program to connect to ydotoold

import pydotool
pydotool.init()

If you use another ydotoold socket than the default one (/tmp/.ydotool_socket), you can provide it in environment variable YDOTOOL_SOCKET, or pass it to init()

pydotool.init("/run/ydotoold/socket")

How to Implement the Examples in ydotool

Switch to tty1 (Ctrl+Alt+F1), wait 2 seconds, and type some words:

import time

from pydotool import KEY_F1, KEY_LEFTALT, KEY_LEFTCTRL, key_combination, init, type_string

init()  # call only once before using pydotool

key_combination([KEY_LEFTCTRL, KEY_LEFTALT, KEY_F1])
time.sleep(2)
type_string("echo Hey guys. This is Austin.")

Close a window in graphical environment (Alt+F4):

from pydotool import KEY_F4, KEY_LEFTALT

key_combination([KEY_LEFTALT, KEY_F4])

Relatively move mouse pointer to -100,100:

from pydotool import mouse_move
mouse_move((100, 100))

Move mouse pointer to 100,100:

mouse_move((100, 100), True)

Mouse right click:

from pydotool import right_click

right_click()

Mouse repeating left click:

from pydotool import click_sequence, ClickEnum

click_sequence([ClickEnum.LEFT_CLICK] * 5)

stdin is not natively supported, but you can implement it with python stdin.

Advanced Usage

There is no delay in pydotool API after the last event emitted.

for i in range(5):
    left_click()
    if i != 4:
        time.sleep(0.025)

# above code is equivalent to
click_sequence([ClickEnum.LEFT_CLICK] * 5)

Customize the delay time:

# Each click event is combined with "press" and "release", so there are totally 10 ydotool events, and 9 delays between each two consecutive events.
# the 9 delays are 10ms, 20ms, ..., 90ms.
click_sequence([ClickEnum.LEFT_CLICK] * 5, delay_sequence=list(range(10, 100, 10)))

Complex key operations:

# Ctrl+K and Ctrl+D without releasing Ctrl
key_seq([(KEY_LEFTCTRL, DOWN), (KEY_K, DOWN), (KEY_K, UP), (KEY_D, DOWN), (KEY_D, UP), (KEY_LEFTCTRL, UP)])

Control the press time when typing:

# Type "abcde", but hold 1 sec for each character.
# Note that long press may generate more content than expected.
type_string("abcde", hold_delay_ms=1000, each_char_delay_ms=20)
# possible output: 
# aaaaaaaaaaaabbbbbbbbbbbccccccccccccddddddddddddeeeeeeeeeeee

Long press key combination:

# Press Alt+F4 for 3 secs. Can close most of the apps. Not recommended to try this.
key_combination([KEY_LEFTALT, KEY_F4], press_ms=3000)

Using the low level API to send event:

# directly send an event using uinput_emit()
uinput_emit(EV_REL, REL_Y, 100, True)  # move 100 in y direction

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

python_ydotool-1.1.0.tar.gz (40.5 kB view details)

Uploaded Source

File details

Details for the file python_ydotool-1.1.0.tar.gz.

File metadata

  • Download URL: python_ydotool-1.1.0.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_ydotool-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d3f96c91cf8da2a5f73a1f6ebd45ae318795c01b4dcdb36620c3492e03d55090
MD5 e10c21a5450d966fd4cc12e650049f7e
BLAKE2b-256 f0f4b5240a9477f8dd33dbfb947f52e6b119c634a830b2c23b0266058e55f60c

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