A very simple AppleScript like this:

tell application "System Events"
    keystroke "abc 123"
end tell

results in just “abc” being typed. Online resources suggest this should not be the case. Adding a delay between keystrokes didn't help.

What might be causing this or how can I determine what might be causing this?

Small side question: in AppleScript terminology, what is keystroke? A function, command, something else?

  • what sort of keyboard do you have. The only thing I can think of is something like a numbers lock (although I would be surprised if even if you did have it on it would affect the keystroke command)
    – markhunte
    Aug 24, 2014 at 12:45
  • I'm on a MacBook Pro, no numlock. Also, I forgot to mention that for quite a while I had an AppleScript that was 'keystroke'ing numbers. Aug 24, 2014 at 15:46

4 Answers 4


Have a look at https://stackoverflow.com/questions/18136567/applescript-keystroke-not-behaving-as-expected & the list of ANSI codes at How do I automate a key press in AppleScript?

It appears you're not the only one with the issue. One solution on there was to use the key code instead...

tell application "System Events"
    key code {18} using {command down}

It may depend on what app you're trying to send the keystrokes to - I just tested with an already-open blank document in Text Edit using

tell application "TextEdit" to activate
tell application "System Events"
    keystroke "abc 123"
end tell

& it worked as expected.

A keystroke is the scripted equivalent of actually pressing that/those keys[s]

How about...

tell application "TextEdit" to activate
tell application "System Events"
    keystroke "abc"
    keystroke space
    keystroke "123"
end tell
  • Good catch on that question, it does sound related. I can get key code to send numbers but even the exact AppleScript you posted doesn't type the numbers. Aug 24, 2014 at 8:21
  • what about if you send 'key code {83}' 'key code {84}' 'key code {85}' which are the num-pad keys, or 18 19 20, which are the numbers above the letters.
    – Tetsujin
    Nov 30, 2014 at 14:00
  • I think that's what I had done after reading your answer and the links but honestly at this point I forget. It was a bit awkward but it worked. Nov 30, 2014 at 14:05
  • tbh, I quite enjoyed the research - & I now have a nice little app that will tell me the key codes ;-)
    – Tetsujin
    Nov 30, 2014 at 14:07

Some more info about this issue that I've discovered is that when using keystroke with numbers, the System Events always sends them as ANSI_Keypad# characters (code 82-92) instead of what you might expect as ANSI_# characters (code 18-29).

For most Mac applications, this does not matter as OS X itself does not care about (use, support, etc) the NUMLOCK function on a Keypad, therefore the Keypad numbers are seen the same as keyboard numbers. However, it will pass the NUMLOCK keypress to the application if you have a keyboard/keypad that has this key.

There are a few applications that do monitor the NUMLOCK key (for example, VMware Fusion application when running a Windows VM) and will change the behavior based on the NUMLOCK state.

So for example, if the AppleScript sends keystroke "456" to a NUMLOCK aware application.

  • If the NUMLOCK state is ON, the numbers "456" will appear.
  • If the NUMLOCK state is OFF, the equivalent keys received are Left Arrow 5 Right Arrow

In the original question, the AppleScript sent abc 123 but most likely his application (which was not mentioned) was aware of the NUMLOCK state, which was currently OFF, and therefore executed the keys as abc [space] End Down Arrow Page Down

I put together a little AppleScript function that loops through the given string sending key code commands for any numbers and keystroke commands for any other characters.

on numberAsKeycode(theString)
  tell application "System Events"
    repeat with currentChar in (every character of theString)
      set cID to id of currentChar
      if ((cID ≥ 48) and (cID ≤ 57)) then
        key code {item (cID - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
        keystroke currentChar
      end if
    end repeat
  end tell
end numberAsKeycode

set myString to "abc 123"

Which executes the following

tell application "System Events"
  keystroke "a"
  keystroke "b"
  keystroke "c"
  keystroke " "
  key code {18}
  key code {19}
  key code {20}
end tell

Hope this helps :)

  • Thanks! I was having this issue with an AppleScript I was using to send clipboard contents to a Parallels VM as keystrokes, and it took me a while to figure out that NumLock was the issue! Using your function with numberAsKeycode(the clipboard as text) did the trick! Jan 23, 2022 at 21:58

As @user271866, I was having this problem and discovered that it was "Enable Mouse Keys" being checked in System Preferences that was affecting the behavior of keystroke.

I had an automator service I had been using to enter search terms into spotlight and copy & paste the result. When I got a new computer, I installed the service. I remembered also turning on mouse keys during the setup and as luck would have it, before I first ran the service today on this new computer, my Magic Mouse 2 battery died and I used mouse keys to to connect my old Magic Mouse. I then encountered erratic behavior from the service when I tried it. On a hunch, I tried disabling mouse keys and running the service and it worked! It pasted the numbers along with the rest of the string.

One thing to note is, it's not just numbers. Other characters on the numeric keypad (but not all) also will not get "typed out" by keystroke. . and / also will not work in a string supplied to keystroke. I thus modified @Insomniac_Software's code to create a working replacement/wrapper for the keystroke function:

--This is a replacement for the keystroke function, which fails when "Enable Mouse Keys" is checked in System Preferences -> Accessibility -> Mouse & Trackpad.  Keystroke will not "type out" any numbers, a dot ('.') or a slash ('/') that are in the supplied string when this option is enabled in System preferences.  This method is safe to use whether that setting is checked or not.  Call like this: my keystrokeForMouseKeys("your string")
on keystrokeForMouseKeys(theString)
    tell application "System Events"
        repeat with theChar in (every character of theString)
            set theCode to id of theChar
            if theCode is 44 then
                key code 44
                --Not sure why 46 (m) is what you get when you call id on "." but key code 47 types a "." and key code 46 types an 'm'...
            else if theCode is 46 then
                key code 47
            else if ((theCode ≥ 48) and (theCode ≤ 57)) then
                key code {item (theCode - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
                keystroke theChar
            end if
        end repeat
    end tell
end keystrokeForMouseKeys

Now, if you call my keystrokeForMouseKeys("./0123456789"), you get:


Tested on macOS High Sierra 10.13.6.

  • Oh yeah, I also encourage everyone to submit feedback to Apple about the bug in keystroke when Enable Mouse Keys is checked. I did. The more people that complain, the more likely they will be to fix it.
    – hepcat72
    Sep 9, 2018 at 21:46

have you checked if Mouse Keys is enabled (System Preferences -> Accessibility -> Mouse & Trackpad -> Enable Mouse Keys)?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .