Computer Science Canada Emulate Mouse/Keyboard |
Author: | Nathan4102 [ 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 |
Author: | DemonWasp [ 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. |
Author: | Nathan4102 [ 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!! |
Author: | Nathan4102 [ 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.
|
Author: | DemonWasp [ 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. |
Author: | Nathan4102 [ 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 ![]() |
Author: | Nathan4102 [ 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 |