• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If statement

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic