【发布时间】:2016-02-28 22:34:10
【问题描述】:
我正在尝试创建一个可以使用全局键绑定执行某些功能的 Electron node.js 应用程序。不幸的是,Electron 中的全局键绑定 API 在游戏中不起作用,因此我需要创建一个本地节点模块来监听这些低级键事件。
所以我使用 node-gyp 来编译项目,使用 Visual Studio 2015 和 nan 来提供 node 和 c++ 之间的通信。我已经设法让项目的两个方面分别工作(低级键绑定和 node.jsnan 通信),但我在组合它们时遇到了麻烦。我也承认我对 c++ 的经验很少(我没有写过一个 c++ 程序)。
#include "node_modules/nan/nan.h"
using namespace std;
using namespace Nan;
HHOOK _hook;
KBDLLHOOKSTRUCT kbdStruct;
class KeyboardEventWorker : public AsyncProgressWorker {
public:
KeyboardEventWorker(Callback *callback, Callback *progress)
: AsyncProgressWorker(callback), progress(progress) {}
~KeyboardEventWorker() {}
LRESULT CALLBACK HookCallback(int nCode,WPARAM wParam,LPARAM lParam) {
executionProgress->Send(reinterpret_cast<const char*>(nCode), sizeof(nCode));
return CallNextHookEx(_hook, nCode, wParam, lParam);
}
void Execute (const AsyncProgressWorker::ExecutionProgress& progress) {
executionProgress = &progress; //PROBLEM #1
_hook = SetWindowsHookEx(13, HookCallback, NULL, 0); //PROBLEM #2
SleepEx(INFINITE, true);
}
void HandleProgressCallback(const char *data, size_t size) {
HandleScope scope;
v8::Local<v8::Value> argv[] = {
New<v8::Integer>(*reinterpret_cast<int*>(const_cast<char*>(data)))
};
progress->Call(1, argv);
}
private:
Callback *progress;
AsyncProgressWorker::ExecutionProgress *executionProgress;
};
NAN_METHOD(DoProgress) {
Callback *progress = new Callback(info[0].As<v8::Function>());
Callback *callback = new Callback(info[1].As<v8::Function>());
AsyncQueueWorker(new KeyboardEventWorker(callback, progress));
}
NAN_MODULE_INIT(Init) {
Set(target
, New<v8::String>("init").ToLocalChecked()
, New<v8::FunctionTemplate>(DoProgress)->GetFunction());
}
NODE_MODULE(asyncprogressworker, Init)
问题 #1:为了能够将消息发送回 node.js,我需要复制 AsyncProgressWorker::ExecutionProgress 的指针并使其可用于整个类,以便当 HookCallback 触发时它可以发送消息到node.js。
编译器不喜欢这样
..\binding.cc(21): 错误 C2440: '=': 无法从 'const 转换 Nan::AsyncProgressWorker::ExecutionProgress *' 到 'Nan: :AsyncProgressWorker::ExecutionProgress *' [C:\Users\eksrow\gdrive\projects\vscode\node-native-hello-world\build\bindin g.vcxproj]。
..\binding.cc(21):注意:转换丢失限定符
格式化:
'const Nan::AsyncProgressWorker::ExecutionProgress *'
'Nan::AsyncProgressWorker::ExecutionProgress *'
我设法通过将关键字 const 添加到私有成员 *executionProgress; 来解决这个问题。但我不明白为什么会修复它,const 变量一旦设置就不应更改。为什么会这样编译?
问题2:这个很奇特:
..\binding.cc(22): 错误 C3867: 'KeyboardEventWorker::HookCallback': 非标准语法;使用 '&' 创建指向成员的指针 [C:\Users\eksrow\gdrive\projects\vscode\node-native-hello-world\build\binding.vcxproj]
我在网上查了很多例子,它们都有相同的语法:
关于那行,我看不出我的代码和他们的代码有什么区别。
如果我按照编译器的说明在该行添加一个 & 符号,则会出现完全不同的错误:
..\binding.cc(22): error C2276: '&': 对绑定成员的非法操作 函数表达式 [C:\Users\eksrow\gdrive\proj ects\vscode\node-native-hello-world\build\binding.vcxproj] ..\binding.cc(22): 错误 C2660: 'SetWindowsHookExA': 函数没有 取 3 个参数 [C:\Users\eksrow\gdrive\project s\vscode\node-native-hello-world\build\binding.vcxproj]
【问题讨论】:
标签: c++ node.js node-gyp node.js-addon node.js-nan