Skip to content

Instantly share code, notes, and snippets.

@nzaillian
Created October 12, 2010 13:43
Show Gist options
  • Save nzaillian/622181 to your computer and use it in GitHub Desktop.
Save nzaillian/622181 to your computer and use it in GitHub Desktop.
"""Found myself needing to emulate key presses in an application on OS X. The solution was the following:"""
import os
def press_key(key):
cmd = "osascript -e 'tell application \"System Events\" to keystroke \"" + key + "\"'"
os.system(cmd)
@adhirmndl
Copy link

for 'up' & 'down' key what key value should be passed..?

@holg
Copy link

holg commented Oct 6, 2018

keystroke uses a string of the pressed key as argument

osascript -e 'tell application "System Events" to keystroke  "K" using command down'

sends CMD-K

osascript -e 'tell application "System Events" to key code  126  using command down'

sends CMD-Cursor Up Code key (lang independent: 123 cursor left , 124 cursor right, 125 cursor down, 126 cursor up)
here a nice reference i found:
https://eastmanreference.com/complete-list-of-applescript-key-codes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment