Search code examples
javaappletkeyboardawtkeypress

Change what keys a Java program thinks are being hit


To keep things short, I want to make a Java applet think that when I hit W, A, S or D I am actually hitting up, down, left, or right respectively.

How would I go about doing this?

I would make a simple little GUI with an activate/deactive button on, but I have no idea how the program would fool the Java applet.


Solution

  • You should be using key bindings.

    Essentially, you "bind" a key to an action. For example, if you want to bind the W key and the UP key to the "pressed" action in button, you'd write:

    button.getInputMap().put(KeyStroke.getKeyStroke("W"), "pressed");
    button.getInputMap().put(KeyStroke.getKeyStroke("UP"), "pressed");
    

    And to define what "pressed" should do, you need to add an action which corresponds to it.

    button.getActionMap().put("pressed", changeTextAction);
    

    changeTextAction should be an instance of a class which extends AbstractAction. For example:

    public class ChangeTextAction extends AbstractAction
    {
        private JButton button;
        private String text;
    
        public ChangeTextAction(JButton button, String text)
        {
            this.button = button;
            this.text = text;
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
            button.setText(text);
        }
    }
    

    Here's an example of a basic program which allows the user to either click, press W, or press UP to trigger an action which changes its text to "Pressed!":

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    
    class KeyBindingExample extends JFrame
    {
        private JButton button;
        private Action changeTextAction;
    
        public KeyBindingExample()
        {
            button = new JButton("Not Pressed!");
            changeTextAction = new ChangeTextAction(button, "Pressed!");
    
            button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "pressed");
            button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("UP"), "pressed");
    
            button.getActionMap().put("pressed", changeTextAction);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().add(button);
            pack();
            setVisible(true);
        }
    
        public class ChangeTextAction extends AbstractAction
        {
            private JButton button;
            private String text;
    
            public ChangeTextAction(JButton button, String text)
            {
                this.button = button;
                this.text = text;
            }
    
            @Override
            public void actionPerformed(ActionEvent e)
            {
                button.setText(text);
            }
        }
    
        public static void main(String[] args)
        {
            new KeyBindingExample();
        }
    }