Xah Lee, 2007-12
This page tells you how to modify Mac OS X's keybindings, so that you can design your own keyboard shortcuts for cursor movement and editing commands or insert unicode character.
In Mac OS X, you can type a special character such as “üéå«»ƒ®©™”≠≤≥° by holding down the Option key. To see what characters are available, you can pull the Keyboard Viewer. To start Keyboard Viewer, go to System Preferences, International, Input Menu tab, then click at the bottom “Show input menu in menu bar”. Then, on the input menu at the top right of the menu bar, there's a “Show Keyboard Viewer” menu. This will show the Keyboard Viewer.
above: Key maps from the Keyboard Viewer (using a Dvorak keyboard), showing you what characters you can type using the Option key. Those colored orange are prefix keys, allowing you to type combination characters such as “áéíóú”.
The default map for special chars tries to jam all the commonly used characters to cover diverse needs (may it be for Europeans (“éüçø£¡¿«»”), or printers (•“”©®™†¶), or mathematicians (∑µ≠π)). You may find that you don't need most of these chars, while some others you need but are not there. The good news is that you can customize the keymap for your own need.
The default keybiding used by the system is at “/System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict”. This is a XML based file.
To create your own, you need to create a file at “~/Library/KeyBindings/DefaultKeyBinding.dict”. (create the KeyBindings dir if you don't already have it)
The “DefaultKeyBinding.dict” file is just a text file (it must be encoded in utf8. (if you don't know what utf8 is, just keep all text in your file ascii)). Its content should be key and action pairs, like this:
{
keycode1 = actionCode1;
keycode2 = actionCode2;
...
}
The file can contain comments like this: “/* this is comment */”.
The keycodes are strings that represent key presses, such as pressing the “b” key, “Cmd+b”, “Ctrl+b”, “keypad 2”, “f2” etc. The actionCode represents what to do. Actions may be inserting characters, or moving the cursor to the beginning of line, or page up, or copy and pasting, etc. (The available actions are predefined in OS X)
Here is a example of the content for DefaultKeyBinding.dict:
/* ~/Library/KeyBindings/DefaultKeyBinding.dict */
{
"~a" = ("insertText:", "α");
"~;" = ("insertText:", "♥");
}
This means, pressing “Option+a” will insert “α”, pressing “Option+;” will insert “♥”. (The tilda character “~” is there to indicate holding the Option down)
Once you've created your DefaultKeyBinding.dict file, restart a Cocoa application and the new keybinding will take effect in that application. (only applications that uses Cocoa Text System will support this. See below.)
A normal key on the keyboard is simply represented by its character. Like this:
{
"." = ("insertText:", "a");
"2" = ("insertText:", "a");
"b" = ("insertText:", "a");
"B" = ("insertText:", "a");
"!" = ("insertText:", "a");
}
With the above, typing any of the chars on the left will insert “a”.
To represent a combination keypress such as “Cmd+b”, prefix a “@” in front of the character, like this: “"@b"”. Here's a table showing the prefixes for various modifier keys:
prefix meaning (glyph rep)
-----------------------------
~ Option key (⌥)
$ Shift key (⇧)
^ Control key (⌃)
@ Command key (⌘)
# keys on number pad
Example:
{
"~a" = ("insertText:", "opt a");
"^b" = ("insertText:", "crtl b");
"@c" = ("insertText:", "cmd c"); /* won't work, see below */
"^@d" = ("insertText:", "cmd d"); /* won't work, see below */
"^~e" = ("insertText:", "crtl opt e");
"#2" = ("insertText:", "2 on the keypad");
}
Note: key combination with the Cmd key usually won't work because other parts of the input manager in OS X consumed it before the Cocoa Text System sees it. Some combination with the Option key also won't work. For example, “"~n" = ("insertText:", "a");” will not work, presumably because “Option+n” is set to enter a prefix for “~” over the char and cannot be overridden.
Note: for some keypresses involving the shift key, such as typing “A” or “!”, do not use “$a” or “$1”; just use the characters themselves like “A” or “!”. Use the dollar sign for special keys, for example: “$\UF72C” for “Shift+PageUp”.
Here are some standard keys on PC keyboard and their key code designated by the Cocoa Text System:
code key description ----------------------------- \U001B Esc \U007F Backspace/delete † \U0009 Tab \U000D Enter/Return † \U0003 Enter (on number-pad) † \UF700 up arrow \UF701 down arrow \UF702 left arrow \UF703 right arrow \UF729 Home \UF72B End \UF72C Page Up \UF72D Page Down \UF727 Ins (Insert) \UF728 Del (forward delete) \UF739 NumLock/clear † \UF704 F1 \UF705 F2 \UF706 F3 \UF707 F4 \UF708 F5 \UF709 F6 \UF70A F7 \UF70B F8 \UF70C F9 \UF70D F10 \UF70E F11 \UF71F F12 \UF710 F13 \UF711 F14 \UF712 F15
† Note: PC and Apple keyboard have some differences, many are just differences in key-labeling convention.
See also: Difference Between Apple and PC keyboards.
You can also set a sequence of keys. For example, if you want “Ctrl+x” followed by “Ctrl+s” to be Save, do this:
"^x" = {"^s" = "save:";};
Here is a list of commonly used actions.
/* insert a unicode character that has hex code 03B1 */
("insertText:", "\U03B1");
The insertion can be a string (as opposed to a single char).
"moveUp:"; "moveBackward:"; "moveLeft:"; "moveWordBackward:"; "moveWordLeft:"; "pageUp:"; "moveToBeginningOfLine:"; "moveToBeginningOfParagraph:" "moveToBeginningOfDocument:";
In the above, there are corresponding actions for reverse direction. For example, there are moveLeft and moveRight, moveUp and moveDown, moveWordBackward and moveWordForward, moveToBeginningOfLine and moveToEndOfLine.
Most (or all) of these actions also have a version with suffix “AndModifySelection”. For example, there's moveUpAndModifySelection, moveWordLeftAndModifySelection, moveToBeginningOfLineAndModifySelection, etc. These are the same as moving cursor but also extend the selection.
"scrollLineUp:"; "scrollPageUp:"; "center:";
"delete:"; "deleteBackward:"; "deleteWordBackward:"; "deleteToBeginningOfLine:"; "deleteToBeginningOfParagraph:"; "copy:"; "cut:"; "paste:"; "setMark:"; "yank:"; "deleteToMark:"; "selectToMark:"; "selectWord:"; "selectLine:"; "selectParagraph:"; "selectAll:"; "insertText:"; "insertNewline:"; "insertLineBreak:";
"capitalizeWord:"; "lowercaseWord:"; "uppercaseWord:";
"newDocument:"; "openDocument:"; "checkSpelling:"; "saveDocument:"; "saveDocumentAs:"; "saveAllDocuments:"; "revertDocumentToSaved:"; "printDocument:"; "performZoom:"; "performClose:"; "performMiniaturize:"; "hide:";
You can create a sequence of actions, like this:
"~j" = ("insertText:", "{}", "moveLeft:");
"~k" = ("insertText:", "()", "moveLeft:");
"~l" = ("insertText:", "[]", "moveLeft:");
With the above, typing “Option+j” will insert “{}” then place your cursor inside.
Here's a example of defining the Home/End keys to move to the beginning/end of line.
{
"\UF729" = "moveToBeginningOfLine:"; /* home key */
"\UF72B" = "moveToEndOfLine:"; /* end key */
}
If you like to see a sample keybinding file, look on your hard disk: “/Developer/Applications/Xcode.app/Contents/Resources/PBKeyBinding.dict”. This is used by Xcode to define its keybindings.
Mac OS X by default support emacs's basic keybindings. (Ctrl+f, Ctrl+b, Ctrl+n, Ctrl+p, Ctrl+a, Ctrl+e, Ctrl+k, Ctrl+y) You can use DefaultKeyBinding.dict to further define emacs's C-SPC, C-w, C-x C-x. However, note that emacs's shortcut set is very inefficient and ergonomically painful. (see Why Emacs's Keyboard Shortcuts Are Painful) If you like a efficient keybinding for text editing, see: A Ergonomic Keyboard Shortcut Layout for Text Editing.
The DefaultKeyBinding.dict scheme does not work in all applications, and when they work, they do not work for some key combinations or actions that you might think it should. The following are some reasons and problems.
Not all Cocoa applications will use your new keybindings. Examples of applications that does not use your keybinding (As of 2007-12-12): Itunes (7.5), Finder (10.4.7), TextWrangler (2.2.1), Firefox (2.0.0.11), Opera (9.24), Aquamacs (1.2a), Second Life (1.18.6.0).
Examples of applications i've tested that does use your new keybinding: Apple Mail (2.1), Safari (3.0.4), TextEdit (1.4), Dictionary (1.0.2), iPhoto (5.0.4 (263)), Adium (1.1.4), Colloquy (2.1 (3761)), OmniDictionary (2.0.4 (v16)).
Even in applications that support the DefaultKeyBinding.dict, not necessarily all definitions with special keys work. Here's a quote from Apple doc: NSEvent Class Reference↗: «Note that some function keys are handled at a lower level and are never seen by your application. They include the Volume Up key, Volume Down key, Volume Mute key, Eject key, and Function key found on many iBook and PowerBook computers.»
If you have defined Option+‹key› to insert unicode chars, Keyboard Viewer does not show your new keymap.
The DefaultKeyBinding.dict scheme is not capable of remapping modifier keys. (such as swapping Command with Option, or make right hand Control as Command.) In OS X 10.4, you can switch modifier keys to any other. This is done in System Preferences → Keyboard & Mouse. For more detail, see How to Swap Modifier Keys.
Here are some references and related sites:
Related essays:
