開發平台:VS2010 + .NET Framework 4.0
1. 下載 DirectX SDK
2. 建立Windows Form Application
3. References中加入Microsoft.DirectX.DirectInput.dll
(C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0\Microsoft.DirectX.DirectInput.dll)
4. 程式撰寫
using Microsoft.DirectX.DirectInput;
private void InitDevices()
{
foreach ( DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
//Throw exception if joystick not found.
throw new Exception("No joystick found.");
}
private void ConfigureDevice()
{
//Set joystick axis ranges.
foreach (DeviceObjectInstance doi in joystick.Objects)
{
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(ParameterHow.ById, doi.ObjectId, new InputRange(-1000, 1000));
}
}
//Set joystick axis mode absolute.
joystick.Properties.AxisModeAbsolute = true;
//set cooperative level.
joystick.SetCooperativeLevel(this, CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
joystick.Acquire();
}
//Get Joystick State.
JoystickState state = joystick.CurrentJoystickState;
//Capture Buttons.
byte[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i] != 0)
{
//your code here
}
}
//Point of View
int[] POV = state.GetPointOfView();
//Slider
int[] Slider = state.GetSlider();
LoaderLock例外狀況
Debug -> Exceptions -> Managed Debugging Assistants -> LoaderLock Thrown選項取消.NET Framework例外狀況,修改app.config
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>
沒有留言:
張貼留言