问题
I am trying to make a mouse recorder, I cant seem to get a mouse listener to work with a console, is this possable and how would I go about it Thanks.
回答1:
Unless you wrote your own console that fired mouse events, I dont' think you're going to be able to do it. What widget are you going to register your mouselistener against otherwise? The console isn't a swing component, therefore, no swing events.
回答2:
You can do this by using global hooks. In order to use them you'll need to include some natives or try the same using JNI (see: wikipedia).
Two examples:
- http://kra.lc/blog/2011/07/java-global-system-hook/
(works well - I would advice to re-indent the c++ content and if you know how to do, merge it into 1-2 files - your eyes will thank you) - http://www.jotschi.de/Technik/2008/01/06/java-global-keyboard-hook-jni.html
(never tried, but looks more simple)
Edit:
Example for some playback functionalities:
import java.awt.AWTException; import java.awt.DisplayMode; import java.awt.MouseInfo; import java.awt.PointerInfo; import java.awt.Robot; import java.util.Random; // class instructions try { PointerInfo pntInfo = MouseInfo.getPointerInfo(); DisplayMode dispMode = pntInfo.getDevice().getDisplayMode(); int newX = new Random().nextInt( dispMode.getWidth() ); int newY = new Random().nextInt( dispMode.getHeight() ); new Robot( pntInfo.getDevice() ).mouseMove( newX, newY ); } catch ( AWTException exception ) { }
Sorry for my late answer ;)
来源:https://stackoverflow.com/questions/4336112/java-console-mouselistener