MacOS – Applescript dialog – return/enter special keys (such as page down)

applescriptmacos

I am creating an Applescript that show a dialog where the user should be able to enter either any common character (such as a) or any special key (such as page down or ⌘). How can the latter keys/key codes be entered into a regular text field in an Applescript dialog?

Best Answer

As promised:

display dialog "Enter your ASCII keycode" default answer ""
set a to the text returned of the result
tell application "System Events" to keystroke (ASCII character a)

Heres a list of all the possible key codes.

Common ones:

You can use any ASCII code, for the arrow keys this will be:

tell application "System Events" to keystroke (ASCII character 31) --down arrow

tell application "System Events" to keystroke (ASCII character 30) --up arrow

tell application "System Events" to keystroke (ASCII character 29) --right arrow

tell application "System Events" to keystroke (ASCII character 28) --left arrow