Exemple #1
0
        /// <summary>
        /// 定时器的回调方法
        /// </summary>
        /// <param name="state"></param>
        private void OnTimerCallback(object state)
        {
            JoystickAPI.JOYINFOEX infoEx = new JoystickAPI.JOYINFOEX();
            infoEx.dwSize  = Marshal.SizeOf(typeof(JoystickAPI.JOYINFOEX));
            infoEx.dwFlags = (int)(JoystickAPI.JOY_RETURNBUTTONS | JoystickAPI.JOY_RETURNPOV);

            int result = JoystickAPI.joyGetPosEx(this.Id, ref infoEx);

            if (result == JoystickAPI.JOYERR_NOERROR)
            {
                JoystickButtons buttons = GetButtons(infoEx.dwButtons);
                GetXYZButtons(infoEx.dwXpos, infoEx.dwYpos, infoEx.dwZpos, ref buttons);
                GetPOVButtons(infoEx.dwPOV, ref buttons);

                if (PreviousButtons != buttons && PreviousButtons != JoystickButtons.None)
                {
                    //按钮状态有更改,则发出ButtonUp事件

                    //取得前一次按下的按钮与本次按下的按钮的差(即不再处于按下状态的按钮)
                    JoystickButtons b = (JoystickButtons)(PreviousButtons - (PreviousButtons & buttons));
                    if (b != JoystickButtons.None)
                    {
                        this.OnButtonUp(new JoystickEventArgs(this.Id, b));

                        //记录还处于按下状态的按钮
                        PreviousButtons = PreviousButtons & buttons;
                    }
                }

                if (buttons != JoystickButtons.None)
                {
                    //本次有按下按钮

                    //取得当前按下的按钮与前一次按下的按钮的差(即本次是新按下的按钮)
                    JoystickButtons b = (JoystickButtons)(buttons - (PreviousButtons & buttons));
                    if (b != PreviousButtons && b != JoystickButtons.None)
                    {
                        this.OnButtonDown(new JoystickEventArgs(this.Id, b));
                        this.OnClick(new JoystickEventArgs(this.Id, b));

                        //记录当前处于按下状态的按钮
                        PreviousButtons = buttons;
                    }
                }
            }
            else
            {
                //其它值无法工作.则停止获取
                //this.ReleaseCapture();
            }
        }
Exemple #2
0
        /// <summary>
        /// 根据游戏手柄的Id实例化
        /// </summary>
        /// <param name="joystickId"></param>
        private Joystick_V(int joystickId)
        {
            this.Id           = joystickId;
            this.JoystickCAPS = new JoystickAPI.JOYCAPS();

            //取得游戏手柄的参数信息
            if (JoystickAPI.joyGetDevCaps(joystickId, ref this.JoystickCAPS, Marshal.SizeOf(typeof(JoystickAPI.JOYCAPS)))
                == JoystickAPI.JOYERR_NOERROR)
            {
                this.IsConnected = true;
                this.Name        = this.JoystickCAPS.szPname;
            }
            else
            {
                this.IsConnected = false;
            }
        }