Robot class and keystrokes

Can somebody help me? I have a 14000 plus line coded program. I need to generate software produced keystrokes. For example I would like to have my program hit the 'Enter' key instead of it being generated from the keyboard by a user. Now, I have just briefly been introduced to the Robot class and I am trying to understand it. I guess to use it I have to throw a AWTException. The problem is where I need to use the Robot class is in a JButton ActionListener(), which is on line 8000 nested in another frame. My question is how and where do I throw the AWTException in my program so that I can use the Robot class to generate an 'Enter' keystroke? I have noticed that in some sample programs the AWTException follows the opening
public static void main(String[] args) throws AWTException
statement. I tried attaching this in my program but the code nested in a AcitionListener did not recognize it.
What I am hoping is that I can throw the exception in the following format:
try
Robot robot = new Robot();
robot.keypressed(VK_ENTER);
catch (AWTException exception)
exception.printStackTrace();
This is just a format I made up to show what I am looking for it has no actual copyright.
I do not want to throw the AWTExecption for my whole program just this localized area when I need to have the software hit enter for me.
Anyway, this is my problem can you help me out
Derek.

I just figured the problem out myself, by just guessing believe it or not.
As follows :
try
Robot rob = new Robot():
rob.keyPressed(VK_ENTER);
catch(AWTException exception)
exception.printStackTrace();
It worked perfectly.

