I am implementing some keyboard code for an existing java swing application, but I can’t seem to get a keyboard press to execute the “mousePressed” action and “mouseReleased” action that are mapped to a JButton. I have no problems clicking it for the “action_performed” with button.doClick(), is there a similar function for simulating the mouse presses? Thanks beforehand.
I am implementing some keyboard code for an existing java swing application, but I
Share
You can simulate mouse presses and mouse actions by using the Robot class. It’s made for simulation e.g. for automatically test user interfaces.
But if you want to share “actions” for e.g. buttons and keypresses, you should use an
Action. See How to Use Actions.Example on how to share an action for a Button and a Keypress:
Read more about Key-bindings on How to Use Key Bindings.