No internet connection
  1. Home
  2. How to

How to automate the keyboard

This script shows how to invoke special keys on the keyboard and how to type unicode text.

Solved in post #2, click to view
  • 14 replies
  1. To press a (special) key based on its position on the keyboard (useful for invoking built in PT shortcuts), use this syntax with any of the key names from the list below:

    sf.keyboard.press({ keys: 'cmd+s' });
    

    A + between two names indicates that the modifier should be held down while pressing the other key.

    Complete list of key names for use in sf.keyboard.press:

    ctrl
    cmd
    shift
    alt
    scroll down
    scroll up
    mouse button 3
    section
    grave
    a
    s
    d
    f
    h
    g
    z
    x
    c
    v
    b
    q
    w
    e
    r
    y
    t
    1
    2
    3
    4
    6
    5
    equal
    9
    7
    minus
    8
    0
    right bracket
    o
    u
    left bracket
    i
    p
    l
    j
    quote
    k
    semicolon
    backslash
    comma
    slash
    n
    m
    period
    numpad 0
    numpad 1
    numpad 2
    numpad 3
    numpad 4
    numpad 5
    numpad 6
    numpad 7
    numpad 8
    numpad 9
    numpad multiply
    numpad plus
    numpad minus
    numpad comma
    numpad divide
    numpad equals
    numpad enter
    numpad clear
    f1
    f2
    f3
    f4
    f5
    f6
    f7
    f8
    f9
    f10
    f11
    f12
    f13
    f14
    f15
    f16
    f17
    f18
    f19
    F20
    command
    shift
    caps lock
    option
    control
    right shift
    right option
    right control
    return
    tab
    space
    backspace
    escape
    function
    volume up
    volume down
    mute
    help
    home
    page up
    delete
    end
    page down
    left
    right
    down
    up
    ⇧
    ⌥
    ⌃
    ⌘
    cmd
    ctl
    control
    alt
    caps
    enter
    esc
    del
    spc
    pgup
    pgdn
    
    Reply3 LikesSolution
    1. I'm trying to trigger the windows search field on a windows computer that is running in Microsoft Remote Desktop.
      So I'm basically just trying to send the command key.

      When I use sf.keyboard.press({keys:'cmd' ) or sf.keyboard.press({keys:'command' ) I can't figure out if it's not sending the command or it's just not received on the Remote Desktop.

      My workaround at the moment is

          sf.system.execAppleScript({
              script:
                  'tell application "System Events"\n' +
                  'key code 55\n'
                  'end'
          })
      

      As this is just sending the key code without thinking about apps or anything.

      1. sf.keyboard.press cannot be used to send just the modifier key. I think you might be able to use sf.keyboard.modifiers with cmd set to true, and then to false, instead. (also insert a wait in between)

        1. Cool. Yeah tried that (without the wait earlier, but tried that now as well). But no luck.

          There's definitely something going on in terms of what Remote Desktop is sending in to windows.

          Using sf.keyboard.type also only sends the letter "a", so I have to use sf.keyboard.press and then just split up the text in order to input text.

          1. Yea gotcha. Remote Desktop is pretty messy with keyboard commands even without SF, so I can imagine it would be unstable or hard to control from SF as well.

            1. It may be better to invoke this through ssh, some kind of remote PowerShell or something like that.

              1. Yeah was thinking of seeing if AutoHot keys could be controlled from SF over ethernet.
                But I think I've got the things I wanted working right now, so that might be for later if things should be more advanced :)

    2. In reply tochrscheuer:

      To type unicode text, use the sf.keyboard.type action.
      Use this for cases where a user supplied string of text needs to be input, or anything where a series of characters - not shortcuts - need to be inserted as a value somewhere.

      Example:

      sf.keyboard.type({ text: 'One two three four æøå ÆØÅ ö ü ä and done' });
      
      1. J
        In reply tochrscheuer:
        Jonathan Grossman @Jonathan_Grossman
          2020-05-13 17:09:55.930Z

          I'm using the type text macro to speed up input of my password when installing something for example.

          the macro works when I'm in a program like MAIL but won't work when there is pop up dialog box like when you are installing an app and it asks for your password.

          1. Hi Jonathan,

            Generally, I would advise very strongly against this type of macro. I would definitely love to have one like this myself, but since your settings are stored in the cloud, storing your password in clear text in a script or macro is not safe.

            1. ok thanks

          2. T
            In reply tochrscheuer:
            TJ Forman @TJ_Forman
              2023-02-09 02:23:24.641Z

              Not sure why some of these scripts work. And the last one does not. Hopefully someone with more experience can help me out.

              **This one works in PT is selects the very next full region to the right. Works Great with Stream Deck! **
              sf.keyboard.press({ keys: 'ctrl+tab' });

              This one works too. Selects the very next full region in PT and then centers it.
              sf.keyboard.press({ keys: 'ctrl+tab' });
              sf.keyboard.press({ keys: 'right' });

              **This does not work. It looks like it should based on the syntax of my understanding. I'm fairly new! **
              sf.keyboard.press({ keys: 'ctrl+tab+alt' });

              1. You would need the modifier keys first, ie. ctrl and alt, then followed by the key to press tab, so ctrl+alt+tab.

                Note that it's easier to learn how to do this by making a Press Keys macro action and then convert it to a script.

                1. TTJ Forman @TJ_Forman
                    2023-02-09 18:42:57.611Z

                    Yes! That worked great. Thank you for the support!