This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If statement  RSS feed

 
Bryan Thomas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code for a robot. To avoid an infinite loop, I would like for the escape key to close the program only I can't figure out how to use the escape key with the If statement.Here is my code. PLEASE HELP!!!

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class Robot05{
//Create an array of keycode data
int keyInput[] = {
KeyEvent.VK_H,
KeyEvent.VK_E,
KeyEvent.VK_L,
KeyEvent.VK_L,
KeyEvent.VK_UP,
KeyEvent.VK_DOWN,
KeyEvent.VK_LEFT,
KeyEvent.VK_RIGHT,
KeyEvent.VK_ENTER,
KeyEvent.VK_CONTROL,
KeyEvent.VK_ESCAPE,
KeyEvent.VK_O
};//end keyInput array

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


Robot robot = new Robot();
robot.delay(10000);
int i = 0;
while (i <=10)

{
i ++;
robot.keyPress(KeyEvent.VK_CONTROL);
robot.delay(100);
robot.mouseMove(955,300);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseMove(711,130);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseMove(875,500);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(100);
robot.keyPress(KeyEvent.VK_F2);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_F2);
robot.delay(100);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(100);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.delay(100);
robot.mouseMove(955,300);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseMove(711,475);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseMove(875,500);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(100);
robot.keyPress(KeyEvent.VK_F2);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_F2);
robot.delay(100);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(100);
}

}//main
}//end class Robot05
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm going to move this to the Swing/AWT forum, where this decidedly non-beginner question will be on-topic. Followups there, please.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryan, please don't post the same question more than once. I deleted your duplicate post. But lucky you, this one gets bumped up.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryan, once again, do not post the same question more than once and in 1 forum please.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to capture the ESCAPE KEY you have to use a Key Listener and in order to use a Key Listener there has to be something listening for key events. Only GUI elements can listen to key events. From you code, I don't see any GUI elements to your application.

Maybe you could explain more about what your program is suposed to do and we can give you better/different ideas on how to achieve what you desire. From you code, all you are doing is moving the mouse around the screen.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!