private void TJsListening() { MessageBox.Show("监听到手柄"); m_JoystickInfoLast = new JoystickInfoClass(); m_JoystickInfoNew = new JoystickInfoClass(); for (int i = 0; i < m_JoystickInfoNew.Buttons.Length; i++) { m_JoystickInfoLast.Buttons[i] = false;//初始化时,所有按键均未按下 } m_JoystickInfoLast.PointOfViewControllers[0] = JoystickPViewHatInfo.PViewHat_Null; try { while (true) { //初始化新按键 for (int i = 0; i < m_JoystickInfoNew.Buttons.Length; i++) { m_JoystickInfoNew.Buttons[i] = false;//初始化时,所有按键均未按下 } m_JoystickInfoNew.PointOfViewControllers[0] = JoystickPViewHatInfo.PViewHat_Null; //如果手柄掉了,会引发异常,结束该线程 var CurJoyState = curJoystick.GetCurrentState(); //获取按键信息 //获取的按键名比厂家软件显示的小1 int ButtonsLen = CurJoyState.Buttons.Length; for (int j = 0; j < ButtonsLen; j++) { if (Convert.ToInt32(CurJoyState.Buttons[j]) > 0) { m_JoystickInfoNew.Buttons[j] = true;//按下 } } //获取Point Of View Hat信息 //int PointOfViewLen= b.GetPointOfView().Length; int PointOfViewLen = CurJoyState.PointOfViewControllers.Length; for (int j = 0; j < PointOfViewLen; j++) { //数据范围是0,4500,9000,13500,18000,22500,27000,31500 //没有按的时候是-1 m_JoystickInfoNew.PointOfViewControllers[j] = (JoystickPViewHatInfo)Convert.ToInt32(CurJoyState.PointOfViewControllers[j]); } //获取Slider信息 int SliderLen = CurJoyState.Sliders.Length; for (int j = 0; j < SliderLen; j++) { m_JoystickInfoNew.AccelerationSliders[j] = CurJoyState.AccelerationSliders[j]; m_JoystickInfoNew.ForceSliders[j] = CurJoyState.ForceSliders[j]; m_JoystickInfoNew.VelocitySliders[j] = CurJoyState.VelocitySliders[j]; m_JoystickInfoNew.Sliders[j] = CurJoyState.Sliders[j]; } //获取摇杆信息 m_JoystickInfoNew.X = CurJoyState.X; m_JoystickInfoNew.Y = CurJoyState.Y; m_JoystickInfoNew.Z = CurJoyState.Z; m_JoystickInfoNew.VelocityX = CurJoyState.VelocityX; m_JoystickInfoNew.VelocityY = CurJoyState.VelocityY; m_JoystickInfoNew.VelocityZ = CurJoyState.VelocityZ; m_JoystickInfoNew.TorqueX = CurJoyState.TorqueX; m_JoystickInfoNew.TorqueY = CurJoyState.TorqueY; m_JoystickInfoNew.TorqueZ = CurJoyState.TorqueZ; m_JoystickInfoNew.RotationX = CurJoyState.RotationX; m_JoystickInfoNew.RotationY = CurJoyState.RotationY; m_JoystickInfoNew.RotationZ = CurJoyState.RotationZ; m_JoystickInfoNew.ForceX = CurJoyState.ForceX; m_JoystickInfoNew.ForceY = CurJoyState.ForceY; m_JoystickInfoNew.ForceZ = CurJoyState.ForceZ; m_JoystickInfoNew.AngularVelocityX = CurJoyState.AngularVelocityX; m_JoystickInfoNew.AngularVelocityY = CurJoyState.AngularVelocityY; m_JoystickInfoNew.AngularVelocityZ = CurJoyState.AngularVelocityZ; m_JoystickInfoNew.AngularAccelerationX = CurJoyState.AngularAccelerationX; m_JoystickInfoNew.AngularAccelerationY = CurJoyState.AngularAccelerationY; m_JoystickInfoNew.AngularAccelerationZ = CurJoyState.AngularAccelerationZ; m_JoystickInfoNew.AccelerationX = CurJoyState.AccelerationX; m_JoystickInfoNew.AccelerationY = CurJoyState.AccelerationY; m_JoystickInfoNew.AccelerationZ = CurJoyState.AccelerationZ; bool bIsSameJs = CompareJoystickInfoClass(m_JoystickInfoLast, m_JoystickInfoNew); } } catch (Exception ex) { curJoystick.Unacquire(); JoystickName = ""; } }
private bool CompareJoystickInfoClass(JoystickInfoClass J1, JoystickInfoClass J2) { bool isMatch = true;//默认一致 if (!ObjectEquel(J1, J2)) { //如果碰到某个字段或属性值不一致,直接返回false isMatch = false; return(isMatch); } //数组类型的暂时不知道怎么判断,只能直接for判断 for (int i = 0; i < J1.Buttons.Length; i++) { if (J1.Buttons[i] != J2.Buttons[i]) { isMatch = false; break; } } for (int i = 0; i < J1.PointOfViewControllers.Length; i++) { if (J1.PointOfViewControllers[i] != J2.PointOfViewControllers[i]) { isMatch = false; break; } } for (int i = 0; i < J1.Sliders.Length; i++) { if (J1.Sliders[i] != J2.Sliders[i]) { isMatch = false; break; } } for (int i = 0; i < J1.VelocitySliders.Length; i++) { if (J1.VelocitySliders[i] != J2.VelocitySliders[i]) { isMatch = false; break; } } for (int i = 0; i < J1.AccelerationSliders.Length; i++) { if (J1.AccelerationSliders[i] != J2.AccelerationSliders[i]) { isMatch = false; break; } } for (int i = 0; i < J1.ForceSliders.Length; i++) { if (J1.ForceSliders[i] != J2.ForceSliders[i]) { isMatch = false; break; } } return(isMatch); }