本文整理汇总了C#中Joystick.Acquire方法的典型用法代码示例。如果您正苦于以下问题:C# Joystick.Acquire方法的具体用法?C# Joystick.Acquire怎么用?C# Joystick.Acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Joystick
的用法示例。
在下文中一共展示了Joystick.Acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CaptureJoysticks
public void CaptureJoysticks()
{
// Initialize DirectInput
var directInput = new DirectInput();
// Find all joysticks connected to the system
IList<DeviceInstance> connectedJoysticks = new List<DeviceInstance>();
// - look for gamepads
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
{
connectedJoysticks.Add(deviceInstance);
}
// - look for joysticks
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
{
connectedJoysticks.Add(deviceInstance);
}
// Use the two first joysticks found
if (connectedJoysticks.Count >= 1)
{
joystick1 = new Joystick(directInput, connectedJoysticks[0].InstanceGuid);
Joystick1DeviceName = connectedJoysticks[0].InstanceName;
joystick1.Acquire();
}
if (connectedJoysticks.Count >= 2)
{
joystick2 = new Joystick(directInput, connectedJoysticks[1].InstanceGuid);
Joystick2DeviceName = connectedJoysticks[1].InstanceName;
joystick2.Acquire();
}
}
示例2: HandleJoystick
private static void HandleJoystick()
{
// Initialize DirectInput
var directInput = new DirectInput();
// Find a Joystick Guid
var joystickGuid = Guid.Empty;
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Gamepad not found, look for a Joystick
if (joystickGuid == Guid.Empty)
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
Debug.WriteLine("No Gamepad found.");
Environment.Exit(1);
}
// Instantiate the joystick
var joystick = new Joystick(directInput, joystickGuid);
Debug.WriteLine("Found Gamepad with GUID: {0}", joystickGuid);
// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;
// Acquire the joystick
joystick.Acquire();
// Poll events from joystick every 1 millisecond
var timer = Observable.Interval(TimeSpan.FromMilliseconds(1));
timer
// Get all joystick input
.SelectMany(_ => { joystick.Poll(); return joystick.GetBufferedData(); })
// Filter only menu key button (xbox one controller)
.Where(a => a.Offset == JoystickOffset.Buttons6)
// Input will contain UP and Down button events
.Buffer(2)
// If button was pressed longer than a second
.Where(t => (TimeSpan.FromMilliseconds(t.Last().Timestamp) - TimeSpan.FromMilliseconds(t.First().Timestamp)).TotalSeconds >= 1)
// Press and hold F key for 1.5s
.Subscribe(t =>
{
SendKeyDown(KeyCode.KEY_F);
System.Threading.Thread.Sleep(1500);
SendKeyUp(KeyCode.KEY_F);
});
}
示例3: GamePadDevice
/// <summary>
/// 番号指定でデバイス生成。番号のデバイスが存在しなければ例外を投げる。
/// </summary>
/// <param name="window"></param>
/// <param name="padNumber">0始まり</param>
public GamePadDevice(int padNumber, DirectInput directInput)
{
var devices = GetDevices(directInput);
if (devices.Count <= padNumber)
{
throw new Exception("指定された数のパッドがつながれていない");
}
try
{
Stick = new Joystick(directInput, devices[padNumber].InstanceGuid);
//Stick.SetCooperativeLevel(window, CooperativeLevel.Exclusive | CooperativeLevel.Foreground);
}
catch (Exception e)
{
throw new Exception("パッドの初期化に失敗", e);
}
///スティックの範囲設定
foreach (var item in Stick.GetObjects())
{
//if ((item.ObjectType & .Axis) != 0)
//{
// Stick.GetObjectPropertiesById((int)item.ObjectType).SetRange(-1000, 1000);
//}
}
Stick.Acquire();
}
示例4: Lock_DX_Devices
//Disable all dinput devices for xinput controllers
public void Lock_DX_Devices()
{
var directInput = new DirectInput();
try
{
IList<DeviceInstance> devicelist = directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly);
foreach (DeviceInstance cdevice in devicelist)
{
if (cdevice.InstanceName.Trim('\0') == XINPUT_NAME)
{
var joystick = new Joystick(directInput, cdevice.InstanceGuid);
joystick.Acquire();
Guid deviceGUID = joystick.Properties.ClassGuid;
string devicePath = joystick.Properties.InterfacePath;
joystick.Unacquire();
string[] dpstlit = devicePath.Split('#');
devicePath = @"HID\" + dpstlit[1].ToUpper() + @"\" + dpstlit[2].ToUpper();
lockedDevices.Add(new DeviceID(deviceGUID, devicePath));
DeviceHelper.SetDeviceEnabled(deviceGUID, devicePath, false);
}
}
}
finally
{
directInput.Dispose();
}
}
示例5: Initialize
public static void Initialize()
{
if (dinput == null)
dinput = new DirectInput();
Devices = new List<GamePad>();
foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
Console.WriteLine("joydevice: {0} `{1}`", device.InstanceGuid, device.ProductName);
if (device.ProductName.Contains("XBOX 360"))
continue; // Don't input XBOX 360 controllers into here; we'll process them via XInput (there are limitations in some trigger axes when xbox pads go over xinput)
var joystick = new Joystick(dinput, device.InstanceGuid);
joystick.SetCooperativeLevel(GlobalWin.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
}
joystick.Acquire();
GamePad p = new GamePad(device.InstanceName, device.InstanceGuid, joystick);
Devices.Add(p);
}
}
示例6: DirectInputManager
public DirectInputManager()
{
var directInput = new DirectInput();
// Prefer a Driving device but make do with fallback to a Joystick if we have to
var deviceInstance = FindAttachedDevice(directInput, DeviceType.Driving);
if (null == deviceInstance)
deviceInstance = FindAttachedDevice(directInput, DeviceType.Joystick);
if (null == deviceInstance)
throw new Exception("No Driving or Joystick devices attached.");
joystickType = (DeviceType.Driving == deviceInstance.Type ? JoystickTypes.Driving : JoystickTypes.Joystick);
// A little debug spew is often good for you
Log.Spew("First Suitable Device Selected \"" + deviceInstance.InstanceName + "\":");
Log.Spew("\tProductName: " + deviceInstance.ProductName);
Log.Spew("\tType: " + deviceInstance.Type);
Log.Spew("\tSubType: " + deviceInstance.Subtype);
// Data for both Driving and Joystick device types is received via Joystick
joystick = new Joystick(directInput, deviceInstance.InstanceGuid);
IntPtr consoleWindowHandle = GetConsoleWindow();
if (IntPtr.Zero != consoleWindowHandle)
{
CooperativeLevel cooperativeLevel = CooperativeLevel.Background | CooperativeLevel.Nonexclusive;
Log.Spew("Console window cooperative level: " + cooperativeLevel);
if (!joystick.SetCooperativeLevel(consoleWindowHandle, cooperativeLevel).IsSuccess)
throw new Exception("Failed to set cooperative level for DirectInput device in console window.");
}
var result = joystick.Acquire();
if (!result.IsSuccess)
throw new Exception("Failed to acquire DirectInput device.");
Log.Spew("Joystick acquired.");
}
示例7: Reconnect
public override void Reconnect()
{
// search for devices
foreach (DeviceInstance device in _DInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
// create the device
try
{
_Joy = new Joystick(_DInput, device.InstanceGuid);
_Joy.SetCooperativeLevel(wnd_int, CooperativeLevel.Nonexclusive | CooperativeLevel.Background);
break;
}
catch (DirectInputException)
{
}
}
if (_Joy == null)
{
IsConnected = false;
Name = "";
Type = "";
return;
}
_Joy.Acquire();
IsConnected = true;
Name = _Joy.Information.ProductName;
Type = "DirectInput";
}
示例8: GamepadReader
public GamepadReader()
{
Buttons = _buttons;
Analogs = _analogs;
_dinput = new DirectInput();
var devices = _dinput.GetDevices (DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly);
if (devices.Count < 1) {
throw new IOException ("GamepadReader could not find a connected gamepad.");
}
_joystick = new Joystick (_dinput, devices[0].InstanceGuid);
foreach (var obj in _joystick.GetObjects()) {
if ((obj.ObjectType & ObjectDeviceType.Axis) != 0) {
_joystick.GetObjectPropertiesById ((int)obj.ObjectType).SetRange (-RANGE, RANGE);
}
}
if (_joystick.Acquire().IsFailure) {
throw new IOException ("Connected gamepad could not be acquired.");
}
_timer = new DispatcherTimer ();
_timer.Interval = TimeSpan.FromMilliseconds (TIMER_MS);
_timer.Tick += tick;
_timer.Start ();
}
示例9: MainController
public MainController()
{
var directInput = new DirectInput();
LogicState = new LogicState();
foreach (var deviceInstance in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
try
{
gamepad = new Joystick(directInput, deviceInstance.InstanceGuid);
gamepad.SetCooperativeLevel(Parent, CooperativeLevel.Exclusive | CooperativeLevel.Foreground);
break;
}
catch (DirectInputException) { }
}
if (gamepad == null)
return;
foreach (var deviceObject in gamepad.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
gamepad.GetObjectPropertiesById((int) deviceObject.ObjectType).SetRange(-1000, 1000);
}
gamepad.Acquire();
}
示例10: SimpleJoystick
///
/// Construct, attach the joystick
///
public SimpleJoystick()
{
DirectInput dinput = new DirectInput();
// Search for device
foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
// Create device
try
{
Joystick = new Joystick(dinput, device.InstanceGuid);
break;
}
catch (DirectInputException)
{
}
}
if (Joystick == null)
throw new Exception("No joystick found");
foreach (DeviceObjectInstance deviceObject in Joystick.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
Joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
}
// Acquire sdevice
Joystick.Acquire();
}
示例11: GetJoystick
private Joystick GetJoystick()
{
var directInput = new DirectInput();
var form = new Form();
foreach (var device in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
var controller = new Joystick(directInput, device.InstanceGuid);
controller.SetCooperativeLevel(form.Handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
var retries = 0;
while (controller.Acquire().IsFailure)
{
retries++;
if (retries > 500)
throw new Exception("Couldnt acquire SlimDX stick");
}
if (controller.Information.InstanceName.Contains("PPJoy"))
{
foreach (DeviceObjectInstance deviceObject in controller.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
controller.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
}
return controller;
}
}
return null;
}
示例12: InitializeController
public void InitializeController(Guid initGuid)
{
controllerGuid = Guid.Empty;
var deviceInst = input.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly);
if (deviceInst.Count == 0)
{
deviceInst = input.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly);
}
if (deviceInst.Count > 0)
{
foreach (var device in deviceInst)
{
if (device.InstanceGuid == initGuid)
{
controllerGuid = initGuid;
}
}
if (controllerGuid == Guid.Empty)
{
controllerGuid = deviceInst[0].InstanceGuid;
}
controller = new Joystick(input, controllerGuid);
controller.Acquire();
defaultControllerState = controller.GetCurrentState();
}
}
示例13: btnOpenPad_Click
private void btnOpenPad_Click(object sender, EventArgs e)
{
if (jp != null)
jp.Dispose();
jp = new Joystick(di, (cbPad.SelectedItem as xJoypad).dix.InstanceGuid);
jp.Acquire();
lblPadStatus.Text = "Open: " + jp.Information.InstanceName;
}
示例14: GamePad
public GamePad()
{
// Initialize DirectInput
directInput = new DirectInput();
// Find a Joystick Guid
joystickGuid = Guid.Empty;
// Find a Gamepad
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Gamepad not found, look for a Joystick
if (joystickGuid == Guid.Empty)
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
Logger.Log(this, "No joystick/Gamepad found.", 2);
}
else
{
// Instantiate the joystick
joystick = new Joystick(directInput, joystickGuid);
Logger.Log(this, String.Format("Found Joystick/Gamepad with GUID: {0}", joystickGuid), 1);
// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;
// Acquire the joystick
joystick.Acquire();
// Poll events from joystick
//while (true)
//{
// joystick.Poll();
// var datas = joystick.GetBufferedData();
// foreach (var state in datas)
// Console.WriteLine(state);
//}
timer = new Timer();
timer.Interval = TIMER_INTERVAL_IN_MS;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
示例15: UseDevices
private void UseDevices(IList<DeviceInstance> devices)
{
var guid = devices[0].InstanceGuid; // use first one
_joyStick = new Joystick(directInput, guid);
_joyStick.Properties.BufferSize = 128; // enable buffer
_joyStick.Acquire();
var timer = new DispatcherTimer(); // Timer(onTimer, null, 100);
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}