热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

听原始蓝牙键盘数据-Listentorawbluetoothkeyboarddata

Ineedtolistentotherawcommandskeypressesthatabluetoothkeyboardsendstomydeviceandif

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:

基本上,我用Node.js和coffee-script编写了一些东西,它从stdin接收按键并控制我的Philips Hue灯泡。它看起来像这样:

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.

它的功能很有用,但它不是很有用,因为它从stdin接收输入,阻止它在后台运行。

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

我的偏好是在我的Mac上运行Node.js或Python中的某些东西,但是如果需要,我愿意切换语言或在我的Raspberry Pi上运行

2 个解决方案

#1


4  

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.

keypress只监听标准输入流,而不是键盘本身。此输入流由操作系统及其硬件驱动程序处理。通常操作系统不希望应用程序直接监听键盘,而是将键盘事件指向它所关注的程序。

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.

您必须直接处理设备,否则操作系统会将这些输入重定向到焦点对准的其他程序。你应该为此尝试node-hid。它可以访问附加的人机界面设备,如键盘/鼠标。说明它适用于USB设备,但它应该适用于蓝牙(HID)设备。

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.

其次,因为您正在聆听硬件,很可能您不会直接接收按键值,而是需要解释一堆原始输入数据/信号。您正在使用键盘作为遥控器,准备像低级设备一样使用它。

#2


1  

One way to solve this problem (control the hue without needing the program to be in the foreground) would be to split the hue control and keyboard shortcuts to different apps:

解决此问题的一种方法(控制色调而不需要程序在前台)将分离色调控件和键盘快捷键到不同的应用程序:

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

我只想写一个命令行工具,例如。 with node,它将运行命令作为参数。然后我会为命令创建操作系统级别的键盘快捷键。

Eg. I would configure Command-Shift-+ to call huecontrol increasebri. Huecontrol would do is thing and exit.

例如。我会配置Command-Shift- +来调用huecontrol increasebri。 Huecontrol会做的事情是退出。


推荐阅读
author-avatar
mobiledu2502927537
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有