Similar Messages

  • Robot class and positioning the cursor

    I have an application where I have a number of panels on the screen and using an editable field wish to enter coordinates to position the Cursor to on another panel.
    ie, using grid coordinates on a map. Using the robot class I am able to position the cursor on the other panel but as soon as I move the mouse the cursor jumps back to the original position on the screen where the first panel with the editable field was. It is like only a picture of the cursor was drawn at the new coordinates and the real cursor is still at the old podition. It is important to this application that if the cursor is moved it stays where it has been moved to and actually is there and not just appearing to be there. I am using jdk 1.3 on Intel Solaris 2.8. Any help on this issue would be helpful and appreciated

    Maybe you can use the mouse move event to position the mouse to where you had moved the cursor to.
    for example
    1.the mouse is at point 100,100
    2. you move the cursor from your code to point 0,0
    3. the user starts to move the mouse
    4. you code gets that event and repositions the cursor to the last place where it was, that is at 0,0

  • Robot class and obtaining mouse position

    The Robot class has the mouseMove(int x, int y) method which allows you to position the mouse at the given x-y coordinates. I am trying to move the mouse using relative coordinates, i.e. move 200 pixels left, as opposed to moving to 400,800. Is there a way to obtain my cursor's current position? I've looked at the MouseEvent class, and I'm not very sure that would work, as it requires input from the mouse in order to trigger.

    TryPointerInfo pointerInfo = MouseInfo.getPointerInfo();
    Point p = pointerInfo.getLocation();

  • J2ME, Robot class, and sending mouse clicks from a remote device

    I'm trying to simulate a mouse click on a client machine, using J2ME on a remote host device. What's the best way to do this?

    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class Main
        public static void main(String[] args)
            JFrame frame = new JFrame();
            frame.getContentPane().add(new JButton("Click Me"));
            frame.pack();
            frame.setLocationRelativeTo(null);
            try
                // Simulate a middle-button mouse click
                Robot robot = new Robot();
                robot.mousePress(InputEvent.BUTTON2_MASK);
                // Simulate a middle-button mouse click again
                robot.mousePress(InputEvent.BUTTON2_MASK);
            catch (Exception e)
                e.printStackTrace();
            frame.setVisible(true);
    }?

  • Robot class in Windows Vista

    Hello,
    It seems like there are problems with the java Robot class in Windows Vista.
    Did anyone encounter them?
    Thanks

    I didn't get your response, but I will try to explain myself.
    I have a problem with a program that I wrote that uses the Robot class and doesn't run well on Vista.
    I think that the operation system isn't letting it to control the mouse/keyboard: The program didn't crash,
    Simply didn't move the mouse although it was supposed to.
    I searched google for relevant information and the only relevant result I found was the link I posted.
    That link doesn't help me because in order to view the answer I have to pay money.
    I posted it because I thought that another description of the problem might help to understand it.

  • Differences between Robot class generated events and real input events

    Hello everyone!
    I'm having a problem with the translation of the description of the awt.Robot class.
    - "This class is used to generate native system input events" -
    Does this mean there is no difference between real input events and generated events from the Robot class? Difference in reference to that the generated events can't be recognized as generated events by the os or programs.
    Thanks in advance.
    Chris B.

    The Robot class will use an interface provided by the operating system to inject events. Those are very close to 'real' input events, but not quite there. There are, for instance, several games out there which have a protection mechanism against Robot's events to prevent scripting/botting. However, any application that is not paying special attention to the event's source will assume that the input events generated by Robot are actually coming from the user, so you can 'control' any application with the exception of the beforementioned games.

  • Applet and Robot class???

    Alright, I want to write a java program uses the robot class (the class that allows for control of keybourd and mouse commands) would this work in an applet, or would it break the applet rules that ensure computer security on the web.

    i just want it to work on 1 computer, is there a way that i can override the things that dont work on the net?

  • Doing Shift-End using Robot class

    I am trying to use the Robot class to do a Shift-End in order to select the text in a window from the cursor to the end of the line. However, the following code only moves the cursor to the end of the line without selecting the text.
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_END);
    robot.keyRelease(KeyEvent.VK_END);
    robot.keyRelease(KeyEvent.VK_SHIFT);
    Note that I have been able to make the following code work. So the problem with the code above does not seem to be with the Shift key per se or with the ordering of the statements.
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_SHIFT);
    Any idea how to make the Shift-End work? I am using an XP PC.

    My ENTIRE code follows. Not surprisingly, the shiftEnd.actionPerformed gets a nullPointerException during execution. And being new to Java, I get the feeling I might be missing a whole bunch of stuff. For example, do I need to register for events? Do I need to generate a shift-End ActionEvent somehow? It is only the last 4 lines that are at issue here. Everything down to that point accomplishes what I want, i.e., position the mouse in a text window in the upper right-hand corner of my screen, move the cursor to the top of that window, then go down 5 lines. At that point, I want to do the shift-End to select all text in the line.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.KeyStroke;
    import javax.swing.JTextField;
    public class KeyStrokeTest {
    /** Creates a new instance of KeyStrokeTest */
    public KeyStrokeTest() {
    * @param args the command line arguments
    public static void main(String[] args)
    throws AWTException{
    Robot robot = new Robot();
    // Position mouse over Test Director test
    robot.mouseMove(600,300);
    // Press and release left mouse button to set focus in Test Director test
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    // Go to top of Test Director test
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_HOME);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_HOME);
    // Go down to first line of request
    for(int i=0;i<5;i++) {
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_DOWN);
    robot.delay(1000);
    // Do a shift End
    KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_END,KeyEvent.VK_SHIFT);
    JTextField textComponent = new JTextField("String");
    ActionListener shiftEnd = textComponent.getActionForKeyStroke( keyStroke );
    shiftEnd.actionPerformed( null );

  • Taking screenshot with java applet using java robot class not working

    Hi Everyone,
    I am using the java applet to take screenshot of the web browser using the java's robot class.
    Robot objRobot = new Robot ();
    BufferedImage objBufferedImage = objRobot.createScreenCapture(objRectArea);
    The thing work good in windows system taking screenshot but in case of mac osx i get the blank image.When i check the event viewer in mac osx i get the following error.
    invalid context
    invalid pixel format
    CoreAnimation: rendering error 506
    The problem is coming for all the browser safari,firefox and chrome.My applet is signed applet.
    What might be the reason.An early reply is very valuable.
    My machine configuration is as follows.
    OS : MAC OSX
    Version : 10.6.4
    Is that a system level issue , java plugin issue?I'am confused with this error.
    Thanks sagar.

    870613 wrote:
    invalid context
    invalid pixel formatHm, seems like the Mac implementation of the Robot class is doing some bad initializations there. I can't be a 100% sure of course, but this smells like a bug to me. Are you sure you have the latest version of Java installed?

  • Alternative to Robot class

    hi have anyone an idea how can i make screenshoot from my scene without the class robot?
    at moment i use the Robot class to make screenshoots from my sceene
    Rectangle screenRect = new Rectangle(20,10,800,600);
             try{
                  Robot robot = new Robot();
                  BufferedImage image = robot.createScreenCapture(screenRect);
                  ImageIO.write(image, "png", new File(fileName));               
             } catch (AWTException e1) {}
                          catch (IOException e) {}         thats the code to make screenshoots but i think its not good to use AWT

    That depends: when modularity kicks in (Java 9 with early availability in Java 8?) you may end up with some light JVMs tailored for JavaFX-only without any AWT / Swing support. And if you are not connected to the Net (which is more common than you may think) you may not be able or want* to download the extra module(s) that give AWT / Swing support.
    *slow speed with limited amount size, expensive data plan, device without enough storage.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Robot class not working

    I need to make a macro with the robot class that draws a picture in a paint program. For each color in the image (256 colors total) it should change the paint color and drawing each 2x2 pixel that's that color. I made the program and it works correctly for about an hour, but then it starts slowing down a LOT, entering the wrong color codes, and drawing single pixels instead of 2x2.
    Here is the code for changing the color:
    robot.mouseMove (535, 504);
    robot.mousePress (InputEvent.BUTTON1_MASK);
    robot.mouseRelease (InputEvent.BUTTON1_MASK);
    robot.mouseMove (568, 410);
    robot.mousePress (InputEvent.BUTTON1_MASK);
    robot.mouseRelease (InputEvent.BUTTON1_MASK);
    robot.mousePress (InputEvent.BUTTON1_MASK);
    robot.mouseRelease (InputEvent.BUTTON1_MASK);
    for (i = 0; i < 6; i++)
        robot.keyPress (color.toUpperCase ().charAt (i));
    robot.keyPress (KeyEvent.VK_ENTER);
    robot.keyRelease (KeyEvent.VK_ENTER);Here is the code for drawing each pixel:
    robot.mouseMove (415 + (w * 2), 185 + (h * 2) - 1);
    robot.mousePress (InputEvent.BUTTON1_MASK);
    robot.mouseMove (415 + (w * 2), 185 + (h * 2) + 1);
    robot.delay (100);
    robot.mouseRelease (InputEvent.BUTTON1_MASK);Does anyone know why the robot stops working after about an hour?

    I'll guess that this might have something to do with it:
    for (i = 0; i < 6; i++)
        robot.keyPress (color.toUpperCase ().charAt (i));
        //need to release the key!
        robot.keyRelease (color.toUpperCase ().charAt (i));
    }Also, about your code to draw one pixel: it draws three pixels in a vertical line, at least in my paint program.

  • Drag events in Robot class

    I am using the Robot class in jdk1.3.1 to record and playback user input. I am getting the list of events and then traversing on it to do the appropriate action, mouse move, key events are played back fine, but I am unable to do mouse drag event.
    Here is simplified version of the code:
    for(int i = 0; i < aListEvents.size(); i++)
    AWTEvent event = (AWTEvent)aListEvents.get(i);
    Component c = (Component)event.getSource();
    int id = event.getID();
    switch(id)
    case MouseEvent.MOUSE_MOVED:
    int x = ((MouseEvent)e).getX();
    int y = ((MouseEvent)e).getY();
    m_robot.mouseMove(x,y);
    break;
    case KeyEvent.KEY_PRESSED:
    m_robot.keyPress(((KeyEvent)e).getKeyCode());
    break;
    case MouseEvent.MOUSE_DRAGGED:
         // do component drag
    break;
    Is there a way to mimic the drag event during playback and drag the object that was dragged during record?
    Any suggestions would be greatly appreciated!!
    Thanks :)

    This is an old posting, so the answer is probably too late for mamta and has also been answered elsewhere. But for whoever comes across this one, share my piece of experience:
    There's no one-to-one mapping between Java events and the system events (which is what Robot is refering to).
    After all, a MOUSE_DRAGGED is nothing but a MOUSE_MOVE after a MOUSE_PRESSED. So you can send a MOUSE_DRAGGED along to the robot as a mouseMove, because the Robot/system keeps in mind that the mouse button has been pressed before and therefor the event will appear in the Java event queue as a MOUSE_DRAGGED.
    Similar is MOUSE_CLICKED which needn't be replayed at all, because sending a MOUSE_PRESSED and a MOUSE_RELEASED to the Robot will automatically produce a MOUSE_CLICKED in the Java event queue as well.
    Same with the key strokes.

  • How to capture a screen by using robot class

    how to capture a screen by using robot class

    open the apidocs for the Robot class
    read the methods - you won't get far when one of the methods will stand out
    if you can't get it working, copy the method name and paste it into the 'search forums' box,
    where you'll find plenty of sample code

  • Robot class slowing down

    I am using the robot class to make a macro that moves and clicks the mouse for a few hours. At first, the macro works correctly, and completes about five runs per second. However, after a few minutes the macro starts slowing down, and after an hour it is running through once every 10 seconds. Every run is exactly the same, so why could it be slowing down?

    Here's exactly what it's doing:
    1. get the color of the next pixel in a BufferedImage.
    2. make a robot and have it type the RGB values of the pixel into another window.
    3. move the mouse and click it a few times.
    I'm not using the hard drive. I'm not searching anything. I am not copying anything. Every run is exactly the same. Why is it getting slower as it runs? It finishes about a quarter of the image in 5 minutes, then hours later it has only done a few more rows.

  • Robot class.

    With the robot class. I know their is the standered clas and then the Robot(GraphicsDevice screen) . Yes i know that the graphic device is for specifying another screen. But do u belive their is any thinkable way to specify to a specific window active on the computer?

    I have no idea what you're talking about. But no, there is no way to specify the window.

