A package that provides a friendly Dart API for accessing gamepads connected to a Windows machine.
This package builds on top of the Dart win32 package, offering a high-level Dart wrapper that avoids the need for users to understand FFI or write directly to the Win32 API.
Usage
Having imported the package, you can get a Gamepad
object as follows:
final gamepad = Gamepad(0); // primary controller
Windows allows up to four gamepads to be connected at once. You can poll the
gamepad for status by calling updateState()
and
then inspecting the state
object during a game loop;
this contains values for the various buttons, triggers and thumbsticks.
You can also trigger vibrations with the vibrate
method, which activates one of the two rumble motors on a typical gamepad.
Example
A simple example of using the gamepad:
import 'dart:io';
import 'package:win32_gamepad/win32_gamepad.dart';
void main() {
for (var idx = 0; idx < 4; idx++) {
final gamepad = Gamepad(idx);
final connectionStatus =
gamepad.state.isConnected ? 'connected' : 'disconnected';
print('Gamepad $idx is $connectionStatus.');
}
print('Testing first gamepad.');
final gamepad = Gamepad(0);
if (gamepad.isConnected) {
final GamepadBatteryInfo(:batteryLevel, :batteryType) =
gamepad.gamepadBatteryInfo;
print('Battery type is ${batteryType.name}.');
print('Battery level is ${batteryLevel.name}.');
print('Vibrating left motor (half intensity).');
gamepad.vibrate(leftMotorSpeed: 32767);
sleep(const Duration(milliseconds: 1000));
print('Vibrating right motor (half intensity).');
gamepad.vibrate(rightMotorSpeed: 32767);
sleep(const Duration(milliseconds: 1000));
print('Vibrating both motors (full intensity).');
gamepad.vibrate(leftMotorSpeed: 65535, rightMotorSpeed: 65535);
sleep(const Duration(milliseconds: 1000));
print('Turning off vibration.');
gamepad.vibrate(leftMotorSpeed: 0, rightMotorSpeed: 0);
}
}
Alternatively, a more sophisticated example is available in the
example\inspector\
directory in the form of a Flutter app that shows how you
can use this to monitor the status of buttons in a simple game loop:
Feature requests and bugs
Please file feature requests and bugs at the issue tracker.
Libraries
- win32_gamepad
- A Dart library that provides a friendly Dart API for accessing gamepads connected to a Windows machine.