Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🕹 A tiny event handler for Switch controllers!

License

NotificationsYou must be signed in to change notification settings

vaneenige/unswitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm versiongzip sizelicensedependencies

Unswitch is a tiny event handler for Switch controllers on the web based on the Gamepad API. Attach callbacks to button presses (up & down) and the joystick!

Install

$ npm install --save unswitch

Use script tags or modules? Check out the version onunpkg!

Setup

Connecting a Switch controller is easy: pair it with bluetooth and you're ready to go! This library doesn't listen toconnected ordisconnected events but in some cases it might be useful. Here's how you can listen to these events:

window.addEventListener("gamepadconnected",({ gamepad})=>{});
window.addEventListener("gamepaddisconnected",({ gamepad})=>{});

Usage

// Import the libraryimportUnswitchfrom'unswitch';// Observe a controllerconstunswitch=newUnswitch({side:'L',// or Rbuttons:(button,pressed,side)=>console.log(`Catch-all - button:${button} was${pressed ?'pressed' :'released'} on the${side} side`),b:p=>{},a:p=>{},y:p=>{},x:p=>{},sl:p=>{},sr:p=>{},minus:p=>{},plus:p=>{},lstick:p=>{},rstick:p=>{},home:p=>{},screenshot:p=>{},bumper:p=>{},// L or Rtrigger:p=>{},// ZL or ZRaxes:p=>{},});functionrender(){// Call the update function manuallyunswitch.update();requestAnimationFrame(render);}render();

Please note that it's not required to pass all button-functions to Unswitch and will only be executed when you provide them.You are able to use thebuttons function to catch all button presses and implement your own logic using the provided data. Thebuttons function will always be executed when provided, even when the button is also passed as property.

It's possible to connect up to two controllers at the same time. To make this workside is to be passed with eitherL (left) orR (right) for the controllers respectively. Callingunswitch.update() will check every button for a change in state. If a callback is provided the new state is passed along. The axis works in the same way, but instead of aboolean it will return anumber from 0 to 8. Number 0 to 7 are for the joystick positions going clockwise, number 8 is used as default (center).

Have a look at the demo onCodepen!

Contribute

Are you excited about this library and have interesting ideas on how to improve it? Please tell me or contribute directly! 🙌

npm install > npm run demo > http://localhost:8080

License

MIT ©Colin van Eenige

About

🕹 A tiny event handler for Switch controllers!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp