Home » Desktop Java » awt » event » Simple key press listener

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Simple key press listener

With this simple tutorial we are going to see how to implement a simple key listener for your Java Desktop Application. This is a very nice tool if your application offers rich keyboard activity to the user.

In short, in order to implement a simple key listener in Java, one should perform these steps:

  • Create a new class that extends KeyAdapter class.
  • Override the keyPressed method to customize the handling of that specific event. Now every time the user presses a key this method will be launched.
  • Use KeyEvent.getKeyChar() and KeyEvent.getKeyCode() to find out which key the user pressed.

Let’s take a look at the code snippet that follow:

package com.javacodegeeks.snippets.desktop;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {

    public static void main(String[] argv) throws Exception {

  JTextField textField = new JTextField();

  textField.addKeyListener(new MKeyListener());

  JFrame jframe = new JFrame();

  jframe.add(textField);

  jframe.setSize(400, 350);

  jframe.setVisible(true);

    }
}

class MKeyListener extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent event) {

  char ch = event.getKeyChar();

  if (ch == 'a' ||ch == 'b'||ch == 'c' ) {

System.out.println(event.getKeyChar());

  }

  if (event.getKeyCode() == KeyEvent.VK_HOME) {

System.out.println("Key codes: " + event.getKeyCode());

  }
    }
}

 
This was an example on how to implement a simple key listener in Java.

(-3 rating, 3 votes)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Koios Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Koios
Guest
Koios

Be sure to include
jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
before the end of psvm, otherwise the JFrame will close, but not exit properly.