java console mouselistener

こ雲淡風輕ζ 提交于 2019-12-02 06:23:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!