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

Similar Messages

  • 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 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.

  • 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

  • How to listen and obtain caret positions of changed text in JTextPane?

    Hi,
    I have a JTextPane that displays text with some styles on particular words in the text. For example, I highlight words with red color if they are in my dictionary file. I use regular expression to find matches, replace them with their corresponded definitions, and call setCharacterAttributes to add styles to the definitions. I store all start and end caret positions of the matched words/definitions in a vector. So, I can redisplay styles of all previous matches.
    The problem is that I'd like to be able to edit the text of some previous matches. So, with those changes all caret positions that I already store in the vector need to be updated.
    How can I obtain caret positions of changed text in JTextPane?
    How can I know that a user is currently changing text in the JTextPane?
    How can I get style of text such as color that is being changed?
    Thank you very much.

    Thank you very much, camickr, for your reply.
    I think that I might not know the right way to handle JTextPane and Document object.
    What I have done are:
    - Add style to JTextPane using, for example,
    Style targetCurrentMatchStyle = this.targetWindowTextPane.addStyle("Red", null);
    StyleConstants.setForeground(targetCurrentMatchStyle, Color.red);//For highlight - Then, I use regular expression (Pattern and Matcher) to find a match in text. For each match, I get start and end position from matcher.start() and matcher.end().
    if(matcher.find(start)){
    String term=matcher.group();
    int start=matcher.start();
    int end = matcher.end();
    //find definition for the matched term.
    String definition=mydictionaryHash.get(term);
    //Store caret positions in lists
    startPositionList.add(start);
    matchedLength=lengthList.add(definition.length());
    //Add changed to text in textpane
    StringBuffer sb=new StringBuffer();
    matcher.appendReplacement(sb, definition);
    matcher.appendTail(sb);
    //Get translated text from StringBuffer after replacement
    String translatedText=sb.toString();
    targetWindoTextPane.setText(translatedText);
    //Update start position for next search
    start=start+definition.length();
    //Add style to matched regions below.
    }- From the lists of start positions and matched lengths, I use the following code to add "Red" color to the matched text regions including all previously matched.
    for(int i=0;i<startPositionList.size();i++){
    this.targetWindowTextPane.getStyledDocument().setCharacterAttributes(
    startPositionList.get(i),
    lengthList.get(i),
    this.targetWindowTextPane.getStyle("Red"),
    true);
    }My issue is that I'd like to be able edit previously matched regions and update all positions of the matched regions stored in the lists.

  • 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.

  • 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?

  • 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.

  • How can I disable or enable the keyboard and fix mouse's position with LaBVIEW?

    Hi,
    1. I'ld like to disable or enable the keyboard with LabVIEW.
    2. I'ld like to fix the mouse's position with LabVIEW.
    How can I do it?
    Please advise.
    Regards
    Hork

    For the keyboard, you could create a filtering event and discard the key change.
    What do you mean by "fix the mouse position"? Warping or stalling the mouse is very bad UI form., the operator should always be in control of the cursor position. Could you explain what you want to do?
    LabVIEW Champion . Do more with less code and in less time .

  • Mouse position capturing

    I was wondering if there is a way to capture the mouse position w/o the use of a frame or jframe? I already know how to use the MouseListener on the JFrame/Frame, but that is no help to me since I'm not using a frame at all. Any suggestions????

    What is your goal. This will work -- (the window is 1 pixel!) -- because the robot starts a "drag" and
    swing has mouse capture during drag, but as soon as the use clicks, it's over...
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class X {
        public static void main(String[] args) throws AWTException {
            JWindow w = new JWindow();
            Container cp = w.getContentPane();
            cp.addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                       System.out.println(e.getPoint());
                public void mouseMoved(MouseEvent e) {
            w.setBounds(new Rectangle(0,0,1,1));
            w.setVisible(true);
            Robot rob = new Robot();
            rob.mouseMove(0,0);
            rob.mousePress(InputEvent.BUTTON1_MASK);
            rob.mouseMove(100,100);
    [/code[

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

  • Problem figuring out annotation using Robot class

    HI
    I have made a desktop sharing tool using the robot class in java. On the server end it captures screen and on the client end i have used a jlabel to diplay it....
    Now i want to extend it to allow users to draw on screen while that is being transmitted to the client end.
    The major problem for me is i dont know how do i get the frame being transmitted as the window i create to diplay the contents being capture would come to the top and hence its contents will be captured by the robot class......
    How can i solve this????
    Thanx in advance...

    I can suggest to you a solution to "draw" on the screen, but you won't be able to see what you're drawing as you draw it.
    You'll need a seperate thread to poll the position of the mouse every 20 or so milliseconds. You can use
    java.awt.MouseInfo.getPointerInfo().getlocation();to get the location of the mouse.
    You then implement your drawing algorithms to draw on a BufferedImage of TYPE_4BYTE_ABGR or any of the other types that support transparency. Of course, the image needs to be the size of the screen. Then on every single image you stream, you draw your ABGR image ontop of the one you are about the stream. Voila.
    As I said though, you can't actually see what you're drawing. In order to see what you are drawing you need to display it in a window that scales down the images and is unobtrusive. Of course, this window in turn will be caught by the screen capture, so the clients will be able to "see" your helper window.
    On the other hand, if all you are doing is streaming images of a *single java window* (not the whole desktop) and all you need to do is draw on that single java window, then I can reccomend to you a better solution that utilizes the GlassPane for drawing. By using the the glass pane, the drawings will appear directly on the window, and you don't need a helper window to see your drawings.

  • Hwo to "track the mouse position"

    I m a java newbie, and need to write a small program to track the mouse position.
    basically, it opens a small window, and tell you the current mouse x, y coordinate. when mouse moves, hte x, y changes...
    by the way, how can i compile it to .exe file, after i finished coding.
    i try this way, but so many errors.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class mouseWin {
    public static void main(String[] args)
              protected int last_x=0, last_y=0;
              mouseMoved(MouseEvent e);
    JFrame frame = new JFrame("Mike's Mouse");
    JLabel X = new JLabel("x: " + last_x + "y: " + last_y);
    frame.getContentPane().add(X);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
         public mouseMoved(mouseEvent e)
              last_x = e.getx();
              last_y = e.gety();

    please help.
    i rewrite the code, but still cannot get it work
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.EventListener;
    public class mouseWin implements MouseMotionListener
    public static void main(String[] args)
                   int last_x;
                   int last_y;
                   last_x = 0;
                   last_y = 0;
                   JFrame frame = new JFrame("Mike's Mouse");
                   JLabel X = new JLabel("x: " + last_x + "y: " + last_y);
              frame.getContentPane().add(X);
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.pack();
                   frame.setVisible(true);
         public void mouseMoved(MouseEvent e)
              last_x = e.getx();
              last_y = e.gety();
              JLabel X = new JLabel("x: " + last_x + "y: " + last_y);
    }

  • DnD - Get Drop Mouse Position

    I want to Drag some image file from a JList, and then drop it on a JPanel. The dropping on the panel functions. The problem is, i would like to get the position of the mouse, when the item is dropped on the panel. I use the panel to visualize graphics with Graphics2D, and the image dragged from the list should then be displayed at the position on the panel, where it is dropped. So how could i get this position? I have added a MouseListener to the panel, but it does not react on the mouse events which are caused by the DnD action.

    I am really helpless... Is this a stupid question or does nobody know the answer?
    I just want to get the mouse position of the drop, after a drag and drop.
    I found a MouseDragGestureRecognizer Class, but i am not able to implement. Actually i have just a JList, from which Strings are dragged, and a JPanel, on which the Strings are dropped.
    The drag and dropping functions, but i need the mouse position on the JPanel, when the String is dropped.

  • 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.

Maybe you are looking for