Java keypad event creation. What type of event to create? KeyEvent or ActionEvent?

I'm writing a Java program that will ultimately target an embedded system.

The embedded system will have a physical keypad.

Pressing a button on the physical keypad creates an interrupt which runs the interrupt code which can do... whatever would be appropriate for it to do. (It provides a different number for each keypad button, so we can tell which keypad button was pressed.) Currently I'm writing the emulation code that emulates this keypad.

I'm not sure if I should be generating a KeyEvent, or an ActionEvent, or some other sort of event.

I have a window (JFrame) that emulates the keypad, showing buttons for each key on the keypad.

I have another window (another JFrame) which is the view. This window should react to the keypad buttons in the other window. I expect this view window will have some sort of listener listening for keypad button pressed events.

To further complicate, it would be nice if the emulation program would also respond to the computer keyboard; e.g. the keypad emulation window has Up, Down, Left, Right arrow keys, so pressing the arrow keys on the computer keyboard should do the same thing as clicking the arrow button in the keypad emulation window.

To further confuse, I just realized there are two different ActionEvents in Java
I'm not using javafx.

So my questions are:
what sort of "event" should my keypad emulation window generate?
How can my View Window subscribe to these events?
(Supposedly Keypad knows nothing about View, and View knows nothing about details of Keypad. Probably a Controller knows about both of them and connects them together.)

How can I get the Computer Keyboard Arrow Keys to also effect the same result as the arrow key buttons in the Keypad Emulation Window?

Can you provide a simple example?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
>> what sort of "event" should my keypad emulation window generate?
KeyEvent for sure
>> How can my View Window subscribe to these events?
Your window that emulates the keypad should implement KeyEventDispatcher
Your window that should listen to those KeyEvents should implement KeyListener and should subscribe itself to receive the key events dispatched by the emulating window by calling:
        <your emulating window>.addKeyListener(this);

Open in new window

>> How can I get the Computer Keyboard Arrow Keys to also effect the same result
>> as the arrow key buttons in the Keypad Emulation Window?
By also let the same listening window subscribe itself to the class that generates the "real" key events:
        <your other class generating "real" key events>..addKeyListener(this);

Open in new window

>> Can you provide a simple example?
Sorry, I don't have the time for that. But maybe these can help you:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydAuthor Commented:
Is there another way to set the parent InputMap and ActionMap of controls to be the JPanel they are all on? Or do I have to go through each control separately and manually set the parent InputMap and ActionMap?
int rows = 3;
int cols = 3;

JPanel panel = new JPanel(new GridLayout(rows, cols));


InputMap inputMapPanel = panel.getInputMap();


ActionMap actionMapPanel = panel.getActionMap();


KeyStroke keyUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
KeyStroke keyDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);

inputMapPanel.put(keyUp, "DoSomething");
inputMapPanel.put(keyDown, "DoThis");

Open in new window

zzynxSoftware engineerCommented:
An alternative can be seen in this code of my class EscDialog being a JDialog that closes when ESC is pressed.
But it should react on pressing ESC, no matter what control is currently focused.
Therefor the KeyListener is added recursively. I think that's what you want if I understand your code above well.

import javax.swing.JDialog;
import java.awt.*;
import java.awt.event.*;

 * a JDialog that disappears when ESC is pressed
public class EscDialog extends JDialog implements ContainerListener, KeyListener {

    public EscDialog(Frame frame, String title, boolean modal) {
        super(frame, title, modal);

    public EscDialog(Frame frame, boolean modal) {
        super(frame, modal);

    //The following function is recursive and is intended for internal use only.
    //It is private to prevent anyone calling it from other classes
    //The function takes a Component as an argument and adds this JDialog as a KeyListener to it.
    //Besides it checks if the component is actually a Container and if it is, 
    //there  are 2 additional things to be done to this Container :
    // 1 - add this Dialog as a ContainerListener to the Container
    // 2 - call this function recursively for every child of the Container.

    private void addListenersRecursively(Component c) {
        //To be on the safe side, try to remove KeyListener first 
        //just in case it has been added before.
        //If not, it won't do any harm
        //Add KeyListener to the Component passed as an argument

        if (c instanceof Container) {
            //Component c is a Container. The following cast is safe.
            Container container = (Container) c;
            //To be on the safe side, try to remove ContainerListener first 
            //just in case it has been added before.
            //If not, it won't do any harm
            //Add ContainerListener to the Container.

            //Get the Container's array of children Components.
            Component[] children = container.getComponents();

            //For every child repeat the above operation.
            for (int i = 0; i < children.length; i++) {

    //The following function is the same as the function above with the exception that it does exactly the opposite - 
    // removes this Dialog from the listener lists of Components.
    private void removeListenersRecursively(Component c) {
        if (c instanceof Container) {
            Container container = (Container) c;
            Component[] children = container.getComponents();
            for (int i = 0; i < children.length; i++) {

    //ContainerListener interface

    //This function is called whenever a Component or a Container is added 
    //to another Container belonging to this Dialog

    public void componentAdded(ContainerEvent e) {

    //This function is called whenever a Component or a Container is removed from another Container belonging to this Dialog
    public void componentRemoved(ContainerEvent e) {

    //KeyListener interface
    //This function is called whenever a Component belonging to this Dialog 
    //(or the Dialog itself) gets the KEY_PRESSED event

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            //Key pressed is the ESCAPE key.
    //We need the following 2 functions to complete imlementation of KeyListener

    public void keyReleased(KeyEvent e) {

    public void keyTyped(KeyEvent e) {

    //Default response to ESCAPE key pressed goes here
    public void performEscapeAction(KeyEvent evt) {

Open in new window

deleydAuthor Commented:
Thank you I think I have it now.

For anyone else reading this, I found how to make a class an "ActionTalker" here:

The parts are:

private EventListenerList actionListenerList = new EventListenerList();

      public void addActionListener(ActionListener actionListener) {
            actionListenerList.add(ActionListener.class, actionListener);

      public void removeActionListener(ActionListener actionListener) {
            actionListenerList.remove(ActionListener.class, actionListener);

      protected void fireActionPerformed(ActionEvent actionEvent) {
            EventListener listenerList[] = actionListenerList.getListeners(ActionListener.class);
            for (int i = 0, n = listenerList.length; i < n; i++) {
                  ((ActionListener) listenerList[i]).actionPerformed(actionEvent);
zzynxSoftware engineerCommented:
Thanx 4 axxepting
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.