Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Assigning function keys in Cocoa apps System
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)?
    •    
  • Currently 3.25 / 5
  You rated: 4 / 5 (4 votes cast)
 
[9,538 views]  

Assigning function keys in Cocoa apps | 5 comments | Create New Account
Click here to return to the 'Assigning function keys in Cocoa apps' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Strange characters...
Authored by: _merlin on Jun 09, '01 02:27:56AM

The strange characters representing the F1 to F16 are actually Mac Roman renditions of UTF-8 character sequences. When you copy this property list into your text editor, make sure you save the file using the Mac OS Roman encoding. If you don't, it won't work. All property lists are in UTF-8 format, however, no font I have seen will render the sequences representing the F keys. There is also no way to type them. So copy the property list from here, paste it into a text editor, preferably BBEdit or TextEdit (a command-line editor probably won't work), then save it as plain text in Mac Roman format. Anything else won't work.

Vasantha Crabb



[ Reply to This | # ]
How about shifted F-keys and Terminal?
Authored by: kent37 on Jan 07, '02 11:41:55AM

This is a great tip, thanks!

Is there any way to map shifted F-keys? For example I would like to map shift-F1 to Redo.
Is there a way to make F2-F4 map to Cut/Copy/Paste in Terminal?



[ Reply to This | # ]
How about shifted F-keys and Terminal?
Authored by: Frungi on Jan 27, '08 08:44:36PM
Shifted keys have a $ in front of the key, like $UF704 for shift-F1 (F704 being F1's hex code in Unicode). You can also use ^ for control, @ for command, and ~ for option. See also this page, from the same Google search that brought me here.

[ Reply to This | # ]
Assigning function keys in Cocoa apps
Authored by: Frungi on Jan 27, '08 08:28:26PM

The Unicode characters for the keys can actually be typed in hex, as \U#### (e.g. \UF704 for F1). The codes can be found in NSEvent.h. I have no idea why it goes up to F35 (\UF726), though



[ Reply to This | # ]
Assigning function keys in Cocoa apps
Authored by: Frungi on Jan 27, '08 08:57:11PM

Well, this doesn't seem to work in the XML style used here, though it does in the older "key" = "command" style used in the page I linked to. I suppose you could enter the non-symbol for the keys using the character palette, but I'll be using the older format, because it's easier to read and it's possible to type.



[ Reply to This | # ]