Listen to raw Bluetooth keyboard data

I need to listen to the raw commands / keystrokes that the Bluetooth keyboard sends to my device, and if possible, prevent them from โ€œspreadingโ€ to the rest of the system.

Basically, I wrote something with Node.js and a coffee script that receives keystrokes from stdin and controls Philips Hue bulbs. It looks something like this:

 keypress = require 'keypress' # Setup keypress events keypress process.stdin process.stdin.on 'keypress', (character, key) -> switch character when 'l' then hue.decreaseTemp() when 'r' then hue.increaseTemp() when 'u' then hue.increaseBri() when 'd' then hue.decreaseBri() when 'b' then hue.turnOff() # Exit on ctrl-c if key?.ctrl and key.name is 'c' process.stdin.pause() 

This function works, but it is not very useful, as it receives input from stdin, preventing it from running in the background.

What can I do to make this input input without the focus of the window?

My preference is for something in Node.js or Python to run on my Mac, but I'm ready to switch languages โ€‹โ€‹or run my raspberry Pi if you need to

+7
javascript bluetooth keyboard philips-hue
source share
2 answers

keypress only listens for standard input, not the keyboard itself. This input stream is processed by the operating system and its hardware drivers. Typically, the OS does not want applications to listen to the keyboard directly and rather send keyboard events to the program that it had in focus.

You will need to access the device directly, otherwise the OS will redirect these inputs to another program in focus. You should try node-hid . It can access connected Human Interface devices, such as a keyboard / mouse. The description says that it works for USB devices, but it should work on Bluetooth (HID) devices.

Secondly, since you are listening to hardware, most likely you will not get directly the keypress value, but a bunch of raw data / signals that need to be interpreted. You are using the keyboard as a remote control, be prepared to use it as a low-level device.

+4
source share

One way to solve this problem (control the hue without having to use the program in the foreground) is to separate the hue controls and keyboard shortcuts in different applications:

I would just write a command line tool, for example. with node, which takes as an argument the command to run. Then I would create OS-level keyboard shortcuts for this command.

Eg. I would configure Command-Shift- + to call huecontrol increasebri . Huecontrol is the thing and the way out.

+1
source share

All Articles