Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java to send key combinations

As per this previous link (How to send keyboard outputs) Java can simulate a key being pressed using the Robot class. However, how could a combination of key presses be simulated? If I wanted to send the combination "alt-123" would this be possible using Robot?

like image 916
Muhammad Khan Avatar asked Jan 30 '13 02:01

Muhammad Khan


People also ask

What is keypress event in Java?

An event which indicates that a keystroke occurred in a component. This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed.

How do I trigger a JButton with a key press?

How to trigger a JButton with a key press? Add an ActionListener to the button. It will fire an event when the button is in focus and the user presses the enter key.

What is robot in Java?

The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue.


2 Answers

The simple answer is yes. Basically, you need to wrap the keyPress/Release of the Alt around the other keyPress/Releases

public class TestRobotKeys {

    private Robot robot;

    public static void main(String[] args) {
        new TestRobotKeys();
    }

    public TestRobotKeys() {
        try {
            robot = new Robot();
            robot.setAutoDelay(250);
            robot.keyPress(KeyEvent.VK_ALT);
            robot.keyPress(KeyEvent.VK_1);
            robot.keyRelease(KeyEvent.VK_1);
            robot.keyPress(KeyEvent.VK_2);
            robot.keyRelease(KeyEvent.VK_2);
            robot.keyPress(KeyEvent.VK_3);
            robot.keyRelease(KeyEvent.VK_4);
            robot.keyRelease(KeyEvent.VK_ALT);
        } catch (AWTException ex) {
            ex.printStackTrace();
        }
    }

}
like image 135
MadProgrammer Avatar answered Oct 08 '22 04:10

MadProgrammer


For sending keys combination using java.awt.Robot the following code works fine for me

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;



public class VirtualKeyBoard extends Robot
{

    public VirtualKeyBoard() throws AWTException
    {
        super();
    }

    public void pressKeys(String keysCombination) throws IllegalArgumentException
    {
            for (String key : keysCombination.split("\\+"))
            {
                try
                {   System.out.println(key);
                    this.keyPress((int) KeyEvent.class.getField("VK_" + key.toUpperCase()).getInt(null));

                } catch (IllegalAccessException e)
                {
                    e.printStackTrace();

                }catch(NoSuchFieldException e )
                {
                    throw new IllegalArgumentException(key.toUpperCase()+" is invalid key\n"+"VK_"+key.toUpperCase() + " is not defined in java.awt.event.KeyEvent");
                }


            }


    }


    public void releaseKeys(String keysConbination) throws IllegalArgumentException
    {

            for (String key : keysConbination.split("\\+"))
            {
                try
                { // KeyRelease method inherited from java.awt.Robot
                    this.keyRelease((int) KeyEvent.class.getField("VK_" + key.toUpperCase()).getInt(null));
                } catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                }catch(NoSuchFieldException e )
                {
                    throw new IllegalArgumentException(key.toUpperCase()+" is invalid key\n"+"VK_"+key.toUpperCase() + " is not defined in java.awt.event.KeyEvent");
                }
            }


    }

    public static void main(String[] args) throws AWTException
    {


         VirtualKeyBoard kb = new VirtualKeyBoard();


         String keyCombination = "control+a"; // select all text on screen
         //String keyCombination = "shift+a+1+c"; // types A!C on screen

         // For your case 
         //String keyCombination = "alt+1+2+3";


         kb.pressKeys(keyCombination);
         kb.releaseKeys(keyCombination); 



    }


}
like image 39
Umer Farooq Avatar answered Oct 08 '22 05:10

Umer Farooq