尝试全局鼠标侦听器停止运行w / o错误

时间:2016-01-05 04:15:57

标签: java

以下是我最近制作的一些代码:

public class InterceptTouchCardView extends CardView {
    private GestureDetector mGestureDetector;
    private boolean mLongClicked;

    public InterceptTouchCardView(Context context) {
        super(context);
        Initialize();
    }

    public InterceptTouchCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Initialize();
    }

    public InterceptTouchCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Initialize();
    }

    private void Initialize() {
        mGestureDetector = new GestureDetector(getContext(),
                new GestureDetector.SimpleOnGestureListener() {
                    public boolean onDown(MotionEvent e) {
                        mLongClicked = false;
                        return true;
                    }

                    public void onLongPress(MotionEvent e) {
                        mLongClicked = true;
                        performLongClick();
                    }
                });
    }

    /**
     * Intercept touch event so that inner views cannot receive it.
     *
     * If a ViewGroup contains a RecyclerView and has an OnTouchListener or something like that,
     * touch events will be directly delivered to inner RecyclerView and handled by it. As a result,
     * parent ViewGroup won't receive the touch event any longer.
     *
     * We can't Intercept the touch event if we want to allow scrolling since ACTION_DOWN always
     * happens before ACTION_MOVE.  So handle touch events here since onTouchEvent won't be triggered.
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        if (ev.getAction() == MotionEvent.ACTION_UP && !mLongClicked)
            this.callOnClick();
        return false;
    }
}

它编译得很好。但是,当我运行它时它运行大约5秒然后退出(没有控制台上的消息),当我在短时间内点击我的鼠标程序运行时,没有消息打印到控制台。我已经尝试增加程序的最大内存,但是,正如我所料,除了稍长的运行时间之外没有任何变化。谢谢!

1 个答案:

答案 0 :(得分:0)

据我所知,这对我有用

public static void main(String args[]) {                                                  
  Test0 t=new Test0();

  t.setVisible(true);

        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
            public void eventDispatched(AWTEvent e) {
                if(e instanceof MouseEvent){
                    MouseEvent event = (MouseEvent)e;
                    if(event.getID() == MouseEvent.MOUSE_PRESSED){}
                        System.out.println("test "+System.currentTimeMillis());
                    }
                }
            }, AWTEvent.MOUSE_EVENT_MASK);

}

其中Test0是某个组件

相关问题