KeyStrokes example

In this example we are going to see how to work with key strokes in a Java Desktop Application. You might find this useful when you want to bind specific keystrokes with certain behaviors in your app. For example you might want to give the user the opportunity to specify key shortcuts etc.

Basically in order to set up and use key strokes in Java, one should follows these steps:

  • Use KeyStroke.getKeyStroke(String keystroke) to get a KeyStroke object that represent the keystroke you dictated.
  • Create an input component like a JButton and use its getInputMap method to get the InputMap of that component.
  • Use InputMap.put method to bind that keystroke with that component and a specific action.
  • Use JButton1.getActionMap() method to get the ActionMap that is bind with this input device.
  • Use ActionMap.put method to bind a specif action of this component with an ActionListener.

Let’s take a look at the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyTester {

    static class MyActionListener extends AbstractAction {

  MyActionListener(String str) {




  public void actionPerformed(ActionEvent event) {



    public static void main(String args[]) {

  String aKey = "Action";

  JFrame jFrame = new JFrame("Test");

  JButton jbutton1 = new JButton("<html><center>A<br>Focused/Typed");

  JButton jbutton2 = new JButton("<html><center>Ctlr-Z<br>Window/Pressed");

  JButton jbutton3 = new JButton("<html><center>Shift-L<br>Ancestor/Released");

  Container pane = jFrame.getContentPane();

  pane.add(jbutton1, BorderLayout.NORTH);

  pane.add(jbutton2, BorderLayout.CENTER);

  pane.add(jbutton3, BorderLayout.SOUTH);

  KeyStroke keStroke = KeyStroke.getKeyStroke("typed A");

  Action act = new MyActionListener("Action occured!");

  InputMap iMap = jbutton1.getInputMap();

  iMap.put(keStroke, aKey);

  ActionMap actionMap = jbutton1.getActionMap();

  actionMap.put(aKey, act);

  keStroke = KeyStroke.getKeyStroke("ctrl Z");

  act = new MyActionListener("No Action");

  iMap = jbutton2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

  iMap.put(keStroke, aKey);

  actionMap = jbutton2.getActionMap();

  actionMap.put(aKey, act);

  keStroke = KeyStroke.getKeyStroke("shift released L");

  act = new MyActionListener("What Happened?");

  iMap = jbutton3.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

  iMap.put(keStroke, aKey);

  actionMap = jbutton3.getActionMap();

  actionMap.put(aKey, act);

  jFrame.setSize(200, 200);


This was an example on how to work with keystrokes in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

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

Inline Feedbacks
View all comments
Back to top button