获取鼠标按键代码:

            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).
     *


     * The system may send a {@link KeyEvent#KEYCODE_BACK} key press to the application
     * when this button is pressed.
     *


     *
     * @see #getButtonState
     */
    public static final int BUTTON_BACK = 1 << 3;

    /**
     * Button constant: Forward button pressed (mouse forward button).
     *


     * The system may send a {@link KeyEvent#KEYCODE_FORWARD} key press to the application
     * when this button is pressed.
     *


     *
     * @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

更多相关文章

  1. Android下修改SeekBar样式
  2. Android的SurfaceView绘图并移动
  3. android 实现类似选项卡的UI
  4. Android(安卓)之ContextMenu上下文菜单
  5. 举例说明Android中AnalogClock的使用
  6. 【AndroidStudio】Aapt2Exception
  7. 配置 Lync 2010 for iPhone
  8. [Android] Android打开WIFI或者移动网络
  9. Android开发获取开机启动项列表

随机推荐

  1. Android app的登录和注册功能
  2. 全志Android A10技术论坛推广
  3. android 刷新View
  4. More than one file was found with OS i
  5. Android Robotium的自动化代码
  6. android 图形开发实例
  7. Android App Development with Clojure
  8. Android利用反射获取状态栏(StatusBar)高
  9. Android 蓝牙配对、连接和通信
  10. Android 使用广播(BroadcastReceiver)传递