Words on Macs: 2 Every Keystroke Counts

The floating Keyboard Viewer shows you the input characters available from your current Keyboard Layout; hold modifier keys down to see more characters.

The first article in this series examined the text format used in OS X, Unicode. This second article considers how that text is entered into your Mac: mainly by the keyboard, but I will also briefly consider speech and scanned input.

How do my keystrokes get into an app?

When you press a key, or a combination of keys, the processor in your keyboard sends a virtual key code to OS X informing it of the key(s) that have been pressed. If you continue to hold the key(s) down, then there will be a steady stream of those codes sent to OS X until you release them. For example, on standard English keyboards, the virtual key code sent when you press the key marked E is 14, which OS X will normally (when an English keyboard layout is active) convert to the character e.

OS X then maps the virtual key code sent to it, to an output string in Unicode. Associated with that is information on which modifier keys were pressed. Standard modifier keys (which can be detected separately from one another) are:

  • left Shift
  • right Shift
  • left Option/alt
  • right Option/alt
  • left Control
  • right Control
  • Command
  • Caps Lock

Keystrokes with modifier keys are special, as they are used as shortcuts for various actions. OS X examines these first, to see whether there are any system-level actions triggered, such as when you press Command-Option-Escape together: that causes OS X to bring up the Force Quit dialog. You can also set up your own shortcuts in the Keyboard pane, in the Shortcuts tab. Only when a particular key code and modifier combination has no higher meaning is it passed through to the app to handle. Modifier keys can be mapped for a particular keyboard in the Keyboard tab of the Keyboard pane, clicking on the Modifier Keys… button.

Set up keystroke shortcuts for different circumstances in the Shortcuts tab of the Keyboard pane.
Set up keystroke shortcuts for different circumstances in the Shortcuts tab of the Keyboard pane.
Modifier keys are set by clicking on the Modifier Keys... button.
Modifier keys are set by clicking on the Modifier Keys… button.

Some key codes are used in dead keys: these are keys or combinations of keys (such as Opt-e) which do not map to a character, but instead are used to modify the character output of another key, typically to generate an accented character. So if you press Opt-e and then a plain e, you will normally see é output as a result of the dead key modification.

Set repeat features and configure modifier keys in the Keyboard tab of the Keyboard pane.
Set repeat features and configure modifier keys in the Keyboard tab of the Keyboard pane.

Use the Keyboard tab in the Keyboard pane to set the delay before a pressed key repeats, and the rate at which it repeats. If you are prone to holding keys down too long, you can turn repeat off altogether. If you find it difficult to hold modifier keys down whilst pressing another key, then the Keyboard tool in the Accessibility pane lets you enable a ‘sticky keys’ feature which should help. There are other tools in that pane which enable support for specialist input devices of different kinds.

Problem: You notice that one or more keys on your keyboard are no longer working reliably, either not producing any characters at all, or producing the wrong characters. Can you fix that?
Solution: First check your selected keyboard layout in the Keyboard pane, and that you have not configured any shortcuts that might be interfering. Try a Safe boot, with the Shift key held, to disable other extensions etc. which might be messing things up. Then rule out mechanical issues with your keyboard: swap for a different one, wired for wireless. If it looks like a dying key switch, give the keyboard a good clean without wetting, using a little alcohol-based cleaner.
Tools: Keyboard pane in System Preferences; wired USB keyboard; alcohol-based keyboard cleaner.

Problem: I have just spilt coffee on my keyboard. Can I rescue and recover it?
Immediate actions: Disconnect the keyboard from power as quickly as possible. If this is an internal keyboard in a laptop, shut the laptop down, disconnect mains power, and remove the battery as quickly as you can, leaving the laptop upside down to ensure any remaining liquid drains out rather than inside the laptop. If a wireless keyboard, remove the batteries as quickly as you can. If a wired keyboard, unplug the USB cable.
Once the power has been removed, put the keyboard upside down, with the keys facing down, to allow all liquid to drain.
Recovery: Determine whether any liquid has got into the keys and most importantly the switches under them. If it has, and the liquid was not water but contained coffee/tea/etc. and most particularly sugar, then you may need to clean the liquid from the keyboard before it will work properly again. You may be able to get away with using a damp paper towel just to clean small amounts away, and leaving it to dry thoroughly.
However if the keyboard has been fully wetted, it may need to be rinsed thoroughly in clean, preferably de-ionised, water. Cold tap water can be OK for this, but it is worth giving it a final rinse with de-ionised or filtered water if you can. Ensure that all residues are removed fully.
Then dry the keyboard thoroughly: gentle heat up to about 40˚C and a couple of days or more can be ideal, or you can put the keyboard in dried rice or special silica drying bags. When you are certain the keyboard is dry, clean using an alcohol-based cleaner, and try the keyboard out. If keys remain sticky, or the keyboard does not work when powered up and connected, it is likely to need replacement.
Tools: Sink and running cold water; de-ionised or filtered water; gentle heat and/or rice or other drier such as silica gel bags; alcohol-based keyboard cleaner.

How are keyboards customised for different languages? Can I design my own? How do I switch between them?

