主题说明了一切:
JavaScript 应用程序如何检测 Leap Motion 设备是否已连接。
将 Leap Motion 设备从一台计算机移动到另一台计算机非常容易,JavaScript 应用程序如何检测当前是否有设备连接到计算机?
2013-08-08 更新
我已将此问题标记为已回答,因为正如 Dmitry 的工作所示,在撰写本文时,JavaScript 应用程序无法知道在应用程序加载时是否连接了 Leap Motion 设备。
主题说明了一切:
JavaScript 应用程序如何检测 Leap Motion 设备是否已连接。
将 Leap Motion 设备从一台计算机移动到另一台计算机非常容易,JavaScript 应用程序如何检测当前是否有设备连接到计算机?
2013-08-08 更新
我已将此问题标记为已回答,因为正如 Dmitry 的工作所示,在撰写本文时,JavaScript 应用程序无法知道在应用程序加载时是否连接了 Leap Motion 设备。
这取决于您可用的操作系统 API 和驱动程序。当设备连接到计算机时,操作系统可以检测到连接到其套接字之一的设备(使用 IRQ、轮询等)。然后,您可以使用驱动程序或 OS API(如果它本身支持此类设备)来检查设备的状态。由于这通常是使用更底层的编程语言完成的,如 C++、C 甚至汇编语言(javascript 不适合有几个原因),您应该检查您的 javascript API 参考(不确定您是否使用浏览器 API、Win8 API 或别的东西),看看是否有任何相关的功能。
更新:您发送的 API 链接对此似乎很模糊。但是我发现它在后台建立了到本地主机的 WebSocket 连接。该Controller.connect()
函数实际上是一个过程(不返回任何内容)。但我发现了一个更有用的链接(入门:http: //js.leapmotion.com/start)有他们提供的不同事件的描述,包括以下内容:
在这种情况下,您可以使用回调而不是谓词:
function doMyOwnStuff()
{
console.log( "O_o" );
}
var controller = new Leap.Controller();
controller.on('deviceConnected', function() {
console.log("A Leap device has been connected.");
doMyOwnStuff();
});
controller.on('deviceDisconnected', function() {
console.log("A Leap device has been disconnected.");
});
//should probably fire a 'deviceConnected'
controller.connect();
我希望它会有所帮助,因为我没有要测试的硬件。
为了了解 Leap 与 Web App 的集成,我为 Leap Motion 反编译了 New York Times Reader。
关于其实现(built.js),以下代码可能会有所帮助。(基于 Backbone.js)
var LeapController = new Leap.Controller({enableGestures: true});
window.L = LeapController;
LeapController.on('deviceConnected', function () {
console.log('deviceConnected', arguments);
// in the example code, this fires when the app starts.
// in our app, it only fires when the device is reconnected after having been connected when the app was started.
dispatch.trigger('LeapControl:reconnect');
});
LeapController.on('ready', function () {
// this fires when the app is ready.
dispatch.trigger('LeapControl:reconnect');
});
LeapController.on('connect', function () {
console.log('device is connected');
// this fires when no device is plugged in. wtf.
});
LeapController.connection.on('deviceConnect', function () {
console.log('deviceConnect');
// this fires when the device is changes state from plugged in to ungplugged and vice versa.
});
LeapController.on('deviceDisconnected', function () {
console.log('deviceDisconnected', arguments);
dispatch.trigger('LeapControl:disconnect');
});
显然,NYTimes Reader 的开发人员也已经发现,在应用程序加载之前检测 Leap Controller 是否已经连接并不容易。(“wtf”,哈哈....)
以及定义 LeapControl:disconnect/reconnect 事件行为的部分代码,可以理解:
newNews.views.Disconnected = Backbone.View.extend({
el: $('#disconnection-box'),
initialize: function () {
_.bindAll(this);
},
open: function () {
this.listenTo(dispatch, 'LeapControl:disconnect', this.show);
this.listenTo(dispatch, 'LeapControl:reconnect', this.hide);
return this;
}, ........
因此,当触发 LeapControl:reconnect 时,会隐藏一个“未检测到跳跃运动控制器”的弹出窗口。
在调试时,如果在启动应用程序之前已经插入了跳跃动作,事件将按以下顺序触发,从而确保正确检测:
同时,如果没有预先插入,这个并且只有这个会被触发:
作为结论,我们可以使用“就绪”事件来处理这种情况。高温高压