Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to raw bluetooth keyboard data

I need to listen to the raw commands/keypresses that a bluetooth keyboard sends to my device and if possible, prevent them from 'propagating' to the rest of the system.

Basically, I've written something with Node.js and coffee-script that receives keypresses from stdin and controls my Philips Hue lightbulbs. 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()

It's functionality works, but it's not very useful as it receives input from stdin, preventing it from running in the background.

What could I do to make this receive input without the window having focus?

My preference is for something in Node.js or Python to run on my Mac, but I'm willing to switch languages or run on my Raspberry Pi if need be

like image 321
Josh Hunt Avatar asked Aug 31 '14 06:08

Josh Hunt


1 Answers

keypress only listens to the standard input stream, not the keyboard itself. This input stream is handled by operating system and its hardware drivers. Usually the OS would not want applications to listen to keyboard directly and rather direct the keyboard events to the program it has on focus.

You would have to handle the device directly, otherwise OS will redirect those inputs to the other program in focus. You should try node-hid for this. It can access attached Human Interface devices like keyboard/mouse. Description says it works for USB devices, but it should work for bluetooth(HID) devices.

Secondly since you are listening to hardware, most likely you won't recieve keypress value directly, but a bunch of raw-input data/signals which need to be interpreted. You are using your keyboard as a remote control, be prepared to use it like a low-level device.

like image 168
user568109 Avatar answered Oct 03 '22 00:10

user568109