Giter Club home page Giter Club logo

snes-gamepad's Introduction

SNES Gamepad

Version: 1.1.0

A TypeScript library providing a simple interface for using USB SNES gamepads in HTML5 game projects.

For more on the ๐ŸŽฎ Gamepad API, see https://w3c.github.io/gamepad/ and https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API.

Quick Start

The SNES Gamepad library allows you to bind callback functions to SNES gamepad events, such as onConnected or onStartButtonPressed.

import snesGamepad = VictorySoftworks.IO.Gamepad.SnesGamepad;

let mapping = new snesGamepad.KiwitataSnesGamepadMapping();
let gamepad = new snesGamepad.SnesGamepad(navigator, window, mapping);

gamepad.onConnected(() => {
  console.log('SNES gamepad connected!');
});

gamepad.onDisconnected(() => {
  console.log('SNES gamepad disconnected.');
});

gamepad.onBButtonPressed(() => {
  console.log('Samus jumps into the air!');
});

gamepad.enable();

Manually Reading Pressed Buttons

If you need to read the list of pressed gamepad buttons manually, you can do so with the getPressedButtons method.

let pressedButtons = gamepad.getPressedButtons();

The getPressedButtons method returns an Array of SnesGamepadButton enumerated values representing each button that is currently pressed:

  • SnesGamepadButton.Up
  • SnesGamepadButton.Down
  • SnesGamepadButton.Left
  • SnesGamepadButton.Right
  • SnesGamepadButton.A
  • SnesGamepadButton.B
  • SnesGamepadButton.X
  • SnesGamepadButton.Y
  • SnesGamepadButton.LeftBumper
  • SnesGamepadButton.RightBumper
  • SnesGamepadButton.Select
  • SnesGamepadButton.Start

If no buttons are being pressed on the gamepad, or if there is no gamepad connected, this method returns an empty Array.

You are encouraged to place your manual checking for button presses inside an onConnected callback, preferably within a setInterval or requestAnimationFrame function that can be cancelled if the onDisconnected callback fires.

Vanilla JavaScript

If you are using the compiled SNES Gamepad library in a vanilla JavaScript project, the enumerated values are cast to plain strings, such as "Up", "A", or "Start".

Method Reference

Method Parameters Description
enable None Enables a connected SNES gamepad. If no gamepad is connected, the browser will wait for one to be connected before firing an onConnected event.
disable None Disables an SNES gamepad. The browser will stop listening for gamepad connections and button press events until enable is called.
reset None Disables an SNES gamepad and clears all registered callbacks.
onConnected callback: () => void Binds a callback function to execute when an SNES gamepad is connected.
onDisconnected callback: () => void Binds a callback function to execute when an SNES gamepad is disconnected.
onUpButtonPressed callback: () => void Binds a callback function to execute when the up button is pressed on the directional pad.
onDownButtonPressed callback: () => void Binds a callback function to execute when the down button is pressed on the directional pad.
onLeftButtonPressed callback: () => void Binds a callback function to execute when the left button is pressed on the directional pad.
onRightButtonPressed callback: () => void Binds a callback function to execute when the right button is pressed on the directional pad.
onAButtonPressed callback: () => void Binds a callback function to execute when the A button is pressed.
onBButtonPressed callback: () => void Binds a callback function to execute when the B button is pressed.
onXButtonPressed callback: () => void Binds a callback function to execute when the X button is pressed.
onYButtonPressed callback: () => void Binds a callback function to execute when the Y button is pressed.
onLeftBumperPressed callback: () => void Binds a callback function to execute when the left bumper is pressed.
onRightBumperPressed callback: () => void Binds a callback function to execute when the right bumper is pressed.
onSelectButtonPressed callback: () => void Binds a callback function to execute when the select button is pressed.
onStartButtonPressed callback: () => void Binds a callback function to execute when the start button is pressed.
getPressedButtons None Returns an Array of SnesGamepadButton enumerated values representing the gamepad buttons currently being pressed.

Available Mappings

The SNES Gamepad library comes with two button mappings:

Pull requests for additional mappings are welcome!

Single-Player Only

The SNES Gamepad library does not currently support multiple gamepads connected to the same computer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.