Skip to content

Instantly share code, notes, and snippets.

@parano
Created March 19, 2014 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parano/9653676 to your computer and use it in GitHub Desktop.
Save parano/9653676 to your computer and use it in GitHub Desktop.
Send random arrow keys using ruby, Mac OS X only
#!/usr/bin/ruby -w
def presskey(keycode)
system "osascript -e 'tell application \"System Events\" to key code #{keycode}'"
end
kVK_LeftArrow = 123
kVK_RightArrow = 124
kVK_DownArrow = 125
kVK_UpArrow = 126
arrow_keys = [kVK_LeftArrow, kVK_RightArrow, kVK_DownArrow, kVK_UpArrow]
if $0 == __FILE__
sleep(2)
while(true) do
presskey(arrow_keys.sample)
sleep(0.4)
end
end
@amirrajan
Copy link

amirrajan commented Jan 31, 2018

Another example (this one is multiline):

system "osascript -e 'tell application \"System Events\"' -e 'keystroke \"t\" using control down' -e 'end tell'"

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