Maybe you are looking for

  • Itunes wont open need help backing up music on external hard drive

    My itunes wont open after upgraded to 8 , ive uninstalled and reinstalled itunes and quicktime and still wont work. i found a topic to possibly help me but i dont know how to tranfer my music to an external disk drive without being able to open itune

  • PSE 9/Mac crashing with .NEF files

    I've been using PSE 9 for a few weeks with no issues. Then I got a new camera, a Nikon D7000, and it started crashing regularly. I'm editing .NEF files (RAW from the Nikon). I've had messages saying it can't initialize because of a program error as w

  • CS4 Trial won't download

    I presently have CS3 and would like to try CS4 before purchasing. I have Vista-64, 12G Ram, i7 PC. After initiating the download, it just sits with a message saying "adobe download starting"...that's it...no dowmload. Is there a switch or setting I'm

  • Print Layout Desginer - Help

    Hi experts, My DO design as following: ItemNo          Description               Quantity            Total Price LineNum()    DLN1.ItemDescription   DLN1.Quantity   variable(??)                    DLN1.Text                    DLN1.FreeText I have fou

  • Flash and MultiTouch Event but not in AIR

    Hi everybody. Im a new in MultiTouch. But I need to create a app for run in big monitor with multitouch. In all exemples that I found in web, all uses or debug for AIR to use the touch.  In my case, I can run the flv or transform in exe this flv to o