There are some important gotchas with the other solutions posted here.
For one, symbol and text substitution entries only work in Cocoa apps. If you want a truly system-wide solution that works in all applications, this is not an option.
Also, apps like KeyRemap4MacBook are great, but relying on them means you have to keep the app running in the background all the time, which may not be what you want.
Custom keyboard layouts to the rescue
Luckily, remapping keys can be done in a way that will work for any type of application, and without any additional software!
Mac OS X has supported .keylayout
files since version 10.2 (Jaguar). You can create your own keyboard layout, or rather, tweak the default one you’re using right now. Simply remap a keyboard combination you never use (for me, there are plenty of those) to the ²
and ³
symbols, and that’s it.
In my custom QWERTY keyboard layout, I can simply press ⌥
+ ⇧
+ 2
to enter ²
, and ⌥
+ ⇧
+ 3
to enter ³
. (My custom AZERTY layout has these mappings, too.)
How to create a custom keyboard layout
To create new keyboard layouts or modify existing ones, I’d recommend Ukelele.app. It has an option to create a new keyboard layout based on the one that’s currently in use.
After you’ve created your custom layout, there’s no need for the application anymore — you certainly don’t need to keep it running in the background.
How to install a custom keyboard layout
- Copy the
.keylayout
file to the Keyboard Layouts
folder within ~/Library
(if you want to install it only for the current user) or /Library
(if you want to install the layout system-wide).
- Reboot (if you installed the layout system-wide), or log out and log in again (if you installed it for the current user only).
- Enable the new keyboard layout via System Preferences › Language & Text › Input Sources.
How to make a custom keyboard layout the system default
Optionally, you could make the custom keyboard layout the system default by running the Setup Assistant with root privileges. This way, it will be used for the login screen, and any new user accounts you create will default to this layout as well. Note that this can only be done for keyboard layouts in /Library/Keyboard Layouts
(i.e., layouts that have been installed system-wide).
sudo rm /var/db/.AppleSetupDone; sudo "/System/Library/CoreServices/Setup Assistant.app/Contents/MacOS/Setup Assistant"
You will have to create a new user account in order to complete the Setup Assistant — but don’t worry, you can delete the new account afterwards.
Adding a custom icon to the keyboard layout
OS X will use the following default icon for your custom keyboard layout:
This icon will show up in the preference pane, and in the “Input menu” in the menu bar.
To replace this with your own icon, create a 16×16px image, and save it in .icns
format in the same directory as the keyboard layout itself, using the same file name (only the extension differs). For example, my custom QWERTY layout is named qwerty.keylayout
, so if I wanted to use a custom icon, it’d have to be named qwerty.icns
.
Revisiting this four years later, now with macOS Sierra.
If you have a non-Apple keyboard then you may want to use a more appropriate keyboard mapping. You can select keyboard mappings through the keyboard tab of the System Preferences dialog. Select Input Sources and use the +
button to select a mapping appropriate for your keyboard. (You may also need to use the -
button to delete the mapping that you do not wish to use before your chosen mapping becomes fully effective.)
If you find the mappings provided by Apple to be unsatisfactory (Apple's British - PC keyboard does not map the pipe to the correct key - adjacent to the left-hand shift key) then you may install third-party layouts or create your own.
Here is a ZIP file containing two alternative layouts files for UK 105 key keyboards. You should unzip the files into /Library/Keyboard Layouts
(use sudo
).
The new layouts won't be available in the abovementioned Input Sources until you log out and back in. After that, you can select them like any other layout (they may be listed in the others category).
You can edit your chosen layout if it is still unsatisfactory (I chose the British (PC 105 alt) layout but found that Alt+3
emitted cent (¢) instead of the expected Euro (€) symbol). Layout files are simple XML text files. I identified the relevant key code and replaced its unicode output
character:
<key code="21" output="¢" />
with
<key code="21" output="€" />
(The change was applied in two places: keymap index 3, for anyOption key (Alt
to PC keyboard users), and keymap index 5 for the same with CapsLock on.)
(Once again, a log out/in cycle is required for the change to take effect).
The key code for the 4
key (which, when used with Alt
should produce €
) is 21. Should you wish to discover the key code for a physical key then you could use the Key Codes app, available free of charge from the Apple App Store.
Should you desire a more GUI way of customising your keyboard then you could look at Ukelele [sic] or Karabiner, formerly known as KeyRemap4MacBook (although the website states that Karabiner doesn't currently work on macOS Sierra at the moment.)
Still unresolved I wanted to identify keycodes without installing anything (think X-Windows xev
) and I could not find Apple's standard key layouts (they aren't in /Library/Keyboard Layouts
). I guess they are in /System/Library/Keyboard Layouts
in a different format. It would be nice to be able to use a standard layout as a starting point for custom edits made as described above.
Best Answer
I just bought a Genius keyboard in the Czech Republic and have the same problem, however I've found the following works for me:
Right alt + \
Shift + Right alt + |