本文整理汇总了C#中Joystick.Poll方法的典型用法代码示例。如果您正苦于以下问题:C# Joystick.Poll方法的具体用法?C# Joystick.Poll怎么用?C# Joystick.Poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Joystick
的用法示例。
在下文中一共展示了Joystick.Poll方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
});
}
示例2: MainForJoystick
static void MainForJoystick()
{
// 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)
{
Console.WriteLine("No joystick/Gamepad found.");
Console.ReadKey();
Environment.Exit(1);
}
// Instantiate the joystick
var joystick = new Joystick(directInput, joystickGuid);
Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);
// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
Console.WriteLine("Effect available {0}", effectInfo.Name);
// 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);
}
}
示例3: pollJoystickLoop
private void pollJoystickLoop(Joystick joystick)
{
joystick.Properties.BufferSize = 128;
joystick.Acquire();
try
{
while (true)
{
joystick.Poll();
var data = joystick.GetBufferedData();
foreach (var state in data)
{
FireJoystickInputEvent(state);
}
}
}
catch(SharpDX.SharpDXException)
{
mLogger.Info("Gamepad disconnected");
}
}
示例4: _Joystic
public static void _Joystic()
{
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;
_is_joysticConnect = true;
}
// 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;
_is_joysticConnect = true;
}
// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
Console.WriteLine("No joystick/Gamepad found.");
_is_joysticConnect = false;
}
if (_is_joysticConnect)
{
var joystick = new Joystick(directInput, joystickGuid);
Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);
// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
Console.WriteLine("Effect available {0}", effectInfo.Name);
// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;
// Acquire the joystick
joystick.Acquire();
// Poll events from joystick
var joystickState = new JoystickState();
bool work;
while (_continue )
{
work = false;
joystick.Poll();
var data = joystick.GetBufferedData();
foreach (var state in data)
{
if (state.Offset == JoystickOffset.X)
{
yaw_curr = ((32767 - state.Value) / 4096);
if (Math.Abs(yaw_curr - yaw_prev) >3 )
{
string mes = Convert.ToString(yaw_curr);
while (mes.Length < 5)
{
mes = ' ' + mes;
}
_serialPort.WriteLine('y' + mes);
yaw_prev = yaw_curr;
}
work = true;
}
if (state.Offset == JoystickOffset.Z)
{
pitch_curr = ((32767 - state.Value) / 20) + pitch0;
if(Math.Abs(pitch_curr - pitch_prev)>100)
{
string mes = Convert.ToString(pitch_curr);
while (mes.Length < 5)
{
mes = ' ' + mes;
}
_serialPort.WriteLine('p'+mes);
pitch_prev = pitch_curr;
}
work = true;
}
if (state.Offset == JoystickOffset.RotationZ)
{
roll_curr = ((state.Value - 32767) / 20) + roll0;
if (Math.Abs(roll_curr - roll_prev) > 100)
{
string mes = Convert.ToString(roll_curr);
while (mes.Length < 5)
{
mes = ' ' + mes;
//.........这里部分代码省略.........
示例5: Main
static void Main(string[] args)
{
if (client == null)
{
// create client instance
client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId, "guest", "guest");
SubscribeMessage();
}
// 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)
{
Console.WriteLine("No joystick/Gamepad found.");
Console.ReadKey();
Environment.Exit(1);
}
// Instantiate the joystick
var joystick = new Joystick(directInput, joystickGuid);
Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);
// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
Console.WriteLine("Effect available {0}", effectInfo.Name);
// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;
// Acquire the joystick
joystick.Acquire();
const int stop = 32511;
// Poll events from joystick
while (true)
{
joystick.Poll();
var datas = joystick.GetBufferedData();
foreach (var state in datas)
{
if (state.Offset.ToString() == "X" && state.Value > stop)
{
MoveRobot("MOVE", "R");
Console.WriteLine("Kanan");
}
else if(state.Offset.ToString() == "X" && state.Value < stop)
{
MoveRobot("MOVE", "L");
Console.WriteLine("Kiri");
}
else if (state.Offset.ToString() == "Y" && state.Value < stop)
{
MoveRobot("MOVE", "F");
Console.WriteLine("Maju");
}
else if (state.Offset.ToString() == "Y" && state.Value > stop)
{
MoveRobot("MOVE", "B");
Console.WriteLine("Mundur");
}
else
{
MoveRobot("MOVE", "S");
Console.WriteLine("Stop");
}
}
}
}
示例6: MainForJoystick
static void MainForJoystick()
{
// 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)
{
Console.WriteLine("No joystick/Gamepad found.");
Console.ReadKey();
Environment.Exit(1);
}
// Instantiate the joystick
var joystick = new Joystick(directInput, joystickGuid);
Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);
// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
Console.WriteLine("Effect available {0}", effectInfo.Name);
// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;
// Acquire the joystick
joystick.Acquire();
string s = "there is a cat";
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
// Poll events from joystick
var originX = 0;
var originY = 0;
var lastStateX = -1;
var lastStateY = -1;
var startFlag = false;
while (true)
{
joystick.Poll();
var datas = joystick.GetBufferedData();
double theta = 0.0;
double radius = 0.0;
foreach (var state in datas)
{
if (state.Sequence == 1)
{
s = (state.Offset).ToString();
if (s == "X")
originX = state.Value;
else if (s == "Y")
originY = state.Value;
}
s = (state.Offset).ToString();
if (s == "X")
lastStateX = state.Value;
else if (s == "Y")
lastStateY = state.Value;
startFlag = true;
}
if (startFlag && datas.Length == 0 && lastStateX != -1 && lastStateY != -1)
{
var slope = 0.0;
if ((lastStateX - originX) == 0)
slope = Math.PI / 2;
else
slope = (lastStateY - originY) / (lastStateX - originX);
theta = Math.Atan(slope);
radius = Math.Sqrt(Math.Pow(originX - lastStateX, 2) + Math.Pow(originY - lastStateY, 2));
originX = lastStateX;
originY = lastStateY;
Console.WriteLine(theta);
Console.WriteLine(radius);
startFlag = false;
}
}
}
示例7: RefreshJoystickState
private void RefreshJoystickState(Joystick joystick, ZXSpectrum.Joystick zxJoystick)
{
// Set BufferSize in order to use buffered data.
// joystick.Properties.BufferSize = 128;
// Poll events from joystick
// joystick.Poll();
// var datas = joystick.GetBufferedData();
joystick.Poll();
JoystickState currentState = joystick.GetCurrentState();
ZXSpectrum.JoystickHorizontalPosition newHorizontalPosition = ZXSpectrum.JoystickHorizontalPosition.Center;
if (currentState.X < 16383)
{
newHorizontalPosition = ZXSpectrum.JoystickHorizontalPosition.Left;
}
else if (currentState.X > 49150)
{
newHorizontalPosition = ZXSpectrum.JoystickHorizontalPosition.Right;
}
ZXSpectrum.JoystickVerticalPosition newVerticalPosition = ZXSpectrum.JoystickVerticalPosition.Center;
if (currentState.Y < 16383)
{
newVerticalPosition = ZXSpectrum.JoystickVerticalPosition.Up;
}
else if (currentState.Y > 49150)
{
newVerticalPosition = ZXSpectrum.JoystickVerticalPosition.Down;
}
bool newButtonPressedState = currentState.Buttons[0];
zxJoystick.RefreshCurrentPosition(newVerticalPosition, newHorizontalPosition, newButtonPressedState);
}
示例8: pollJoystick
private void pollJoystick(Guid joystickGuid)
{
Joystick joystick;
// Instantiate the joystick
joystick = new Joystick(directInput, joystickGuid);
joystick.Properties.BufferSize = 128;
Console.WriteLine("Joystick {0}", joystick.Properties.ProductName.ToString());
Console.WriteLine("GUID: {0}", joystickGuid);
// Query all suported ForceFeedback effects
// TODO - maybe look into vibration effects on gamepads?
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
{
Console.WriteLine("Effect available {0}", effectInfo.Name);
}
// Acquire the joystick
joystick.Acquire();
// Poll events from joystick
while (true)
{
joystick.Poll();
var data = joystick.GetBufferedData();
foreach (var state in data)
{
//Null characters in the device name clogs up the write buffer when not run as a Console app. Replace all \0 with empty string to fix this.
Console.WriteLine("{0} - {1} - {2}", joystick.Properties.InstanceName.Replace("\0", ""), state.Offset, state.Value);
if (state.Offset.ToString().StartsWith("Button"))
{
buttonPressed(state.Offset, state.Value);
Console.WriteLine("Button {0} state changed to {1}", state.Offset, state.Value);
}
else if (state.Offset.ToString().StartsWith("Point"))
{
// POV controller
povChanged(state.Offset, state.Value);
}
else
{
//is an axis
int range = 32767; // this is for main joysticks. Might have to change if there are issues
axisMoved(state.Offset, state.Value, range);
}
}
}
}
示例9: joystickManagerThreadStart
private void joystickManagerThreadStart()
{
// 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;
foreach (var deviceInstance in directInput.GetDevices())
{
if (deviceInstance.InstanceName.Contains("X52 Pro"))
{
Console.WriteLine(deviceInstance.InstanceName+", "+deviceInstance.InstanceGuid);
joystickGuid = deviceInstance.InstanceGuid;
}
}
// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
Console.WriteLine("No joystick/Gamepad found.");
Console.ReadKey();
Environment.Exit(1);
}
// Instantiate the joystick
joystick = new Joystick(directInput, joystickGuid);
Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);
// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
Console.WriteLine("Effect available {0}", effectInfo.Name);
// 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)
{
if (state.Offset == JoystickOffset.Y)
{
Y_Axis = (state.Value - (Y_MAX_VAL / 2.0)) / (Y_MAX_VAL / 2.0);
if (Math.Abs(Y_Axis) <= 0.025)
Y_Axis = 0;
if (Y_INVERT)
Y_Axis *= -1;
}
if (state.Offset == JoystickOffset.X)
{
X_Axis = (state.Value - (X_MAX_VAL / 2.0)) / (X_MAX_VAL / 2.0);
if (Math.Abs(X_Axis) <= DEAD_ZONE)
X_Axis = 0;
if (X_INVERT)
X_Axis *= -1;
}
if(state.Offset == JoystickOffset.Z)
{
throttle = (state.Value) / ((double)X_MAX_VAL);
if (THROTTLE_INVERT)
throttle = (1 - throttle);
}
if(state.Offset == JoystickOffset.Buttons0)
{
if (state.Value == 128)
fire = true;
else
fire = false;
}
}
}
}