Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

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

Library to talk to the 3dConnexion space mice in NodeJS and browsers

License

NotificationsYou must be signed in to change notification settings

nytamin/spacemouse

Repository files navigation

Node CInpmnpm

A Node.js module to interact with the3Dconnexion devices, such as the SpaceMouse and SpaceNavigator.

This repository is not affiliated with 3Dconnexion in any way.

License: MIT

Demo

If you are using abrowser that supports WebHID, you can try out the library right away, in the browser:Demo.

Supported devices

Some of the devices supported by this library are:

  • SpaceNavigator
  • SpaceMouse Wireless
  • SpaceMouse Compact
  • SpaceMouse Module
  • SpaceMouse Pro
  • SpaceMouse Pro Wireless
  • SpacePilot PRO
  • SpaceMouse Enterprise
  • CadMouse
  • CadMouse Wireless
  • CadMouse Pro Wireless
  • CadMouse Compact
  • CadMouse Pro
  • CadMouse Compact Wireless

See the full list inproducts.ts.

Installation

To use in Node.js

$ npm install --save spacemouse-nodeor$ yarn add spacemouse-node

To use in browser

$ npm install --save spacemouse-webhidor$ yarn add spacemouse-webhid

Linux

On linux, theudev subsystem blocks access for non-root users to the SpaceMouse without some special configuration. Save the following to/etc/udev/rules.d/50-spacemouse.rules and reload the rules withsudo udevadm control --reload-rules

SUBSYSTEM=="input", GROUP="input", MODE="0666"SUBSYSTEM=="usb", ATTRS{idVendor}=="0x46d", MODE:="666", GROUP="plugdev"KERNEL=="hidraw*", ATTRS{idVendor}=="0x46d", MODE="0666", GROUP="plugdev"SUBSYSTEM=="usb", ATTRS{idVendor}=="0x256f", MODE:="666", GROUP="plugdev"KERNEL=="hidraw*", ATTRS{idVendor}=="0x256f", MODE="0666", GROUP="plugdev"

Note: If you need more than 4 devices simultaneously, you might also have to set your env-varUV_THREADPOOL_SIZE:

var{ env}=require('process')env.UV_THREADPOOL_SIZE=8// Allow up to 8 devices

Getting started - Node.js

Watch for connected SpaceMouse (recommended)

This is the recommended way to use this library, to automatically be connected or reconnected to the device.

Note: The watcher uses thenode-usb library, which might be unsupported on some platforms. If it is not supported, it can use polling as fallback.

const{ SpaceMouseWatcher}=require('spacemouse-node')// or spacemouse-webhid in browser/*This example connects to any connected SpaceMouse devices and logswhenever the mouse is moved or a button is pressed.*/// Set up the watcher for SpaceMouse:constwatcher=newSpaceMouseWatcher({// usePolling: false // To be used if node-usb is not supported// pollingInterval= 1000})watcher.on('error',(e)=>{console.log('Error in SpaceMouseWatcher',e)})watcher.on('connected',(spaceMouse)=>{console.log(`SpaceMouse device of type${spaceMouse.info.name} connected`)spaceMouse.on('disconnected',()=>{console.log(`SpaceMouse device of type${spaceMouse.info.name} was disconnected`)// Clean up stuffspaceMouse.removeAllListeners()})spaceMouse.on('error',(...errs)=>{console.log('SpaceMouse error:', ...errs)})// Listen to Rotation changes:spaceMouse.on('rotate',(rotate)=>{console.log(`Rotate${JSON.stringify(rotate)}`)})// Listen to Translation changes:spaceMouse.on('translate',(translate)=>{console.log(`Translate${JSON.stringify(translate)}`)})// Listen to pressed buttons:spaceMouse.on('down',(keyIndex)=>{console.log('Button pressed ',keyIndex)})// Listen to released buttons:spaceMouse.on('up',(keyIndex)=>{console.log('Button released',keyIndex)})})// To stop watching, call// watcher.stop().catch(console.error)

Connect to a devices manually

const{ setupSpaceMouse}=require('spacemouse-node')// or spacemouse-webhid in browser/*This example shows how to use setupSpaceMouse()directly, instead of going via SpaceMouseWatcher()*/// Connect to an SpaceMouse-device:setupSpaceMouse().then((spaceMouse)=>{console.log(`Connected to${spaceMouse.info.name}`)spaceMouse.on('disconnected',()=>{console.log(`Disconnected!`)spaceMouse.removeAllListeners()})spaceMouse.on('error',(...args)=>{console.log('SpaceMouse error:', ...args)})// Listen to Rotation changes:spaceMouse.on('rotate',(rotate)=>{console.log(`Rotate${JSON.stringify(rotate)}`)})// ...}).catch(console.log)// Handle error

