android 拦截鼠标按键

获取鼠标按键代码:

            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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值