AWT KeyEvent and KeyListener

KeyEvent and KeyListener

1. AWT KeyEvent and KeyListener

Java AWT tracks Keyboard Event through KeyListener. The KeyListener will get the KeyEvent which discloses data of user interaction with the keyboard. For example, it holds what key is typed by the user. The KeyEvent is raised when the user pressed a keyboard key and it also raised when he/she released it. The Event also tells what key is typed by the user. When these actions take place, AWT reports the KeyEvent to KeyListener. The KeyListener receives the KeyEvent on exposed function which tells what action is done by the user. The handler functions exposed by the KeyListener are below:

  1. keyPressed(KeyEvent e)
  2. keyReleased(KeyEvent e)
  3. keyTyped(KeyEvent e)

Below picture shows the relation between KeyEvent and KeyListener:

KeyEvent and KeyListener
KeyEvent and KeyListener

2. Keyboad Key Categories

The Keyboard keys are categorized in groups. The below picture shows the Keyboard key groups:

Keyboard-Key-Category
Keyboard-Key-Category

Character Keys, and Special Character Keys are known as Char Keys. Shift, Alt and Ctrl keys are called Control Keys. These Control Keys are often used with Char Keys. For Example, a New Document command of a notepad can be invoked by pressing down the Ctrl Key and pressing the ‘N’ Char Key.

Function Keys perform specific action when the user hits it. Most of the keyboards provide 12 such keys. For example, the Windows Explorer invokes file rename operation when user hits the F2 function key after selecting a file or a folder. Toggle Keys shift their state between ON/OFF. When the toggle key is ON, most of the keyboards point out that by glowing the LED allocated to it. Java AWT KeyEvent can capture all these keyboard keys.

3. Keyboard Events – Pressed, Released & Typed

Before we start writing the code to get an insight on KeyEvent and KeyListener, let us understand how Key Pressed, Released and Typed works together. Have a look at the below picture:

Keyboard Key-Press, Key-Release and Key-Typed
Keyboard Key-Press, Key-Release and Key-Typed

First time graph shows that the user pressed the keyboard key ‘a’ and released it at time t seconds. You can see that AWT raises the Key Pressed event first and then Key-Released when the user releases the keyboard key ‘a’. Immediately after raising the Key-Release, AWT also raises one more event called Key-Typed. So, Key-Typed is the combination of Key Press & Release.

The second part of the time graph shows that the user is pressing the keyboard key ‘a’ for few second without releasing it. In this case, AWT will raise the Key-Pressed event periodically till the user releases the key from the pressed state. For example, to scroll a document downward, we end up pressing the down arrow key without releasing it till we reach the required document section. Now let us begin the example. Here we will extend our previous example about AWT Mouse to support Keyboard interactions.

4. Implementing KeyListener

Our Example already implements listeners to receive Mouse Events. Now, we will go ahead with the KeyListener to get the keyboard events. Below is the code:

5. Knowing Typed Keys

The KeyListener will receive the KeyEvent on its handler function keyTyped. AWT calls this handler function when user pressed and released a key in the keyboard. As these two actions complete a key stroke, AWT will treat user typed a key and calls keyTyped.

In the above code, we get the status text from the AWT Label control and append the user typed key in it. The function getKeyChar on the KeyEvent object passed-in will tell what keystroke is performed by the user. Finally, we update the status text with typed key. Note, here we are using the same status label which we used in our previous example: MouseEvents.

6. Control Key Status

One can not type a Control key. So, we can handle it in the keyPressed or keyReleased handler of the AWT KeyListener.

In the above code, we handle both Key Press and Key Release events by writing the keyPressed and keyReleased handler functions. Since, Control Keys are not Char Keys, we use the getKeyCode function from the KeyEvent e. The function getKeyCode returns an integer which we compare against the key constants exposed by the KeyEvent. These key constants start with VK_. In the above code, we used only VK_SHIFT, VK_CONTROL and VK_ALT. But one can use other constants also check a specific key. For example, function key F1 can be checked with the key constant VK_F1.

7. Register with KeyListener

In our early example on Mouse Tracking, we signed up our MouseTestPanel with a couple of AWT Listeners for mouse. Now, we will enroll the same panel with the KeyListener via addKeyListener method so that panel will receive keyboard events as well.

8. Code Reference

8.1 MainEntryAWT.java

8.2 AWTMouseExample.java

8.3 MouseTestPanel.Java

9. AWT Mouse Events Example – Youtube Demo

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.