Skip to content

Reacting to Keyboard Input

Another simple but useful task when developing an application in AppleScript Studio is the ability to respond to keyboard input. For example, I want my application to respond to the “return” key, in certain fields, the same way that it does when one presses the “tab” key, ie. it moves the cursor to the next field.

Activate the “keyboard up” handler for the text field you wish to detect keyboard entries for and add some code.

When a key is pressed, a numerical Apple “key code” is generated that can be accessed by AppleScript. However, according to AppleScript Studio documentation:

KEY CODE: the hardware-dependent value of the key pressed; you aren’t likely to work with the key code and no constants are currently provided to specify key codes; however, you may want to experiment with possible values; (for example, for at least one keyboard, the key code for delete is 51 and for backspace is 117)

This is not very reassuring and I don’t know how standard the numbers are from keyboard to keyboard.

You can also access the “character” of the keyboard event which is just the literal character. Check the AppleScript document for other properties of the “event” class that may be useful to you, including booleans (true/false values) for things like: “command key down” “option key down” and “shift key down.”

Here is some code that will detect many of the possible keys typed into an input field and then show which numeric Apple key code corresponds to that key.

on keyboard up theObject event theEvent
	if the name of theObject is "inputField" then
		(* Is this the input field we are typing in?  *)
		set theKeyPressed to (key code of theEvent)
		(* Save the key code into a variable *)
		set theCharacterTyped to (character of theEvent)
		(* Save the character that was typed *)
		if theCharacterTyped is return then set theCharacterTyped to "RET"
		(* The return key doesn't show up nicely in the result, so let's
                replace it with "RET" *)
		
		if theKeyPressed is not 76 then
			(* 76 is the Apple key code for the "Enter" key 
                        - at least on my keyboard *)
			set the contents of text field "showResult" of window "sampleWindow" to ¬
				theCharacterTyped & "=" & theKeyPressed
			return false
			(* Returning false will allow the typing to continue without disruption *)
		else
			(* The "Enter" key was pressed *)
			display dialog "You pressed the enter key, I think."
			return false
		end if
	end if
end keyboard up

After checking to see if the user is typing into the input field (which they obviously are in a window that has only one text field receiving this handler), the key code and character of the event are put into a variable for later use (some will simply refer to this directly rather than putting it into a variable first). It then checks to see if the character typed is the return key and changes the variable to “RET” so it will show up easier in the output. The code then checks to see if the enter key was pressed (on my keyboard at least) and if not, displays the character pressed, an equals sign, and the corresponding key code. If it thinks the enter key was pressed (if your keyboard also has 76 set to “enter”) then it will display a dialog to that effect. Returning false allows the user to type undisturbed by the events. Set this to “true”, if you like, in order to see what happens to the typed text.

You can download the sample code to try this out here: Keyboard Input Sample

In the case of my own application, I’m looking for the user to press the “return” (key code 36 on my keyboard) or “enter” keys (key code 76) and if this is caught, I will “set the first responder” (see upcoming posting) to the next field I want the user to type into, as if they had pressed the “tab” key.

One Comment

  1. Gé van Gasteren wrote:

    Thanks for the good description; I’ll try that. Only, your project probably only runs in Leopard? In my XCode 2.5 it doesn’t. Anyway, I have to learn everything still.

    Wednesday, August 6, 2008 at 2:51 pm | Permalink