Key mapping – the process of converting virtual key codes to a Unicode string – depends on the keyboard layout which you currently have active. Keyboard layouts are stored in the folder named Keyboard Layouts in your active Library folders: system-level layouts are kept in a bundle in /System/Library/Keyboard Layouts, and should not be tampered with. Those which you can customise are in /Library/Keyboard Layouts (for all users) and ~/Library/Keyboard Layouts (for your use only).

Over the years OS X has supported various formats for Keyboard Layouts. You may still come across ancient Keyboard Suitcases, with the extension .rsrc (as they are a resource file), and Localised Keyboard Suitcase Bundles (.bundle). However the preferred format now is an XML Keyboard Definition (.keylayout), perhaps contained in a Localised XML Keyboard Bundle (.bundle).

Ukelele lets you design your own custom Keyboard Layouts for different physical keyboards and languages.
Ukelele lets you design your own custom Keyboard Layouts for different physical keyboards and languages.

You can create your own custom keyboards using a keyboard editor such as Ukelele, free from SIL. Helpful notes on this are at OS X Notes.

Select the Keyboard Layouts to be made available in the Input menu in the Input Sources tab of the Keyboard pane.
Select the Keyboard Layouts to be made available in the Input menu in the Input Sources tab of the Keyboard pane.

Once you have installed the Keyboard Layout correctly, you should be able to add it to the list of available Keyboard Layouts in the Input Sources tab of the Keyboard pane. Simply click on the + button to add it to the list on the left, and you can order the list by dragging items up and down. Enable the Input Menu by checking the box at the foot, and you will be able to switch keyboard layouts through that item in the menubar.

How do I insert unusual characters into a document?

The floating Keyboard Viewer shows you the input characters available from your current Keyboard Layout; hold modifier keys down to see more characters.
The floating Keyboard Viewer shows you the input characters available from your current Keyboard Layout; hold modifier keys down to see more characters.

If you want to try to do this directly from the keyboard, first open the Keyboard Viewer from the Input Menu. This shows the complete layout of characters available from the current Keyboard Layout. Hold modifier keys such as Shift, Option, and Control in combination to see the full range of available output, including any dead keys.

If the character that you want is not available in your current Keyboard Layout, and you are likely to use it a lot, you can switch to a layout which does include the character. For example, if you are typing plenty of French words with accented characters, you may find it easier to enable a French AZERTY keyboard and switch to that for the time being. This is particularly useful if you are used to such a different layout, but can be baffling and error-prone if you are not. If you use a different language a great deal, consider buying a localised keyboard for that language, either from Apple or a third party.

If you know the Unicode codes for the characters, you can switch to a special Keyboard Layout which takes those codes. Most users find that too ponderous, but there are times when it is invaluable.

The Character Viewer allows you to insert any Unicode character into a document, provided that you have a font to display it.
The Character Viewer allows you to insert any Unicode character into a document, provided that you have a font to display it.

To insert more unusual characters, it is often best to use the Character Viewer from the Input Menu. There you can navigate different character sets and locate exactly the right character to use. With the insertion point correctly set in your document, double-click on the character you wish to insert in the Character Viewer. It should then be inserted into your document. However a common problem arising from this is when the font that you are using does not contain that Unicode character: you may need to change font to DejaVu or similar to see the character correctly.

Can I mix languages which run in different directions, such as English and Arabic, in the same document?

In addition to Keyboard Layouts which determine how you enter characters from your keyboard, language settings also determine a complete script environment, which includes the direction in which those characters run across the page. Set those up in the Language & Region pane. Some apps work better than others with mixed script systems such as English and Arabic. One word processor which has always specialised in supporting multiple mixed scripts and languages is Nisus Writer Pro.

Can I dictate and have my words converted to text automatically?

Enable Dictation so that what you speak into your microphone is converted to text input.
Enable Dictation so that what you speak into your microphone is converted to text input.

The Dictation & Speech pane supports this, and can be a boon if you are not able to use a keyboard, either because your hands are otherwise occupied, or because of a medical issue such as RSI. Some users struggle though to achieve worthwhile accuracy, forcing them to spend a long time checking and correcting dictated documents. You will need to experiment with enunciation, articulation, and speed to find the sweetest spot for your particular voice, and it may be worth trying out some different microphones to see which works best for you.

Can I scan text documents in and have their content automatically converted to text?

There is currently no direct support for Optical Character Recognition (OCR) in OS X, and you will need third party applications to achieve this. As with dictation, results are not perfect, varying between scan resolution, type of input print, language, and words recognised (or not). The bundled Paper Capture plug-in in Adobe Acrobat Pro offers this, as do several apps available from the App Store and directly from vendors. ABBYY FineReader OCR Pro (£89.99) is notable for its high accuracy and support for many different languages, but there are several other excellent contenders, including Prizmo 3 (£39.99 plus in-app purchases). Search the App Store using OCR to see them all.

If you need to scan and convert many documents, then consider getting a scanner designed to make this easier and quicker. Although they can be expensive, those with automatic sheet feeders can make bulk entry of loose leaf documents much more efficient. You should also consider getting this done by a specialist service, which can be cheaper if you only need to enter a large batch once.

Additional resources

Keyboard Layouts are detailed in Apple Technical Note TN2056.
How to change the keyboard layout used for the login window, Apple Support.
Alan Wood’s Unicode resources.
The Multilingual Mac Blog.
How to use a Dvorak Keyboard Layout at ATMac.