Mtf.Joystick 2.0.3

dotnet add package Mtf.Joystick --version 2.0.3
                    
NuGet\Install-Package Mtf.Joystick -Version 2.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mtf.Joystick" Version="2.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mtf.Joystick" Version="2.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Mtf.Joystick" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mtf.Joystick --version 2.0.3
                    
#r "nuget: Mtf.Joystick, 2.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Mtf.Joystick&version=2.0.3
                    
Install Mtf.Joystick as a Cake Addin
#tool nuget:?package=Mtf.Joystick&version=2.0.3
                    
Install Mtf.Joystick as a Cake Tool

JoystickHandler Class

The JoystickHandler class provides functionality to handle joystick input using the SharpDX.DirectInput library. It enables polling joystick states, reacting to axis movements, and triggering actions for button presses.

Namespace

Mtf.Joystick

Methods

InitializeJoystick

Initializes and starts listening to joystick input. It polls the joystick for its current state, interprets axis movements, and triggers associated actions.

Parameters
  • Func<int> getDeviceIndex: Function to provide the correct joystick index.
  • Func<bool> continuePulling: Function to determine if polling should continue.
  • Func<int> getDeltaModifier: Function to provide a value for scaling joystick axis movements.
  • Func<int> getMinimumDelta: Function to provide the minimum threshold for movement detection.
  • Action<int, int> restAction: Action to invoke when the joystick is at rest.
  • Action<int, int> forwardOrBackwardAction: Action to invoke for forward/backward movement.
  • Action<int, int> forwardWithLeftTurnAction: Action to invoke for forward with a left turn.
  • Action<int, int> forwardWithRightTurnAction: Action to invoke for forward with a right turn.
  • Action<int, int> backwardWithLeftTurnAction: Action to invoke for backward with a left turn.
  • Action<int, int> backwardWithRightTurnAction: Action to invoke for backward with a right turn.
  • Action<int, int> turnLeftOrRightAction: Action to invoke for turning left or right.
  • Action afterPullingAction: Action to invoke after polling stops.
  • Action[] buttonActions: Array of actions to invoke for button presses.
Returns
  • bool: To indicate if the operation was successful or not.

CalibrateJoystick

Calibrate the joystick.

Returns
  • bool: To indicate if the operation was successful or not.

StopJoystick

Stops joystick polling.


Example Usage

Here is a simplified example demonstrating how to use the JoystickHandler:

using System;
using System.Threading;

var succeeded = JoystickHandler.InitializeJoystick(
    getDeviceIndex: () => 0,
    continuePulling: () => true,
    getDeltaModifier: () => 10,
    getMinimumDelta: () => 5,
    restAction: (deltaX, deltaY) => Console.WriteLine("Joystick at rest."),
    forwardOrBackwardAction: (deltaX, deltaY) => Console.WriteLine($"Moving forward/backward: {deltaY}"),
    forwardWithLeftTurnAction: (deltaX, deltaY) => Console.WriteLine($"Forward with left turn: {deltaX}, {deltaY}"),
    forwardWithRightTurnAction: (deltaX, deltaY) => Console.WriteLine($"Forward with right turn: {deltaX}, {deltaY}"),
    backwardWithLeftTurnAction: (deltaX, deltaY) => Console.WriteLine($"Backward with left turn: {deltaX}, {deltaY}"),
    backwardWithRightTurnAction: (deltaX, deltaY) => Console.WriteLine($"Backward with right turn: {deltaX}, {deltaY}"),
    turnLeftOrRightAction: (deltaX, deltaY) => Console.WriteLine($"Turning left/right: {deltaX}"),
    afterPullingAction: () => Console.WriteLine("Polling stopped."),
    buttonActions: new Action[]
    {
        () => Console.WriteLine("Button 1 pressed."),
        () => Console.WriteLine("Button 2 pressed."),
        () => Console.WriteLine("Button 3 pressed."),
    }
);

// Simulate stopping the joystick handler after some time
Thread.Sleep(5000);
JoystickHandler.StopJoystick();

Notes

  1. The library uses SharpDX.DirectInput for device communication. Ensure the SharpDX library is installed.
  2. The joystick must be connected, and the DeviceClass.GameControl class is used to detect input devices.
  3. All movements and button presses are translated into corresponding actions, making this class versatile for various input scenarios.
  4. Remember to dispose of the cancellation token properly after stopping the joystick.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.3 91 3/27/2025
2.0.2 79 1/20/2025
1.0.0 110 11/17/2024