What is the VK_[key] code for the command key on a mac, if one exists? I am trying to get a Robot (java Robot) to press the command key. I am using the command keyPress(), and I need to know the integer keycode for the command key on a mac.
KeyEvent.VK_META, with key code 157, is Java's virtual key that maps to the the Mac command key.
KeyEvent.VK_META can be used as COMMAND button in Mac OS.
if its not working with you, that is because you need to add a delay
sample code for opening a new tab
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.delay(200);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_T);
there is also isMetaDown() method, which in my case works if somebody wants to use the shortcuts for copy/paste text and so on.
Sample code:
public void keyPressed(KeyEvent e) {
if (e.isMetaDown() && (e.getKeyCode() == KeyEvent.VK_V) && readonly.isSelected()){
e.consume();
}
}
Related
I am currently experimenting with java awt robot and now I wanted to try to press something inside my game window.
For steam.exe, I put the following properties:
"compatibility to Windows 7",
"always run as administrator".
Then, I launched the game "Counter-Strike: Global Offensive" with this .bat file:
#echo off
start "" "D:\Program Files (x86)\Steam\Steam.exe" -login username password -applaunch 730 -low -nohltv -nosound -novid -window -w 400 -h 300 +exec autoexec.cfg -x 0 -y 0
The game launched and now I wanted to click something in the game with the following code:
public static void keyStroke(int x, int y) throws AWTException
{
Robot robo = new Robot();
robo.mouseMove(x, y);
delay(1000);//milliseconds
robo.mousePress(InputEvent.BUTTON1_DOWN_MASK);
delay(100);
robo.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
Here is a picture of the game:
The mouse goes to the correct location but when it should be clicking something, the game does not respond or recognize it.
However, if I do not put steam.exe in windows 7 compatibility, it works...
Why does robot not work anymore if the game is in windows 7 compatibility mode ?
Unfortunaetely I need to find a fix for this because I can only run steam in win7 comp.
Are there ways to fix it?
Okay I needed to run eclipse as administrator. My bad
I've been reading up on this and haven't found an answer that has made sense to me.
I'm trying to write a program in Java to interact with an application to see if I can write a program to play a video game for me. The game is on my computer.
Here is an excerpt of code:
public static void main(String[] args) throws java.io.IOException {
Runtime run = Runtime.getRuntime();
run.exec("open /Applications/OpenEmu.app");
try {
Robot robot = new Robot();
System.out.println("Waiting 5 Seconds");
//robot.delay(5000);
System.out.println("Pressed X");
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_X);
//Starts an easy mode game
It opens the application fine, and in something like notepad, it will type XXXX, but it won't do so for the game?
I've assigned the 'x' key on my keyboard as a command button for the game. My guess is that the 'x' press is internal. All help is appreciated!
If you are trying to simulate input, try add robot.keyRelease as well. Javadoc for robot says for keyPress "Presses a given key. The key should be released using the keyRelease method."
System.out.println("Pressed X");
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
...
Also remember this:
https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html "Note that some platforms require special privileges or extensions to access low-level input control. "
So I have the following code:
public static void main(String[] args) throws AWTException, InterruptedException {
Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(40);
// This works fine
robot.mouseMove(40, 130);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
// The 'a' is never inputted
robot.keyPress(KeyEvent.VK_A);
robot.delay(200);
robot.keyRelease(KeyEvent.VK_A);
}
This code successfully moves the mouse to the location 40, 130 and clicks. I make sure to have a text editor open in this location so that it becomes focused.
The next bit of code is the issue. The keyPress/Release snippet works perfectly fine for other codes. (Like 100, corresponding to the number 4.) But for some reason the letter 'a' will not be printed into the text editor.
I have tried having the program continually loop and print a for ~5 seconds. After the programatic click it will not print 'a' in the text editor. If I click on the editor again myself during this time, the string of 'a's will then start to appear.
What is causing this behavior and how can I fix it?
As i assumed in my comments i think you have a problem focusing the editor correctly.
You can try to use the Windows solution by doing ALT+TAB and then release it to select the editor.
Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(40);
robot.keyPress(KeyEvent.VK_ALT);//on mac use VK_META
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_ALT);//on mac use VK_META
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_A);
robot.delay(200);
robot.keyRelease(KeyEvent.VK_A);
I am pressing some keys on keyboard(and buttons on mouse) remotely using socket and java Robot class.
Homebrew program is running on my Nintendo Wii.(but in here, I am using emulator)
Wii sends Wii remote button info through socket, and java program receives it and presses key or controls mouse.
Everything seems working well, except for Windows key.
It receives key press as well as home button.(and PC should open windows menu when I press home button)
So, I think the problem is in pressing key.
I press the key using this method.
if Press is true It presses the key. Otherwise, It releases the key.
private static void PressKey(Robot R, int Key, boolean Press)
{
if(Press)
R.keyPress(Key);
else
R.keyRelease(Key);
}
And I called like this:
PressKey(rBot, KeyEvent.VK_WINDOWS, BH);
rBot is:
Robot rBot = new Robot();
BH is true when home button is pressed, false when home button is not pressed
How can I fix this?
This bug is known for years, yet is is still present in Java 1.7.0_25 version which I'm using on Windows 8. The following result are same regardless of wether i have numlock turned on or not:
Robot bot = new Robot();
bot.keyPress(KeyEvent.VK_UP); //this in documentation is non-numpad up arrow key
bot.keyRelease(KeyEvent.VK_UP); //pressed the numpad up arrow key
//folowing line is line #43
bot.keyPress(KeyEvent.VK_KP_UP); //this in documentation is numpad up arrow key
bot.keyRelease(KeyEvent.VK_KP_UP); //causes folowing exception:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Robot.java:358)
at test.RobotArrow.main(RobotArrow.java:43)
I know this question was already asked here but over a year ago, so is there any progress? I cant google anything, there is even an ofiicial bug report
So, is there finnaly a solution or not?
//PRESS WINDOWS + ARROW LEFT
Robot divideWindow = new Robot();
divideWindow.keyPress(KeyEvent.VK_WINDOWS);
divideWindow.delay(100);
divideWindow.keyPress(KeyEvent.VK_LEFT);
divideWindow.delay(100);
divideWindow.keyRelease(KeyEvent.VK_LEFT);
divideWindow.delay(100);
divideWindow.keyRelease(KeyEvent.VK_WINDOWS);
Works fine for me :)
A possible workaround is to disable numlock. See this jdk bug comment