Send_key and hotkeys

更新时间:
复制 MD 格式

Usage

If your automation process requires shortcut keys, use send_key to send them.

Note

Differences between send_key and input_hotkey (see Input_hotkey usage and virtual key code reference for details):

  • Effect: send_key and input_hotkey have nearly identical effects.

  • Usage Differences

    • input_hotkey targets a specific control, which makes its execution more accurate and controllable.

    • send_key sends input to the active window or cursor, not a specific control. Ensure that preceding automation steps position the cursor correctly.

We recommend using input_hotkey whenever possible.

In code-based development mode, you can use send_key.

In visual development mode, you can use the Input Hotkey component.

For an example, see Sending Messages in DingTalk Using CV Mode.

The following examples show additional uses for send_key in code-based development mode:

  • Key combinations

    Shorthand

    Example

    Effect

    ^{a}

    send_key("^{A}")

    Ctrl + A

    !{a}

    send_key("!{A}")

    Alt + A

    +{a}

    send_key("+{A}")

    Shift + A

    #{R}

    send_key("#{R}")

    Windows logo key + R

    ^+{S}

    send_key("^+{S}")

    Ctrl + Shift + S

  • Holding down a key

    # Hold down the A key
    rpa.ui.win32.send_key("{A DOWN}")
    
    # Release the A key
    rpa.ui.win32.send_key("{A UP}")
  • Repeating a key press

    # Press the DEL key 4 times
    rpa.ui.win32.send_key("{DEL 4}")
    
    # Type 30 S characters
    rpa.ui.win32.send_key("{S 30}")
    
    # Press SHIFT+TAB 4 times
    rpa.ui.win32.send_key("+{TAB 4}")
    
    # Use a variable to control the number of SHIFT+TAB presses
    n = 4
    rpa.ui.win32.send_key("+{TAB %s }" % n)

Controlling applications

Many applications have built-in shortcut keys that let you perform actions without using the mouse.

Important

This is useful when a UI control cannot be captured, but the application provides a shortcut key for the action.

Refer to the documentation of the application you are automating.

The following example uses Notepad, which is built into Windows.

Command

Effect

send_key("!{f}")

Opens the File menu in Notepad.

send_key("{DOWN}")

Moves the focus to the next menu item.

send_key("{UP}")

Moves the focus to the previous menu item.

send_key("{LEFT}")

Switches to the menu to the left or collapses a submenu.

send_key("{RIGHT}")

Switches to the menu to the right or expands a submenu.

send_key("^+{S}")

Opens the "Save As" dialog in Notepad.

Hotkeys

Important

The Fn key cannot be simulated.

Command

Effect

{!}

!

{#}

#

{+}

+

{^}

^

{{}

{

{}}

}

{SPACE}

Spacebar

{ENTER}

Enter key on the main keyboard

{ALT}

ALT

{BACKSPACE} or {BS}

Backspace

{DELETE} or {DEL}

Delete

{UP}

Up arrow key

{DOWN}

Down arrow key

{LEFT}

Left arrow key

{RIGHT}

Right arrow key

{HOME}

HOME

{END}

END

{ESCAPE} or {ESC}

Escape

{INSERT} or {INS}

Insert

{PGUP}

Page Up

{PGDN}

Page Down

{F1} - {F12}

Function keys

{TAB}

TAB

{PRINTSCREEN}

Print Screen

{LWIN}

Left Windows logo key

{RWIN}

Right Windows logo key

{NUMLOCK}

Num Lock

{CTRLBREAK}

Ctrl+Break

{PAUSE}

PAUSE

{CAPSLOCK}

Caps Lock

{NUMPAD0} - {NUMPAD9}

Number keys on the numeric keypad

{NUMPADMULT}

Multiply key (*) on the numeric keypad

{NUMPADADD}

Add key (+) on the numeric keypad

{NUMPADSUB}

Subtract key (-) on the numeric keypad

{NUMPADDIV}

Divide key (/) on the numeric keypad

{NUMPADDOT}

Decimal point key (.) on the numeric keypad

{NUMPADENTER}

Enter key on the numeric keypad

{APPSKEY}

Windows Application key

{LALT}

Left ALT key

{RALT}

Right ALT key

{LCTRL}

Left CTRL key

{RCTRL}

Right CTRL key

{LSHIFT}

Left SHIFT key

{RSHIFT}

Right SHIFT key

{SLEEP}

Sleep key

{ALTDOWN}

Holds down the ALT key until {ALTUP} is sent

{SHIFTDOWN}

Holds down the SHIFT key until {SHIFTUP} is sent

{CTRLDOWN}

Holds down the CTRL key until {CTRLUP} is sent

{LWINDOWN}

Holds down the left Windows logo key until {LWINUP} is sent

{RWINDOWN}

Holds down the right Windows logo key until {RWINUP} is sent

{ASC nnnn}

Sends a character using its ALT+nnnn key combination.