Using Java to send key combinations
Asked Answered
S

4

12

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?

Scupper answered 30/1, 2013 at 2:2 Comment(1)
I reviewed the robot for other Java related posts that were obviously referred to awtrobot & edited them. I noticed 2 things in the process. 1) The number of AWT Robot tags increased from 144 to 185. 41 posts out of less than 200 for robot, or at least 20%, were incorrectly tagged. 2) The edit made the total numbers of AWT v. robot swap around. There are now more questions tagged AWT Robot than robot. - I'm thinking maybe I was a little premature in giving you a down-vote because you happened to be one of a very large group of people who apparently made the exact same mistake!Od
P
20

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();
        }
    }

}
Pa answered 30/1, 2013 at 2:7 Comment(0)
V
5

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); 



    }


}
Veg answered 24/1, 2018 at 10:55 Comment(1)
Great answer, very simple and easy to useReliance
T
3

This is an example

Robot r = new Robot();
Thread.sleep(1000);

r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_NUMPAD1);
r.keyPress(KeyEvent.VK_NUMPAD2);
r.keyPress(KeyEvent.VK_NUMPAD3);            
r.keyRelease(KeyEvent.VK_ALT);

Don't forget to release some special keys, it will make some crazy things on your machine

Totalizer answered 30/1, 2013 at 2:7 Comment(0)
N
0

This code is too close to native windows keyboard. Even Api keyboard "presses" are coming into Eclipse ide as those would pressed normally from ide. Keys was produced from current debugged application!! (jdk 1.8, win 7, hp)

Nauru answered 5/3, 2016 at 17:3 Comment(1)
This appears to be a comment directed at another answer... which one?Marozik

© 2022 - 2024 — McMap. All rights reserved.