or

const{ listAllConnectedDevices, setupSpaceMouse}=require('spacemouse-node')// List and connect to all SpaceMouse-devices:listAllConnectedDevices().forEach((connectedDevice)=>{setupSpaceMouse(connectedDevice).then((spaceMouse)=>{console.log(`Connected to${spaceMouse.info.name}`)spaceMouse.on('disconnected',()=>{console.log(`Disconnected!`)spaceMouse.removeAllListeners()})spaceMouse.on('error',(...args)=>{console.log('SpaceMouse error:', ...args)})// Listen to Rotation changes:spaceMouse.on('rotate',(rotate)=>{console.log(`Rotate${JSON.stringify(rotate)}`)})// ...}).catch(console.log)// Handle error

Getting started - Browser (WebHID)

See the example implementation atpackages/webhid-demo.

Demo

If you are using a Chromium v89+ based browser, you can try out thewebhid demo.

API documentation

SpaceMouseWatcher (Node.js only)

The SpaceMouseWatcher has a few different options that can be set upon initialization:

const{ SpaceMouseWatcher}=require('spacemouse-node')constwatcher=newSpaceMouseWatcher({// usePolling: false// pollingInterval: 1000})watcher.on('error',(e)=>{console.log('Error in SpaceMouseWatcher',e)})watcher.on('connected',(spaceMouse)=>{// SpaceMouse connected...})

usePolling

When this is set, the SpaceMouseWatcher will not use thenode-usb library for detecting connected devices,but instead resort to polling at an interval (pollingInterval).This is compatible with more systems and OS:es, but might result in higher system usage, slower detection of new devices.

SpaceMouse Events

// Example:spaceMouse.on('rotate',(rotation)=>{console.log(rotation)})
EventDescription
"error"Triggered on error.
Emitted with(error).
"disconnected"Triggered when device is disconnected.
"rotate"Triggered when the mouse is rotated.
Emitted with(rotation: {pitch: number, roll: number, yaw: number})
"translate"Triggered when the mouse is moved.
Emitted with(translation: {x: number, y: number, z: number})
"down","up"Triggered when a button is pressed / released.
Emitted with(buttonIndex: number)

Other functionality

Seethe SpaceMouse-class for more functionality.

For developers

This is a mono-repo, usingLerna andYarn.

Setting up your local environment

This repo is usingYarn. If you don't want to use it, replaceyarn xyz withnpm run xyz below.

To install Yarn, just runnpm install -g yarn.

Setting up the repo

  • Clone the repo andcd into it.
  • Install all dependencies:yarn.
  • Do an initial build:yarn build

Running and testing local changes

If you'd like to run and test your local changes,yarn link is a useful tool to symlink your localspaceMouse dependency into your test repo.

# To set up the SpaceMouse-repo for linking:cd your/spaceMouse/repoyarn lernaexec yarn link# This runs "yarn link" in all of the mono-repo packagesyarn build# Every time after you've made any changes to the SpaceMouse-repo you need to rebuildcd your/spaceMouse/repoyarn build# Set up your local test repo to used the linked SpaceMouse libraries:cd your/test/repoyarn add spacemouse-nodeyarn link spacemouse-nodeyarn link @spacemouse-lib/core# To unlink the spacemouse-lib from your local test repo:cd your/test/repoyarn unlink spacemouse-nodeyarn unlink @spacemouse-lib/coreyarn --force# So that it reinstalls the ordinary dependencies

Contribution guidelines

If you have any questions or want to report a bug,please open an issue at Github.

If you want to contribute a bug fix or improvement, we'd happily accept Pull Requests.(If you're planning something big,please open an issue to announce it first, and spark discussions.

Coding style and tests

Please follow the same coding style as the rest of the repository as you type. :)

Before committing your code to git, be sure to run these commands:

yarn# To ensure the right dependencies are installed and yarn.lock is updatedyarn build# To ensure that there are no syntax or build errorsyarn lint# To ensure that the formatting follows the right rulesyarntest# To ensure that your code passes the unit tests.

If you're adding a new functionality, adding unit tests for it is much appreciated.

Notes to maintainers

Making a nightly build

Making a Pre-release

  • Update the branch (preferably the master branch)
  • yarn release:bump-prerelease and push the changes (including the tag)
  • Trigger a run ofCI: publish-prerelease

Making a Release

  • Update the the master branch
  • yarn release:bump-release and push the changes (including the tag)
  • Trigger a run ofCI: publish-release to publish to NPM.
  • Trigger a run ofCI: publish-demo to update the docs.

License

By contributing, you agree that your contributions will be licensed under theMIT License.


[8]ページ先頭

©2009-2025 Movatter.jp