Assigning function keys in Cocoa apps

Jun 08, '01 08:58:47AM

Contributed by: tlr

After a lot of tryouts, I figured how to have the function keys F1, F2, etc., working (in Cocoa app only). In particular, I like to have F1 = undo, F2 = cut, F3 = copy, F4 = paste. As you will see I also have F9 = save (faster than Cmd-S) and F12 = check spelling.

If you're interested in defining your F-keys in Cocoa apps, read the rest of this article...

Here is the content of my file DefaultKeyBinding.dict:In order to do that you must have a defaultKeyBindings file. You must first create a folder named "KeyBindings" in your Library folder (if necessary). Then create a text file named "DefaultKeyBinding.dict" (don't misspell the names; they do not contain quotes). If you use TextEdit, ensure that the file is in Text format, not RTF, using the Format menu. All Cocoa apps apply the key bindings, after you relaunch them.

Here is the content of my file DefaultKeyBinding.dict:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>ÔúÑ</key>
<string>undo:</string>
<key>ÔúÖ</key>
<string>cut:</string>
<key>ÔúÜ</key>
<string>copy:</string>
<key>Ôúá</key>
<string>paste:</string>
<key>Ôúà </key>
<string>complete:</string>
<key>Ôúâ</key>
<string>noop6:</string>
<key>Ôúä</key>
<string>replace:</string>
<key>Ôúã</key>
<string>findNext:</string>
<key>Ôúå</key>
<array>
<string>save:</string>
<string>saveDocument:</string>
</array>
<key>Ôúç</key>
<array>
<string>open:</string>
<string>openDocument:</string>
</array>
<key>Ôúé</key>
<string>noop11:</string>
<key>Ôúè</key>
<string>checkSpelling:</string>
<key>Ôúê</key>
<string>noop13:</string>
<key>Ôúë</key>
<string>noop14:</string>
<key>Ôúí</key>
<string>noop15:</string>
</dict>
</plist>
It is a dictionary mapping keys to string representing common Cocoa commands.

The strange value in correspond to function keys, from F1 to F15 here. Note that some are not affected (noopXX: where XX is the number of the key). Some are affected to two commands, since the name differs from an application to another.

Note : F5 is already affected to complete: in the standard key binding file, so it is just redundant here. it is the only F-key alreadyaffected. I suggest you don't change it. The complete: command is used by some applications like ProjectBuilder to complete the word you're typing.

Remark: the keys F10, F7, F8 do not work for me. I don't know why (thenames of the commands are correct).

How to find other interesting key bindings:
Take a look in file "StandardKeyBinding.dict" hidden somewhere in System (use Sherlock). The settings in your defaultKeyBinding file override these.

How to figure out the name of commands:
You can try to guess. Here is a better way, assuming you did install the Developer tools: locate a Cocoa app (like TextEdit), control click inthe Finder; in the contextual menu, choose "display content" (don't know the exact name in English). A new window opens, with a "Contents"folder; open it, and open the "Resources" folder inside. Then open the nib files inside (InterfaceBuilder should launch) until you find one containing "Main menu". Open that, choose a menu, and in the inspector Window take a look at "connections". Here is the name of the command corresponding to the menu.

P.S.: Since you need a KeyBindings folder in your Library folder, I guess you can have more than one keybindings file, presumably one for each Cocoa app. Does anyone know to do that (a file with the name of the app doesn't work)?

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20010608085847818