switch-joy-con
Use Nintendo Switch Joy-Cons as input devices (Bluetooth).
Features
- Run callback(s) when buttons are pressed/released/changed
- Toggle player LEDs
Installation
npm install --save switch-joy-con
Usage
const listConnectedJoyCons = ; // First, list all the Joy-Cons connected to the computer.const devices = ;console;// [// {// vendorId: 1406,// productId: 8198,// path: 'IOService:/IOResources/IOBluetoothHCIController/AppleBroadcomBluetoothHostController/IOBluetoothDevice/IOBluetoothL2CAPChannel/IOBluetoothHIDDriver',// serialNumber: '94-58-cb-a6-1b-86',// manufacturer: 'Unknown',// product: 'Joy-Con (L)',// release: 1,// interface: -1,// usagePage: 1,// usage: 5,// open: [Function: open]// },// {// vendorId: 1406,// productId: 8199,// path: 'IOService:/IOResources/IOBluetoothHCIController/AppleBroadcomBluetoothHostController/IOBluetoothDevice/IOBluetoothL2CAPChannel/IOBluetoothHIDDriver',// serialNumber: '94-58-cb-a5-cb-9d',// manufacturer: 'Unknown',// product: 'Joy-Con (R)',// release: 1,// interface: -1,// usagePage: 1,// usage: 5,// open: [Function: open]// }// ] // Decide which to use, and then call `open` on it.const left = devices0; // The `buttons` property always contains up-to-date buttons state:console;// {// dpadUp: false,// dpadDown: false,// dpadLeft: false,// dpadRight: false,// minus: false,// screenshot: false,// sl: false,// sr: false,// l: false,// zl: false,// analogStickPress: false,// analogStick: left.Directions.NEUTRAL// } // A "change" event will be emitted whenever the button state changesleft; // Whenever a button is pressed or released, a `down:${buttonName}` or `up:${buttonName}` event is emitted.left;left; // a `change:${buttonName}` event is also emitted:left; // The `analogStick` "button" is a number. Use the `Directions` property on the// instance to understand its value:left; // When you're done with the device, call `close` on it:left; // Usage with a right-side Joy-Con is the same, but it has different button names:const right = devices1;console;// {// a: false,// x: false,// b: false,// y: false,// plus: false,// home: false,// sl: false,// sr: false,// r: false,// zr: false,// analogStickPress: false,// analogStick: right.Directions.NEUTRAL// } // If you need to tell whether a Joy-Con is a left or right Joy-Con, use the `side` property:console; // "left"console; // "right"
API Documentation
The "switch-joy-con"
module has one named export, listConnectedJoyCons
.
listConnectedJoyCons() => Array<JoyConDescription>
Returns an array of objects, each describing a connected Joy-Con.
The objects have the following properties:
The most important thing here is the open
method, which returns a JoyCon
instance.
JoyCon
You can obtain a JoyCon
instance by calling open
on a JoyConDescription
, as returned by listConnectedJoyCons()
.
Each JoyCon
adheres to the following interface:
JoyCon
s are also EventEmitter
s, and they emit the following events:
change
- Button state has changed. Inspect thebuttons
property on the device for more info.change:${buttonName}
- A specific button changed state. The event listener will be called with one argument, containing the new button value.down:${buttonName}
- A specific button is now being held down.up:${buttonName}
- A specific button is no longer being held down.
List of button names:
a
x
b
y
plus
home
l
r
zl
zr
dpadUp
dpadDown
dpadLeft
dpadRight
minus
screenshot
sl
sr
analogStickPress
analogStick
License
MIT