Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Xkeys.filterDevice() static method #94

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Conversation

nytamin
Copy link
Member

@nytamin nytamin commented Dec 6, 2023

This PR adds a static method to the Xkeys class : Xkeys.filterDevice()

This is used to determine whether a HID.device is supported by this library or not. Which is useful when implementing XKeys together with other libraries that handle USB devices.

Example usage:

const HID = require('node-hid')
const { setupXkeysPanel, XKeys } = require('xkeys')

/*
	This example shows how to use node-hid to list all connected usb devices, then
	connecting to any supported X-keys panels.
*/

Promise.resolve().then(async () => {

	// List all connected usb devices:
	const devices = await HID.devicesAsync()

	for (const device of devices) {

		// Filter for supported X-keys devices:
		if (XKeys.filterDevice(device)) {

			console.log('Connecting to X-keys device:', device.product)

			setupXkeysPanel(device)
				.then((xkeysPanel) => {
					xkeysPanel.on('disconnected', () => {
						console.log(`X-keys panel of type ${xkeysPanel.info.name} was disconnected`)
						// Clean up stuff
						xkeysPanel.removeAllListeners()
					})
					xkeysPanel.on('error', (...errs) => console.log('X-keys error:', ...errs))

					xkeysPanel.on('down', (keyIndex) => console.log('Button pressed', keyIndex))
					// ...
				})
				.catch(console.log) // Handle error

		} else {
			// is not an X-keys device
			console.log('Not a supported X-keys device:', device.product || device.productId)
		}

	}

}).catch(console.log)

…mpatible X-keys devices when manually handling HID devices
@nytamin nytamin merged commit b00d271 into master Jan 11, 2024
12 checks passed
@nytamin nytamin deleted the fix/device-filtering branch January 11, 2024 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant