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:
- keyPressed(KeyEvent e)
- keyReleased(KeyEvent e)
- keyTyped(KeyEvent e)
Below picture shows the relation between KeyEvent and KeyListener:

2. Keyboad Key Categories
The Keyboard keys are categorized in groups. The below picture shows the Keyboard key groups:

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:

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:
1 2 3 4 |
//Sample 08: Implement KeyListener public class MouseTestPanel extends Panel implements MouseMotionListener, MouseListener, KeyListener { |
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.
1 2 3 4 5 6 7 |
@Override public void keyTyped(KeyEvent e) { //Sample 10: Updated Typed Char String out = Status.getText(); out += " " + e.getKeyChar(); Status.setText(out); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@Override public void keyPressed(KeyEvent e) { //Sample 11: Set the Control Key Status int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_CONTROL) CtrlOn = true; if (KeyCode == KeyEvent.VK_ALT) AltOn = true; if (KeyCode == KeyEvent.VK_SHIFT) ShiftOn = true; } @Override public void keyReleased(KeyEvent e) { //Sample 12: Set the Control Key Status int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_CONTROL) CtrlOn = false; if (KeyCode == KeyEvent.VK_ALT) AltOn = false; if (KeyCode == KeyEvent.VK_SHIFT) ShiftOn = false; } |
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.
1 2 3 4 |
//Sample 10: Register with Mouse Move panel.addMouseListener(panel); panel.addMouseMotionListener(panel); panel.addKeyListener(panel); |
8. Code Reference
8.1 MainEntryAWT.java
1 2 3 4 5 6 7 8 9 10 11 |
package AwtDemoPkg; import java.awt.Frame; public class MainEntryAwt { public static void main(String[] args) { //Sample 01: Make the FrameWindow Visible AWTMouseExample fw = new AWTMouseExample("JSplitPane Example"); fw.setVisible(true); } } |
8.2 AWTMouseExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class AWTMouseExample extends Frame implements WindowListener { public AWTMouseExample(String FrameTitle) { //Sample 02: Set Frame Window Properties super(FrameTitle); setSize(400, 300); setLocation(100, 100); addWindowListener(this); //Sample 09: Create Mouse Test Panel Label StatusLabel = new Label(); MouseTestPanel panel = new MouseTestPanel(StatusLabel); panel.setBackground(Color.gray); add(panel); add(BorderLayout.SOUTH, StatusLabel); //Sample 10: Register with Mouse Move panel.addMouseListener(panel); panel.addMouseMotionListener(panel); } public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { this.dispose(); } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } |
8.3 MouseTestPanel.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
package AwtDemoPkg; import java.awt.Dimension; import java.awt.Label; import java.awt.Panel; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; //Sample 01: Implement MouseListeners //Sample 06: Implement MouseMotionListener //Sample 08: Implement KeyListener public class MouseTestPanel extends Panel implements MouseMotionListener, MouseListener, KeyListener { //Sample 02: Ctor Label Status; boolean CtrlOn = false; boolean AltOn = false; boolean ShiftOn = false; public MouseTestPanel(Label status) { super(); this.Status = status; setPreferredSize(new Dimension(300,200)); } //Sample 07: Motion Handler @Override public void mouseDragged(MouseEvent e) { Status.setText( "Mouse Draggged. (" + e.getX() + "," + e.getY() + ")"); } @Override public void mouseMoved(MouseEvent e) { Status.setText( "Mouse Moved. (" + e.getX() + "," + e.getY() + ")"); } @Override public void mouseClicked(MouseEvent e) { //Sample 04: Check which Mouse Button Clicked //Sample 14: Use the MouseEvent to Know the Button Click String out = ""; if(e.getButton() == MouseEvent.BUTTON1) out += "Left Mouse Clicked "; if(e.getButton() == MouseEvent.BUTTON2) out += "Middle Mouse Clicked "; if(e.getButton() == MouseEvent.BUTTON3) out += "Right Mouse Clicked "; //Sample 05: Get the Location out += "@ " + e.getX() + "," + e.getY(); //Sample 13: Update the Key Status String KeyState = "with "; if (CtrlOn) KeyState += " Ctrl"; if (AltOn) KeyState += " Alt"; if (ShiftOn) KeyState += " Shift"; if (CtrlOn || AltOn || ShiftOn ) out += KeyState + " Key Pressed"; Status.setText(out); } @Override public void mousePressed(MouseEvent e) { Status.setText("Mouse Pressed"); } @Override public void mouseReleased(MouseEvent e) { Status.setText("Mouse Released"); } @Override public void mouseEntered(MouseEvent e) { Status.setText("Mouse Entered"); } @Override public void mouseExited(MouseEvent e) { Status.setText("Mouse Exits"); } @Override public void keyTyped(KeyEvent e) { //Sample 10: Updated Typed Char String out = Status.getText(); out += " " + e.getKeyChar(); Status.setText(out); } @Override public void keyPressed(KeyEvent e) { //Sample 11: Set the Control Key Status int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_CONTROL) CtrlOn = true; if (KeyCode == KeyEvent.VK_ALT) AltOn = true; if (KeyCode == KeyEvent.VK_SHIFT) ShiftOn = true; } @Override public void keyReleased(KeyEvent e) { //Sample 12: Set the Control Key Status int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_CONTROL) CtrlOn = false; if (KeyCode == KeyEvent.VK_ALT) AltOn = false; if (KeyCode == KeyEvent.VK_SHIFT) ShiftOn = false; } } |
9. AWT Mouse Events Example – Youtube Demo
Categories: AWT
Tags: getKeyChar, getKeyCode, KeyEvent, KeyListener, keyPressed, keyReleased, keyTyped