{"id":13602595,"url":"https://github.com/vaneenige/unswitch","last_synced_at":"2025-04-05T00:09:04.288Z","repository":{"id":57386911,"uuid":"141927393","full_name":"vaneenige/unswitch","owner":"vaneenige","description":"🕹 A tiny event handler for Switch controllers!","archived":false,"fork":false,"pushed_at":"2018-08-09T18:56:15.000Z","size":29,"stargazers_count":572,"open_issues_count":0,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-28T23:07:56.365Z","etag":null,"topics":["controller","gamepad","input","joystick","switch"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vaneenige.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-22T20:12:08.000Z","updated_at":"2025-03-13T03:07:25.000Z","dependencies_parsed_at":"2022-09-26T16:50:46.036Z","dependency_job_id":null,"html_url":"https://github.com/vaneenige/unswitch","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaneenige%2Funswitch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaneenige%2Funswitch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaneenige%2Funswitch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vaneenige%2Funswitch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vaneenige","download_url":"https://codeload.github.com/vaneenige/unswitch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266564,"owners_count":20910836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["controller","gamepad","input","joystick","switch"],"created_at":"2024-08-01T18:01:31.260Z","updated_at":"2025-04-05T00:09:04.269Z","avatar_url":"https://github.com/vaneenige.png","language":"JavaScript","readme":"# Unswitch\n\n[![npm version](https://img.shields.io/npm/v/unswitch.svg)](https://www.npmjs.com/package/unswitch)\n[![gzip size](http://img.badgesize.io/https://unpkg.com/unswitch/dist/unswitch.es.js?compression=gzip)](https://unpkg.com/unswitch)\n[![license](https://img.shields.io/npm/l/unswitch.svg)](https://github.com/vaneenige/unswitch/blob/master/LICENSE)\n[![dependencies](https://img.shields.io/badge/dependencies-none-ff69b4.svg)](https://github.com/vaneenige/unswitch/blob/master/package.json)\n\nUnswitch is a tiny event handler for Switch controllers on the web based on the Gamepad API. Attach callbacks to button presses (up \u0026 down) and the joystick!\n\n## Install\n\n```\n$ npm install --save unswitch\n```\n\n\u003e Use script tags or modules? Check out the version on [unpkg](https://unpkg.com/unswitch)!\n\n## Setup\nConnecting a Switch controller is easy: pair it with bluetooth and you're ready to go! This library doesn't listen to `connected` or `disconnected` events but in some cases it might be useful. Here's how you can listen to these events:\n\n```js\nwindow.addEventListener(\"gamepadconnected\", ({ gamepad }) =\u003e {});\n```\n\n```js\nwindow.addEventListener(\"gamepaddisconnected\", ({ gamepad }) =\u003e {});\n```\n\n## Usage\n\n```js\n// Import the library\nimport Unswitch from 'unswitch';\n\n// Observe a controller\nconst unswitch = new Unswitch({\n  side: 'L', // or R\n  buttons: (button, pressed, side) =\u003e console.log(`Catch-all - button: ${button} was ${pressed ? 'pressed' : 'released'} on the ${side} side`),\n  b: p =\u003e {},\n  a: p =\u003e {},\n  y: p =\u003e {},\n  x: p =\u003e {},\n  sl: p =\u003e {},\n  sr: p =\u003e {},\n  minus: p =\u003e {},\n  plus: p =\u003e {},\n  lstick: p =\u003e {},\n  rstick: p =\u003e {},\n  home: p =\u003e {},\n  screenshot: p =\u003e {},\n  bumper: p =\u003e {}, // L or R\n  trigger: p =\u003e {}, // ZL or ZR\n  axes: p =\u003e {},\n});\n\nfunction render() {\n  // Call the update function manually\n  unswitch.update();\n  requestAnimationFrame(render);\n}\n\nrender();\n```\n\nPlease note that it's not required to pass all button-functions to Unswitch and will only be executed when you provide them.\nYou are able to use the `buttons` function to catch all button presses and implement your own logic using the provided data. The `buttons` function will always be executed when provided, even when the button is also passed as property.\n\nIt's possible to connect up to two controllers at the same time. To make this work `side` is to be passed with either `L` (left) or `R` (right) for the controllers respectively. Calling `unswitch.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 a `boolean` it will return a `number` from 0 to 8. Number 0 to 7 are for the joystick positions going clockwise, number 8 is used as default (center).\n\n\u003e Have a look at the demo on [Codepen](https://codepen.io/cvaneenige/pen/gjdJrP)!\n\n## Contribute\n\nAre you excited about this library and have interesting ideas on how to improve it? Please tell me or contribute directly! 🙌\n\n```\nnpm install \u003e npm run demo \u003e http://localhost:8080\n```\n\n## License\n\nMIT © \u003ca href=\"https://use-the-platform.com\"\u003eColin van Eenige\u003c/a\u003e\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvaneenige%2Funswitch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvaneenige%2Funswitch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvaneenige%2Funswitch/lists"}