C# 摇杆设备连接,获取键值
程序员文章站
2022-03-04 13:09:51
...
摇杆设备连接类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SharpDX.DirectInput;
namespace Joystick
{
public class JoystickObj
{
public delegate void JoystickMoveDelegate(int JoyX, int JoyY);
public event JoystickMoveDelegate JoystickMoveEvent;
public delegate void JoystickButtonDelegate(int KeyNum);
public event JoystickButtonDelegate JoystickButtonEvent;
public delegate void JoystickConnectStateDelegate();
public event JoystickConnectStateDelegate JoystickConnectEvent;
public event JoystickConnectStateDelegate JoystickDisConnectEvent;
//public Action<int, int> JoystickMoveEvent;
//public Action<int> JoystickButtonEvent;
//public Action JoystickConnectEvent;
//public Action JoystickDisConnectEvent;
public bool IsFocusZoomButtonDown = false;
private DirectInput dirInput;
private SharpDX.DirectInput.Joystick curJoystick;
public bool isAcquire = false;
private int JoystickX = 0;
private int JoystickY = 0;
private int JoystickB = 0;
private string jsName = string.Empty;
public JoystickObj(string Name)
{
jsName = Name;
dirInput = new DirectInput();
}
public void Connect()
{
Task.Run(new Action(JoystickAcquire));
}
public void JoystickAcquire()
{
while (true)
{
try
{
if (App.CurrentWindowState == WindowStateType.Login)
{
return;
}
if (!isAcquire)
{
IList<DeviceInstance> allDevices = dirInput.GetDevices();
curJoystick = new SharpDX.DirectInput.Joystick(dirInput, allDevices.First(name => name.ProductName == jsName).InstanceGuid);
curJoystick.Acquire();
Task.Run(new Action(JoystickTask));
isAcquire = true;
JoystickConnectEvent?.Invoke();
}
}
catch
{
isAcquire = false;
}
System.Threading.Thread.Sleep(1000);
}
}
public void JoystickTask()
{
try
{
while (true)
{
if (App.CurrentWindowState == WindowStateType.Login)
{
return;
}
JoystickState joys = curJoystick.GetCurrentState();
if (joys.Buttons.Count(B => B == true) > 0)
{
for (int Key = 0; Key < 30; Key++)
{
if (joys.Buttons[Key])
{
if (JoystickB != Key + 1)
{
JoystickB = Key + 1;
JoystickButtonEvent?.Invoke(JoystickB);
break;
}
else { break; }
}
}
}
else
{
JoystickB = 0;
if (IsFocusZoomButtonDown)
{
JoystickButtonEvent?.Invoke(JoystickB);
IsFocusZoomButtonDown = false;
}
}
if (joys.X - 32767 != JoystickX || joys.Y - 32767 != JoystickY)
{
JoystickX = joys.X - 32767;
JoystickY = joys.Y - 32767;
JoystickMoveEvent?.Invoke(JoystickX, JoystickY);
}
System.Threading.Thread.Sleep(50);
}
}
catch
{
isAcquire = false;
JoystickDisConnectEvent?.Invoke();
}
}
}
}
调用:
JoystickObj _joystickOBJ = null;
private bool StartJoystickObj(string Name)
{
try
{
_joystickOBJ = new JoystickObj(Name);
_joystickOBJ.Connect();
_joystickOBJ.JoystickConnectEvent += _joystickOBJ_JoystickConnectEvent;
_joystickOBJ.JoystickDisConnectEvent += _joystickOBJ_JoystickDisConnectEvent;
_joystickOBJ.JoystickMoveEvent += _joystickOBJ_JoystickMoveEvent;
_joystickOBJ.JoystickButtonEvent += _joystickOBJ_JoystickButtonEvent;
GetInitData();
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
return false;
}
在其他主类中启动:StartJoystickObj(PublicStaticVariables.JoystickName);
private void _joystickOBJ_JoystickMoveEvent(int JoyX, int JoyY)
{
try
{
if (JoyX < 0) { }
if (JoyX > 0) { }
if (JoyY < 0) { }
if (JoyY > 0) { }
}
catch (Exception ex)
{
}
}
private void _joystickOBJ_JoystickButtonEvent(int KeyNum)
{
try
{
//根据具体键值来写操作逻辑
if (KeyNum > 0 && KeyNum < 10)
{
}
//设置控制方式
if (KeyNum == 16)
{
if (JoystickFunState == 0)
{
JoystickFunState = 1;
}
else
{
JoystickFunState = 0;
}
}
//设置控制类型
if (KeyNum == 13 && _isStickInZero)
{
}
//
if (KeyNum == 12)
{
}
//复位一些状态
if (KeyNum == 20)
{
}
//
if (KeyNum == 18)
{
}
//
if (KeyNum == 19)
{
}
//......
}
catch (Exception ex)
{
}
}