The JComponent class supports key bindings as a way of responding to individual keys typed by a user. Here are some examples of when key bindings are appropriate: You're creating a custom component and want to support keyboard access to it. Visit Mac files with native Mac app in dired; Using the GUI Emacs.app from the. Emacs keybindings in Cocoa Apps; Emacs keybindings in Terminal; Emacs. There is also a script (emacs/mac/make-package) to create a Mac OS X. Turn on anti-aliasing (default) (setq mac-allow-anti-aliasing nil);; turn off anti-aliasing.
Note: To define special reactions to particular keys, use key bindings instead of a key listener. For further information, see. Notifications are sent about two basic kinds of key events:. The typing of a Unicode character. The pressing or releasing of a key on the keyboard The first kind of event is called a key-typed event. The second kind is either a key-pressed or key-released event.
In general, you react to only key-typed events unless you need to know when the user presses keys that do not correspond to characters. For example, to know when the user types a Unicode character — whether by pressing one key such as 'a' or by pressing several keys in sequence — you handle key-typed events. On the other hand, to know when the user presses the F1 key, or whether the user pressed the '3' key on the number pad, you handle key-pressed events. Note: To fire keyboard events, a component must have the keyboard focus. To make a component get the keyboard focus, follow these steps:.
Make sure the component's isFocusable method returns true. This state allows the component to receive the focus.
For example, you can enable keyboard focus for a JLabel component by calling the setFocusable(true) method on the label. Make sure the component requests the focus when appropriate. For custom components, implement a mouse listener that calls the requestFocusInWindow method when the component is clicked.
Component.setFocusTraversalKeysEnabled(false) on the component that is firing the key events. Your program must then handle focus traversal on its own. Alternatively, you can use the class to pre-listen to all key events. The has detailed information on the focus subsystem. You can obtain detailed information about a particular key-pressed event.
For example, you can query a key-pressed event to determine if it was fired from an action key. Examples of action keys include Copy, Paste, Page Up, Undo, and the arrow and function keys. You can also query a key-pressed or key-released event to determine the location of the key that fired the event. Most key events are fired from the standard keyboard, but the events for some keys, such as Shift, have information on whether the user pressed the Shift key on the left or the right side of the keyboard. Likewise, the number '2' can be typed from either the standard keyboard or from the number pad.
For key-typed events you can obtain the key character value as well as any modifiers used. Try this:. Click the Launch button to run KeyEventDemo using.
Alternatively, to compile and run the example yourself, consult the. Type a lowercase 'a' by pressing and releasing the A key on the keyboard.
The text field fires three events: a key-pressed event, a key-typed event, and a key-released event. Note that the key-typed event doesn't have key code information, and key-pressed and key-released events don't have key character information. None of the events so far are from modifier or action keys and the key location, reported on the key-pressed and key-released events, is most likely standard. Press the Clear button. You might want to do this after each of the following steps. Press and release the Shift key.
The text field fires two events: a key-pressed and a key-released. The text field doesn't fire a key-typed event because Shift, by itself, doesn't correspond to any character. Type an uppercase 'A' by pressing the Shift and A keys.
You'll see the following events, although perhaps not in this order: key-pressed (Shift), key-pressed (A), key typed ('A'), key-released (A), key-released (Shift). Note that Shift is listed as the modifier key for the key-typed and key-pressed events. Type an uppercase 'A' by pressing and releasing the Caps Lock key, and then pressing the A key.
You should see the following events: key-pressed (Caps Lock), key-pressed (A), key typed ('A'), key-released (A). Note that Caps Lock is not listed as a modifier key. Press the Tab key.
No Tab key-pressed or key-released events are received by the key event listener. This is because the focus subsystem consumes focus traversal keys, such as Tab and Shift Tab. Press Tab twice more to return the focus to the text area. Press a function key, such as F3. You'll see that the function key is an action key. Press the left Shift key, followed by the right Shift key. The key-pressed and key-released events indicate which Shift key was typed.
Press the Num Lock key if your keyboard has a number pad. As for Caps Lock, there is a key-pressed event, but no key-released event. Press the '2' key on the number pad. You see the key-pressed, key-typed, and key-released events for the number '2'. Press the '2' key on the standard keyboard.
Again, you see the three event messages. The key-typed events for both number 2 keys are identical. But the key-pressed and key-released events indicate different key codes and different key locations. Press the Num Lock key again. A key-released event is fired. You can find the example's code in.
Here is the demo's key event handling code.