Mac: How to Create Keyboard Layout and Keybinding
This page tells you how to modify Mac OS X's keybinding.
You can:
- Define your own keys for cursor movement and editing commands.
- Define 【⌥ option+letter】 key to insert your favorite unicode characters or math symbols. 〔➤see Unicode Characters ☯ ☭ ⚡ ∑ ♀ ♂ ♥ 😄〕
Key Config File
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.
The file's content should be key & action pairs, like this:
/* this is comment */
{
"keycode1" = actionCode1;
"keycode2" = actionCode2;
}
- Keycode is a string that represents key press.
- actionCode represents what to do.
Simple Example
Create a file at ~/Library/KeyBindings/DefaultKeyBinding.dict, and put the following content.
/* -*- coding: utf-8 -*- */
/* my keybindings */
{
/* insert Unicode character with Option key down*/
"~a" = ("insertText:", "\U03B1"); /* greek alpha α */
"~;" = ("insertText:", "\U2665"); /* heart symbol ♥ */
/* move cursor with i j k l keys while Ctrl key down */
"^i" = ("moveUp:");
"^k" = ("moveDown:");
"^j" = ("moveLeft:");
"^l" = ("moveRight:");
}
Restart a application and the new keybinding will take effect in that application. Launch TextEdit to test your changes.
Note: Only applications that uses Cocoa Text System will support this.
Note: if you use Unicode Characters directly in the file such as →, you must save the file using UTF-8 encoding.
Key Syntax
Mac OS X Keybinding Key Syntax
Action Code
Mac OS X Keybinding Action Code
Sample File
Example of defining the {↖ Home, ↘ End} keys to move to the beginning/end of line.
/* make home/end key to move to begin/end of line */
{
"\UF729" = "moveToBeginningOfLine:"; /* home key */
"\UF72B" = "moveToEndOfLine:"; /* end key */
}
Here's example file for inserting Unicode characters with the ⌥ option key.
/* -*- coding: utf-8 -*- */
{
/* insert Unicode characteres with Option key down */
"~#8" = ("insertText:", "↑");
"~#2" = ("insertText:", "↓");
"~#4" = ("insertText:", "←");
"~#6" = ("insertText:", "→");
"~a" = ("insertText:", "α");
"~h" = ("insertText:", "θ");
"~3" = ("insertText:", "†");
"~7" = ("insertText:", "—");
"~8" = ("insertText:", "•");
"~9" = ("insertText:", "★");
"~&" = ("insertText:", "‣");
"~*" = ("insertText:", "°");
/* insert pairs with Option down */
"~d" = ("insertText:", "«»", "moveBackward:");
"~h" = ("insertText:", "{}", "moveBackward:");
"~t" = ("insertText:", "()", "moveBackward:");
"~n" = ("insertText:", "[]");
"~s" = ("insertText:", "“”", "moveBackward:");
"~-" = ("insertText:", "「」", "moveBackward:");
"~D" = ("insertText:", "‹›", "moveBackward:");
"~S" = ("insertText:", "‘’", "moveBackward:");
"~_" = ("insertText:", "『』", "moveBackward:");
/* insert sig */
"~1" = ("insertText:", " John\n http://example.com/\n\n☄");
}
Here's a example of ErgoEmacs Keybinding, one for QWERTY layout and one for Dvorak Keyboard Layout:
You can look at Xcode's keybinding file at
/Developer/Applications/Xcode.app/Contents/Resources/PBKeyBinding.dict.
You can view it here: osx_keybinding_xcode.dict.txt.
Emacs Keybinding
Mac OS X by default support emacs' keybindings. They are:
| Key | Action |
|---|---|
| 【Ctrl+f】 | move forward |
| 【Ctrl+b】 | move backward |
| 【Ctrl+n】 | move down a line |
| 【Ctrl+p】 | move up a line |
| 【Ctrl+a】 | beginning of line |
| 【Ctrl+e】 | end of line |
| 【Ctrl+k】 | delete current position to end of line |
| 【Ctrl+y】 | paste |
You can add more of emacs's
| Key | Action |
|---|---|
| 【Ctrl+space】 | set mark |
| 【Ctrl+w】 | cut |
| 【Ctrl+x Ctrl+x】 | Swap cursor position to last mark |
However, i don't recommend it. Emacs's keys is very inefficient and ergonomically painful. See: Why Emacs's Keyboard Shortcuts are Painful. If you like a efficient keybinding for text editing, you might try: ErgoEmacs Keybinding.
Problems
See: Problems of Mac OS X's Keybinding Scheme DefaultKeyBinding.dict.
Alternatives
Something DefaultKeyBinding.dict can't do. For example:
- It cannot remap keys.
- It can't set a key such as F8 to type other keys such as 【⌘ command+c】.
- It can't set a key to launch a app or script.
There are many solutions to these. See:
Mac: Key Remapping, Keybinding Tools
Best is to get a programable keyboard. I can recommend Logitech G910 Orion Spark Keyboard. May look ugly, but is functionally the best keyboard. Quiet mechanical keys.
References
- Text System Defaults and Key Bindings By Apple. @ https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/TextDefaultsBindings/TextDefaultsBindings.html
- Apple Doc. NSResponder Class Reference By Apple. @ https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/index.html
- Customizing the Cocoa Text System By Jacob Rus. @ http://www.hcs.harvard.edu/~jrus/Site/cocoa-text.html
- Technical Note TN2056: Installable Keyboard Layouts @ https://developer.apple.com/library/mac/technotes/tn2056/_index.html
Note: the default keybinding config file used by the system is a XML file at: /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict. You don't have to do anything with that file. This is for your information only.