Git Product home page Git Product logo

node-gamepad's Introduction

node-gamepad

Build Status Build status

Bindings for Alex Diener's cross-platform gamepad code

The library is tiny and included inside this addon so you don't need any special libraries installed on your system like SDL.

To install as a dependency simple type the following in your project's folder:

npm install gamepad

Examples

var gamepad = require("gamepad");

// Initialize the library
gamepad.init()

// List the state of all currently attached devices
for (var i = 0, l = gamepad.numDevices(); i < l; i++) {
  console.log(i, gamepad.deviceAtIndex());
}

// Create a game loop and poll for events
setInterval(gamepad.processEvents, 16);
// Scan for new gamepads as a slower rate
setInterval(gamepad.detectDevices, 500);

// Listen for move events on all gamepads
gamepad.on("move", function (id, axis, value) {
  console.log("move", {
    id: id,
    axis: axis,
    value: value,
  });
});

// Listen for button up events on all gamepads
gamepad.on("up", function (id, num) {
  console.log("up", {
    id: id,
    num: num,
  });
});

// Listen for button down events on all gamepads
gamepad.on("down", function (id, num) {
  console.log("down", {
    id: id,
    num: num,
  });
});

Events

There are 5 event types attach, remove, down, up, and move. All pass the session id of the gamepad as the first argument.

The attach event also passes current state as the second arg to attach.

The down and up events also pass in the button id and timestamp after device session id.

The move event passes axis, value, old value and timestamp after session id.

Functions

gamepad.init()

Initializes gamepad library and detects initial devices. Call this before any other library function. If you want to receive attach events callbacks from devices detected in gamepad.init(), you must listen for attach before calling gamepad.init().

gamepad.shutdown()

Tears down all data structures created by the gamepad library and releases any memory that was allocated. It is not necessary to call this function at application termination, but it's provided in case you want to free memory associated with gamepads at some earlier time.

numDevices()

Returns the number of currently attached gamepad devices.

deviceAtIndex(deviceIndex)

Returns the specified gamepad's current state, or undefined if deviceIndex is out of bounds.

detectDevices()

Polls for any devices that have been attached since the last call to gamepad.detectDevices() or gamepad.init(). If any new devices are found, the attach event will be emitted once per newly detected device.

Note that depending on implementation, you may receive button and axis event callbacks for devices that have not yet been detected with gamepad.detectDevices(). You can safely ignore these events, but be aware that your callbacks might receive a device ID that hasn't been emitted as an attach event yet.

processEvents()

Reads pending input from all attached devices and emits the appropriate events, if any have been registered.

Developing

Publishing a release

This repository uses prebuild to create pre-built binaries (hosted on GitHub) for Windows, macOS, and Linux.

To trigger a new release, simply push a new tag. The TravisCI and Appveyor build scripts will see that tag, and publish binaries for it.

Once the builds have completed, the release can be published to npm via the normal command: npm publish.

node-gamepad's People

Contributors

creationix avatar cronopio avatar deadprogram avatar edgarsilva avatar from-the-river-to-the-sea avatar hgcummings avatar stewart avatar warp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-gamepad's Issues

any way to delay the button pushed and increase when held?

trying to make a navigational UI and i am struggling to figure out how to make it so that it navigates like the keyboard does where pressing once doesn't move up or down so much but when held down it does.

any hints on how i can go about this?

Does not compile on node 6.9

I'm getting

/~/gamepad/~/node-pre-gyp/lib/info.js
Module not found: Can't resolve 'aws-sdk' in '/Users/xxxx/project/node_modules/gamepad/node_modules/node-pre-gyp/lib'

No "attach" events are emitted

Hi there!

I've just discovered this library, and it's fantastic! However, it seems that I'm not receiving any attach events. Here is my code:

const gamepad = require('gamepad');

gamepad.on('attach', (id, state) => {
	console.log('attach', {id, state});
});

gamepad.init();

// Create a game loop and poll for events
setInterval(gamepad.processEvents, 16);
// Scan for new gamepads as a slower rate
setInterval(gamepad.detectDevices, 500);

// Listen for button 0 down event from our target gamepad.
gamepad.on('down', (id, num) => {
	if (num !== 0) {
		return;
	}

	const device = gamepad.deviceAtIndex(id);
	if (device.vendorID === 9025 && device.productID === 32845) {
		console.log('down', {
			id,
			num
		});
	}
});

I have yet to see a log message from the attach event, even though I can receive gamepad down events just fine.

In addition, this code throws an uncaught exception if I unplug and re-plug my controller and press button 0 again, because the new device doesn't seem to be returned when calling gamepad.deviceAtIndex(id).

Thanks for your time and this lovely library!
Alex

EDIT: This is on Windows 10, Node v6.9.1

will not compile on node 4

fails on node-pre-gyp install --fallback-to-build
trying to install on a raspberry pi running raspian.
looked through the npm install log but did not see any obvious reason for the error. nothing obvious.

it installs correctly under 0.12.7 of node.... so may be a 4.0.0 .... mapbox/node-pre-gyp#173

How to rebuild on electron?

