KeyPress in Java

May 29, 2020
Tutorials
keypress in java

In this tutorial will show you how to build a program in java that has a KeyPress or KeyStroke.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of keyStroke.java.

2. Import the following package library:

import java.awt.event.*; // used to access the ActionEvent clas
import javax.swing.*; //used to access the AbstractAction,Action,ActionMap,InputMap,JButton,JComponent,JFrame, and KeyStroke

3. We will initialize variables in our Main, variable frame as JFrame, button as JButton, and ACTION_KEY as String.

String ACTION_KEY = "theAction";
JFrame frame = new JFrame("KeyStroke Sample"); 
JButton button = new JButton("Press the SpaceBar");

4. Create an ActionEvent to your button and will print “The spacebar button is pressed!”. Have this code below:

Action actionListener = new AbstractAction() {
  public void actionPerformed(ActionEvent actionEvent) {
    JButton source = (JButton) actionEvent.getSource();
    System.out.println("The spacebar button is pressed!");
  }
};

5. To have a space keytroke used the KeyStroke class with the getKeyStroke class.

KeyStroke space = KeyStroke.getKeyStroke(' ');

To provide an action on the button when pressing the spacebar, have this code below:

InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
ActionMap actionMap = button.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
button.setActionMap(actionMap);

6. Finally, add the button, set the size, visibility, and the close operation of the frame. Copy this code below:

frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setVisible(true);

Output:

output


Here’s the full code of this tutorial:

import java.awt.event.*; // used to access the ActionEvent clas
import javax.swing.*; //used to access the AbstractAction,Action,ActionMap,InputMap,JButton,JComponent,JFrame, and KeyStroke
 
public class keyStroke {
  public static void main(String[] a) {
 
    String ACTION_KEY = "theAction";
    JFrame frame = new JFrame("KeyStroke Sample"); 
    JButton button = new JButton("Press the SpaceBar");
 
    Action actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        JButton source = (JButton) actionEvent.getSource();
        System.out.println("The spacebar button is pressed!");
      }
    };
 
    KeyStroke space = KeyStroke.getKeyStroke(' ');
    InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    ActionMap actionMap = button.getActionMap();
    actionMap.put(ACTION_KEY, actionListener);
    button.setActionMap(actionMap);
    frame.getContentPane().add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200);
    frame.setVisible(true);
  }
}
Download Code
Facebook Comments

Leave a Reply

Your email address will not be published.