android 拦截鼠标按键

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bawang_cn/article/details/18665981

获取鼠标按键代码:

            view.setOnGenericMotionListener(new OnGenericMotionListener() {
                
                @Override
                public boolean onGenericMotion(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    int what = event.getButtonState();
                    //鼠标移动  0
                    //BUTTON_PRIMARY  (鼠标左键)  1
                    //BUTTON_SECONDARY  (鼠标右键) 8
                    //BUTTON_TERTIARY   (鼠标中键)  32
                    //BUTTON_BACK
                    //BUTTON_FORWARD
                    Log.d("debug","keyCode===777===="+event.getSource() + ":" + event.getToolType(0)  + "::" +what); //8194  3
                    event.getToolType(0); //MotionEvent.TOOL_TYPE_MOUSE
                switch (what) {
                    case MotionEvent.BUTTON_PRIMARY:
                        Log.d("debug","keyCode======="  +what);
                        break;    
                    case MotionEvent.BUTTON_TERTIARY:
                        Log.d("debug","keyCode========"  +what);
                        break;        
                    case MotionEvent.BUTTON_BACK://(鼠标右键)
                        Log.d("debug","keyCode======"+what );
                        break;    
                    }
                    return false;
                }
            });


查看MotionEvent源码,说明如下:

   /**
     * Button constant: Primary button (left mouse button).
     *
     * This button constant is not set in response to simple touches with a finger
     * or stylus tip.  The user must actually push a button.
     *
     * @see #getButtonState
     */
    public static final int BUTTON_PRIMARY = 1 << 0;

    /**
     * Button constant: Secondary button (right mouse button, stylus first button).
     *
     * @see #getButtonState
     */
    public static final int BUTTON_SECONDARY = 1 << 1;

    /**
     * Button constant: Tertiary button (middle mouse button, stylus second button).
     *
     * @see #getButtonState
     */
    public static final int BUTTON_TERTIARY = 1 << 2;

    /**
     * Button constant: Back button pressed (mouse back button).
     * <p>
     * The system may send a {@link KeyEvent#KEYCODE_BACK} key press to the application
     * when this button is pressed.
     * </p>
     *
     * @see #getButtonState
     */
    public static final int BUTTON_BACK = 1 << 3;

    /**
     * Button constant: Forward button pressed (mouse forward button).
     * <p>
     * The system may send a {@link KeyEvent#KEYCODE_FORWARD} key press to the application
     * when this button is pressed.
     * </p>
     *
     * @see #getButtonState
     */
    public static final int BUTTON_FORWARD = 1 << 4;


这里  1 << 4 表示 1向左移动4位,向左移动一位表示乘以2,向右移动表示除以2,因为1 为0001,左移三位为1000,为8,左移四位为10000,为16,

其他类似,如2<< 3 = 16


可以参考如下文章:

http://blog.csdn.net/shanpengfei77/article/details/8157493

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页