System:windows (x64)
node version:v10.16.3 x86
electron version:v6.0.0
python version:v2.7
vs tool:vs2017
I use the electron-rebuild to rebuild the gamepad,but it tell me rebuild failed.
then,I use the "node-gyp rebuild --target=6.0.0 --arch=x86 --dist-url=https://electronjs.org/headers" to manually compile the gamepad,it still failed.
can I get some help?

Does not compile on Node v8.12.0, Windows 10 1803

Tried installing it, had to install windows-build-tools, and now the problem I get is:

gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at emitTwo (events.js:126:13)
gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\skybl\Documents\#Programming\XBXctrl\node_modules\gamepad
gyp ERR! node -v v8.12.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `prebuild-install || node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\skybl\AppData\Roaming\npm-cache\_logs\2018-09-16T17_26_43_340Z-debug.log

Why is it looking for its files on the root of my disk? Is there any way to fix it and make it compile? Thanks

Error: [email protected] install: `prebuild-install || node-gyp rebuild`

System: Linux Mint (x86), Tried also in Ubuntu 19.04 and got same error
Node version: using node v12.15.0 (npm v6.13.6)

Error Log from npm:

gyp ERR! build error

gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:223:5)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
gyp ERR! System Linux 5.0.0-38-generic
gyp ERR! command "/usr/bin/node" "/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/neo/Documentos/Workspace/RetroAct/node_modules/gamepad
gyp ERR! node -v v12.15.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux",
"arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `prebuild-install || node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/neo/.npm/_logs/2020-02-12T19_11_53_462Z-debug.log

Linux: Cannot find module (gamepad.node Binary)

I am working on a Control System for Robotics using Node-Webkit (inheriting the project is a more accurate description). I honestly barely know how node (or nw) work and am basically stumbling through this learning as I go.

ran
npm install gamepad

placed
var gamepad = require("gamepad");
in one of the js files, there all the other dependencies are.
I started up nw, and got a nice little error:

Uncaught node.js Error 

Error: Cannot find module '/home/gixxy/Desktop/DriverStation.js/node_modules/gamepad/build/gamepad/v1.0.2/Release/node-webkit-v0.10.5-linux-x64/gamepad.node'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:273:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/home/gixxy/Desktop/DriverStation.js/node_modules/gamepad/gamepad.js:4:15)
    at Module._compile (module.js:454:26)
    at Object.Module._extensions..js (module.js:472:10)
    at Module.load (module.js:354:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:362:17)

Basically the last part of the path is wrong, on my system it is at:
node-v11-linux-x64
not
node-webkit-v0.10.5-linux-x64

I am using Arch Linux, here is my uname -a:
Linux ASUS14-ARCH 3.17.3-1-ARCH #1 SMP PREEMPT Fri Nov 14 23:13:48 CET 2014 x86_64 GNU/Linux

Any and all help is appreciated. If you want to know what kind of robots this is going to be used for: YouTube

Cannot compile in Windows

Hi @creationix,

I'm trying to compile in all platforms so we can add pre-compiled binaries, using node-pre-gyp, for all major platforms, but I keep getting this error:

C:\projects\cylon-joystick\node_modules\gamepad\gamepad\source\gamepad/Gamepad.h(29): fatal error C1083: Cannot open include file: 'stdbool.h': No such file or directory [C:\projects\cylon-joystick\node_modules\gamepad\build\gamepad.vcxproj]

I'm currently using appveyor and a windows machine, I only see this problem in windows platforms, works well in linux and OSX as far as I can tell. Wondering if you know what the issue might be.

Missing build artifacts for 1.6.0

It looks like the latest npm release is 1.6.0, but the latest tag is 1.5.2. As a result, the build process has not published pre-built binaries for 1.6.0.

Additionally, from looking at the commit history, it is unclear if 1.6.0 was meant to be published at all. I see no explicit release commit for it.

Build fails on linux

../binding.cc: At global scope:
../binding.cc:115:6: error: variable or field ‘init’ declared void
  115 | void init(Handle<Object> target) {
      |      ^~~~

gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
gyp ERR! System Linux 5.7.6-arch1-1
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! node -v v14.5.0
gyp ERR! node-gyp -v v7.0.0
gyp ERR! not ok 

building on archlinux via yarn add. someone knows what's up?

Segmentation fault: 11

I thought I'd bring this bug to your attention. Thank you!

Hardware Details

Sony Dualshock 3
Raspberry pi3 - Raspbian Jessie with Pixel

Bug Description

If two buttons are pressed right after each other in the right timing, the script will seg fault and terminate. It occurs in wireless and wired configurations.

which version of python does this require to run ?

I'm wondering what version of python is required for the install to work? I've installed the latest with binaries and path, however I get this error

prebuild-install WARN install No prebuilt binaries found (target=9.11.1 runtime=node arch=x64 platform=win32)
gyp ERR! configure error
gyp ERR! stack Error: Command failed: XXXXX\AppData\Local\Programs\Python\Python37-32\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack File "", line 1
gyp ERR! stack import sys; print "%s.%s.%s" % sys.version_info[:3];

Electron

This doesn't appear to install, or work with electron. It works in other projects on the same machine..
Here is a log. Help would be appreciated
http://pastebin.com/9KKWUKCD

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.