0

I am trying to simulate pressing the key 1 within a Java application. I have tried using key code 18, text to type, key down and keystrokes but whatever I try, the application displays the a character. Even when the script types into applescript window I just get aaaaaaa.

What am I doing wrong and how to properly simulate keypresses with AppleScript?

Jawa
  • 2,336
  • 6
  • 34
  • 39
Tobias
  • 31
  • 4
  • I use the free "Key Codes" application to get the codes for any pressed key [ http://manytricks.com/keycodes/ ] –  Oct 28 '14 at 10:58
  • i downloaded that but even using key code 18 for the 1 key doesn't work – Tobias Oct 28 '14 at 11:01

1 Answers1

4

Did you try anything like the following?

tell application "System Events"
    keystroke "1"
end tell

You can pass string literals to System Events keystroke-Handler

ShooTerKo
  • 2,242
  • 1
  • 13
  • 18
  • thank you, that does yield a "1" however i would now like to hold down the 1 key for 0.2 seconds and then release it. "tell application "System Events" key down "1" delay 0.2 key up "1" end tell" this still yields an "a".(apologies; i don't know how to format this stuff) thanks – Tobias Oct 29 '14 at 14:43
  • I found this [Question](http://stackoverflow.com/questions/21472368/applescript-press-and-hold-⌘-f2-for-5-seconds) and unfortunately it seems like it will not work the way you want :( – ShooTerKo Oct 29 '14 at 16:05