Skip to content

Keyboard

Keyboard sends keys to the currently focused window.

Keyboard

Send keystrokes to the currently focused window.

Key syntax follows pywinauto conventions::

Keyboard.press("^a")          # Ctrl+A — select all
Keyboard.press("^s")          # Ctrl+S — save
Keyboard.press("{ENTER}")     # Enter key
Keyboard.press("+{F10}")      # Shift+F10 — context menu
Keyboard.type("Hello World")  # type plain text with spaces
Keyboard.hotkey("ctrl", "c")  # Ctrl+C — copy

Special key tokens: {ENTER}, {TAB}, {ESC}, {BACK}, {DELETE}, {F1}{F12}, {UP}, {DOWN}, {HOME}, {END}.

Modifiers: ^ ctrl, + shift, % alt.

Source code in src\dolphin_desktop\_keyboard.py
class Keyboard:
    """Send keystrokes to the currently focused window.

    Key syntax follows pywinauto conventions::

        Keyboard.press("^a")          # Ctrl+A — select all
        Keyboard.press("^s")          # Ctrl+S — save
        Keyboard.press("{ENTER}")     # Enter key
        Keyboard.press("+{F10}")      # Shift+F10 — context menu
        Keyboard.type("Hello World")  # type plain text with spaces
        Keyboard.hotkey("ctrl", "c")  # Ctrl+C — copy

    Special key tokens: ``{ENTER}``, ``{TAB}``, ``{ESC}``, ``{BACK}``,
    ``{DELETE}``, ``{F1}``…``{F12}``, ``{UP}``, ``{DOWN}``, ``{HOME}``, ``{END}``.

    Modifiers: ``^`` ctrl, ``+`` shift, ``%`` alt.
    """

    @staticmethod
    def press(keys: str) -> None:
        """Send keys using pywinauto key syntax."""
        _send_keys(keys)

    @staticmethod
    def type(text: str) -> None:
        """Type plain text including spaces and special characters."""
        _send_keys(text, with_spaces=True, with_tabs=True, with_newlines=True)

    @staticmethod
    def hotkey(*keys: str) -> None:
        """Press a key combination, e.g. hotkey('ctrl', 'c')."""
        modifiers = {"ctrl": "^", "shift": "+", "alt": "%"}
        combo = ""
        for k in keys:
            combo += modifiers.get(k.lower(), f"{{{k.upper()}}}")
        _send_keys(combo)

press staticmethod

press(keys: str) -> None

Send keys using pywinauto key syntax.

Source code in src\dolphin_desktop\_keyboard.py
@staticmethod
def press(keys: str) -> None:
    """Send keys using pywinauto key syntax."""
    _send_keys(keys)

type staticmethod

type(text: str) -> None

Type plain text including spaces and special characters.

Source code in src\dolphin_desktop\_keyboard.py
@staticmethod
def type(text: str) -> None:
    """Type plain text including spaces and special characters."""
    _send_keys(text, with_spaces=True, with_tabs=True, with_newlines=True)

hotkey staticmethod

hotkey(*keys: str) -> None

Press a key combination, e.g. hotkey('ctrl', 'c').

Source code in src\dolphin_desktop\_keyboard.py
@staticmethod
def hotkey(*keys: str) -> None:
    """Press a key combination, e.g. hotkey('ctrl', 'c')."""
    modifiers = {"ctrl": "^", "shift": "+", "alt": "%"}
    combo = ""
    for k in keys:
        combo += modifiers.get(k.lower(), f"{{{k.upper()}}}")
    _send_keys(combo)

Usage

from dolphin_desktop import Keyboard

Keyboard.press("^s")          # Ctrl+S
Keyboard.press("{ENTER}")     # Enter
Keyboard.type("Hello World")  # plain text
Keyboard.hotkey("ctrl", "c")  # Ctrl+C

Key Notation

Keyboard.press() uses pywinauto send_keys notation.

Notation Key
{ENTER} Enter
{TAB} Tab
{ESC} Escape
{BACK} Backspace
{DELETE} Delete
{UP}, {DOWN}, {LEFT}, {RIGHT} Arrow keys
{HOME}, {END} Home / End
{F1} ... {F12} Function keys
^ Ctrl modifier
% Alt modifier
+ Shift modifier