Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Emulate Mouse/Keyboard
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: Wed Jul 16, 2014 10:57 pm   Post subject: Emulate Mouse/Keyboard

I'm looking for a way to emulate key presses and mouse movements/clicks to a java application. And by that I mean programatically call the actions without having any effect on the windows mouse, yet "tricking" the java application into thinking there has been action.

Is there a way to do this? I've tried processMouseMotionEvent and Component.distatchEvent(MouseEvent), neither have worked so far though.

If it matters, I'm using the DJ Native Swing browser so ideally I'd like the actions to be performed in there, but if I need to downgrade to a more basic browser that is A-Ok.

Thanks
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Jul 16, 2014 11:48 pm   Post subject: RE:Emulate Mouse/Keyboard

Like all things, Java has a class for that: http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html

Note that according to the documentation, it will actually move your mouse around for you (which violates one of your requirements). If you want to avoid that, then you will have to construct mouse events and post them to the AWT event queue. This thread ( http://stackoverflow.com/questions/9722428/allow-events-to-be-posted-in-evenqueue-between-postevent-calls-in-swing ) will give you a brief insight into how you can post events, and one of the less obvious problems you could run into by doing that.
Nathan4102




PostPosted: Wed Jul 16, 2014 11:52 pm   Post subject: RE:Emulate Mouse/Keyboard

I've already worked with the Robot class a lot, but I've decided that having my application running without needing to sacrifice my mouse is a major requirement. That's weird, I've been looking for HOURS and I havn't seen a thing about posting to an AWT event queue. Well I'll try it now and ill let you know what happens. Thanks!!
Nathan4102




PostPosted: Thu Jul 17, 2014 12:46 am   Post subject: Re: Emulate Mouse/Keyboard

This stuff is so weird, in my browser program it's DEFINETLY doing something because I lose focus in textboxes, but no clicks are being made on the browser.

I made a little test app just to see if I could get the program to click inside the window and I've had no success so far... Am I doing something incorrectly here? This program basically just keeps sending MouseEvents to the queue.

Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ClickTest extends JFrame{

static JPanel panel = new JPanel();
   public ClickTest() {
        initUI();
    }
   
   private void initUI() {
        getContentPane().add(panel);

        JButton button = new JButton("TESTING");
        button.setLocation(0, 0);
        button.setSize(500, 500);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {     
                System.out.println("HELLO");
            }
        });
       
        add(button);
        panel.setLayout(null);
        setTitle("Test");
        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
   
     public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ClickTest ex = new ClickTest();
                ex.setVisible(true);
                new Thread(new Runnable() {
                    @Override public void run() {
                        while (true) {
                            MouseEvent e = new MouseEvent(panel, MouseEvent.MOUSE_PRESSED, MouseEvent.BUTTON1_MASK, 0, 100, 100, 1, false);
                            java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
                        }
                    }
                }).start();
            }
        });
    }
}
DemonWasp




PostPosted: Thu Jul 17, 2014 7:19 am   Post subject: RE:Emulate Mouse/Keyboard

You're posting a MOUSE_PRESSED event but not a MOUSE_RELEASED event. A single "click" is both press and release, so you will have to post both.

Be aware that, like the SO link I posted suggests, if you post both events together, and the PRESSED event generates further events (in your code, or in component code) then it might be wiser to enqueue the PRESSED event, wait until it is processed, then enqueue the RELEASED event. I'm not sold on the "accepted" solution there, but it is a start.
Nathan4102




PostPosted: Thu Jul 17, 2014 12:58 pm   Post subject: RE:Emulate Mouse/Keyboard

That and a few parameter issues and I've fixed the problem. Unfortunately it looks like component j is only the button/address bar at the top of my browser, so I'm going to have to snoop about to find a way to control the browser portion, or I'll just find a new broswer. Thanks for the help though Very Happy
Nathan4102




PostPosted: Tue Jul 22, 2014 3:07 pm   Post subject: RE:Emulate Mouse/Keyboard

Solved!

Well, sort of. I ended up using VirtualBox to run instances of windows running a java program which moves the actual mouse. I use up 2GB of RAM for every instance, but it works for me. Thanks for the help everyone, this problem was a bit too much over my java level though
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: