예제 #1
0
 public void GetJoyStickThread()
 {
     while (true)
     {
         API.JoyInfoEx joyInfo1 = new API.JoyInfoEx();
         joyInfo1.dwSize  = (uint)Marshal.SizeOf(typeof(API.JoyInfoEx));
         joyInfo1.dwFlags = (int)API.JOY_RETURNALL;
         if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
         {
             _ButtonInfos = GetButtonFromJoyInfoEx(joyInfo1);
             if (_ButtonInfos != JoystickButtons.None)
             {
                 return;
             }
             //OnClick(new JoystickEventArgs(API.JOYSTICKID1, _ButtonInfos));
         }
         Thread.Sleep(10);
     }
 }
예제 #2
0
 public void GetJoyStickThread()
 {
     while (true)
     {
         API.JoyInfoEx joyInfo1 = new API.JoyInfoEx();
         joyInfo1.dwSize = (uint)Marshal.SizeOf(typeof(API.JoyInfoEx));
         joyInfo1.dwFlags = (int)API.JOY_RETURNALL;
         if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
         {
             _ButtonInfos = GetButtonFromJoyInfoEx(joyInfo1);
             if (_ButtonInfos != JoystickButtons.None)
                 return;
             //OnClick(new JoystickEventArgs(API.JOYSTICKID1, _ButtonInfos));
         }
         Thread.Sleep(10);
     }
 }
예제 #3
0
        /// <summary>
        /// 处理系统消息.
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //没有关注的,直接返回
            if (IsClickEventEmpty())
                return IntPtr.Zero;

            bool flag = false;
            if (hwnd != IntPtr.Zero && (wParam != IntPtr.Zero || lParam != IntPtr.Zero))
            {
                if (msg != API.MM_JOY1MOVE && msg != API.MM_JOY2MOVE)
                    return IntPtr.Zero;

                Action<JoystickEventArgs> action = null;
                JoystickButtons buttons = JoystickButtons.None;
                int joystickId = -1;
                API.JoyInfoEx joyInfo1 = new API.JoyInfoEx();
                joyInfo1.dwSize = (uint)Marshal.SizeOf(typeof(API.JoyInfoEx));
                joyInfo1.dwFlags = (int)API.JOY_RETURNALL;

                if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                {
                    buttons = GetButtonFromJoyInfoEx(joyInfo1);
                    action = this.OnClick;
                }
                joystickId = msg == API.MM_JOY1MOVE ? API.JOYSTICKID1 : API.JOYSTICKID2;

                //switch (msg)
                //{
                //    case API.MM_JOY1MOVE:
                //    case API.MM_JOY2MOVE:
                //        //单击事件
                //        //buttons = GetButtonsStateFromMessageParam(wParam.ToInt64(), lParam.ToInt64());
                //        if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                //        {
                //            buttons = GetButtonFromJoyInfoEx(joyInfo1);
                //            action = this.OnClick;
                //        }
                //        joystickId = msg == API.MM_JOY1MOVE ? API.JOYSTICKID1 : API.JOYSTICKID2;
                //        break;
                //    case API.MM_JOY1BUTTONDOWN:
                //    case API.MM_JOY2BUTTONDOWN:
                //        //按钮被按下
                //        //buttons = GetButtonsPressedStateFromMessageParam(wParam.ToInt32(), lParam.ToInt32());
                //        if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                //        {
                //            buttons = GetButtonFromJoyInfoEx(joyInfo1);
                //            action = this.OnButtonDown;
                //        }
                //        joystickId = msg == API.MM_JOY1BUTTONDOWN ? API.JOYSTICKID1 : API.JOYSTICKID2;
                //        break;
                //    case API.MM_JOY1BUTTONUP:
                //    case API.MM_JOY2BUTTONUP:
                //        //按钮被弹起
                //        //buttons = GetButtonsPressedStateFromMessageParam(wParam.ToInt32(), lParam.ToInt32());
                //        if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                //        {
                //            buttons = GetButtonFromJoyInfoEx(joyInfo1);
                //            action = this.OnButtonUp;
                //        }
                //        joystickId = msg == API.MM_JOY1BUTTONUP ? API.JOYSTICKID1 : API.JOYSTICKID2;
                //        break;

                //    case API.MM_JOY1ZMOVE:
                //    case API.MM_JOY2ZMOVE:
                //        break;
                //}
                if (action != null && joystickId != -1 && buttons != JoystickButtons.None)
                {
                    //阻止消息继续传递
                    flag = true;
                    //触发事件
                    action(new JoystickEventArgs(joystickId, buttons));
                }

                if (joystickId != -1 && _LastButtonsInfo != buttons)
                {
                    //无论是否有按键按下,发送move消息
                    this.OnMove(new JoystickEventArgs(joystickId, buttons));
                    _LastButtonsInfo = buttons;

                }
                
            }
            handled = flag;
            return IntPtr.Zero;
        }
예제 #4
0
        private JoystickButtons GetButtonFromJoyInfoEx(API.JoyInfoEx joyInfo)
        {
            JoystickButtons buttons = JoystickButtons.None;

            //pov
            if (joyInfo.dwPOV == API.JOY_POVFORWARD)
            {
                buttons |= JoystickButtons.POVUp;
            }
            else if (joyInfo.dwPOV == API.JOY_POVBACKWARD)
            {
                buttons |= JoystickButtons.POVDown;
            }
            else if (joyInfo.dwPOV == API.JOY_POVLEFT)
            {
                buttons |= JoystickButtons.POVLeft;
            }
            else if (joyInfo.dwPOV == API.JOY_POVRIGHT)
            {
                buttons |= JoystickButtons.POVRight;
            }
            else if (joyInfo.dwPOV == API.JOY_POVFORWARDLEFT)
            {
                buttons |= JoystickButtons.POVUp;
                buttons |= JoystickButtons.POVLeft;
            }
            else if (joyInfo.dwPOV == API.JOY_POVFORWARDRIGHT)
            {
                buttons |= JoystickButtons.POVUp;
                buttons |= JoystickButtons.POVRight;
            }
            else if (joyInfo.dwPOV == API.JOY_POVBACKWARDLEFT)
            {
                buttons |= JoystickButtons.POVDown;
                buttons |= JoystickButtons.POVLeft;
            }
            else if (joyInfo.dwPOV == API.JOY_POVBACKWARDRIGHT)
            {
                buttons |= JoystickButtons.POVDown;
                buttons |= JoystickButtons.POVRight;
            }

            //move
            if (joyInfo.dwYpos > API.JOY_MOVE_Y_ZERO)
            {
                buttons |= JoystickButtons.Down;
            }
            else if (joyInfo.dwYpos < API.JOY_MOVE_Y_ZERO)
            {
                buttons |= JoystickButtons.UP;
            }

            if (joyInfo.dwXpos > API.JOY_MOVE_X_ZERO)
            {
                buttons |= JoystickButtons.Right;
            }
            else if (joyInfo.dwXpos < API.JOY_MOVE_X_ZERO)
            {
                buttons |= JoystickButtons.Left;
            }

            if (joyInfo.dwZpos > API.JOY_MOVE_Z_ZERO)
            {
                buttons |= JoystickButtons.MoveZUp;
            }
            else if (joyInfo.dwZpos < API.JOY_MOVE_Z_ZERO)
            {
                buttons |= JoystickButtons.MoveZDown;
            }

            //normal button
            if ((joyInfo.dwButtons & API.JOY_BUTTON1) == API.JOY_BUTTON1)
            {
                buttons |= JoystickButtons.B1;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON2) == API.JOY_BUTTON2)
            {
                buttons |= JoystickButtons.B2;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON3) == API.JOY_BUTTON3)
            {
                buttons |= JoystickButtons.B3;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON4) == API.JOY_BUTTON4)
            {
                buttons |= JoystickButtons.B4;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON5) == API.JOY_BUTTON5)
            {
                buttons |= JoystickButtons.B5;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON6) == API.JOY_BUTTON6)
            {
                buttons |= JoystickButtons.B6;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON7) == API.JOY_BUTTON7)
            {
                buttons |= JoystickButtons.B7;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON8) == API.JOY_BUTTON8)
            {
                buttons |= JoystickButtons.B8;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON9) == API.JOY_BUTTON9)
            {
                buttons |= JoystickButtons.B9;
            }
            if ((joyInfo.dwButtons & API.JOY_BUTTON10) == API.JOY_BUTTON10)
            {
                buttons |= JoystickButtons.B10;
            }

            return(buttons);
        }
예제 #5
0
        /// <summary>
        /// 处理系统消息.
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //没有关注的,直接返回
            if (IsClickEventEmpty())
            {
                return(IntPtr.Zero);
            }

            bool flag = false;

            if (hwnd != IntPtr.Zero && (wParam != IntPtr.Zero || lParam != IntPtr.Zero))
            {
                if (msg != API.MM_JOY1MOVE && msg != API.MM_JOY2MOVE)
                {
                    return(IntPtr.Zero);
                }

                Action <JoystickEventArgs> action  = null;
                JoystickButtons            buttons = JoystickButtons.None;
                int           joystickId           = -1;
                API.JoyInfoEx joyInfo1             = new API.JoyInfoEx();
                joyInfo1.dwSize  = (uint)Marshal.SizeOf(typeof(API.JoyInfoEx));
                joyInfo1.dwFlags = (int)API.JOY_RETURNALL;

                if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                {
                    buttons = GetButtonFromJoyInfoEx(joyInfo1);
                    action  = this.OnClick;
                }
                joystickId = msg == API.MM_JOY1MOVE ? API.JOYSTICKID1 : API.JOYSTICKID2;

                //switch (msg)
                //{
                //    case API.MM_JOY1MOVE:
                //    case API.MM_JOY2MOVE:
                //        //单击事件
                //        //buttons = GetButtonsStateFromMessageParam(wParam.ToInt64(), lParam.ToInt64());
                //        if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                //        {
                //            buttons = GetButtonFromJoyInfoEx(joyInfo1);
                //            action = this.OnClick;
                //        }
                //        joystickId = msg == API.MM_JOY1MOVE ? API.JOYSTICKID1 : API.JOYSTICKID2;
                //        break;
                //    case API.MM_JOY1BUTTONDOWN:
                //    case API.MM_JOY2BUTTONDOWN:
                //        //按钮被按下
                //        //buttons = GetButtonsPressedStateFromMessageParam(wParam.ToInt32(), lParam.ToInt32());
                //        if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                //        {
                //            buttons = GetButtonFromJoyInfoEx(joyInfo1);
                //            action = this.OnButtonDown;
                //        }
                //        joystickId = msg == API.MM_JOY1BUTTONDOWN ? API.JOYSTICKID1 : API.JOYSTICKID2;
                //        break;
                //    case API.MM_JOY1BUTTONUP:
                //    case API.MM_JOY2BUTTONUP:
                //        //按钮被弹起
                //        //buttons = GetButtonsPressedStateFromMessageParam(wParam.ToInt32(), lParam.ToInt32());
                //        if (API.joyGetPosEx(API.JOYSTICKID1, ref joyInfo1) == API.JOYERR_NOERROR)
                //        {
                //            buttons = GetButtonFromJoyInfoEx(joyInfo1);
                //            action = this.OnButtonUp;
                //        }
                //        joystickId = msg == API.MM_JOY1BUTTONUP ? API.JOYSTICKID1 : API.JOYSTICKID2;
                //        break;

                //    case API.MM_JOY1ZMOVE:
                //    case API.MM_JOY2ZMOVE:
                //        break;
                //}
                if (action != null && joystickId != -1 && buttons != JoystickButtons.None)
                {
                    //阻止消息继续传递
                    flag = true;
                    //触发事件
                    action(new JoystickEventArgs(joystickId, buttons));
                }

                if (joystickId != -1 && _LastButtonsInfo != buttons)
                {
                    //无论是否有按键按下,发送move消息
                    this.OnMove(new JoystickEventArgs(joystickId, buttons));
                    _LastButtonsInfo = buttons;
                }
            }
            handled = flag;
            return(IntPtr.Zero);
        }