Add onDidChangeKeyboardLayout on Mac
This commit is contained in:
Родитель
5293e947a1
Коммит
0e4ab55a50
|
@ -1,2 +1,3 @@
|
|||
/build/
|
||||
/node_modules/
|
||||
npm-debug.log
|
||||
|
|
11
index.js
11
index.js
|
@ -32,6 +32,14 @@ NativeBinding.prototype.getCurrentKeyboardLayout = function() {
|
|||
return null;
|
||||
}
|
||||
};
|
||||
NativeBinding.prototype.onDidChangeKeyboardLayout = function(callback) {
|
||||
try {
|
||||
this._init();
|
||||
this._keymapping.onDidChangeKeyboardLayout(callback);
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
var binding = new NativeBinding();
|
||||
|
||||
|
@ -41,3 +49,6 @@ exports.getCurrentKeyboardLayout = function() {
|
|||
exports.getKeyMap = function() {
|
||||
return binding.getKeyMap();
|
||||
};
|
||||
exports.onDidChangeKeyboardLayout = function(callback) {
|
||||
return binding.onDidChangeKeyboardLayout(callback);
|
||||
}
|
||||
|
|
|
@ -167,4 +167,44 @@ void _GetCurrentKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args)
|
|||
args.GetReturnValue().Set(result);
|
||||
}
|
||||
|
||||
static v8::Persistent<v8::Function, v8::CopyablePersistentTraits<v8::Function>> _cb;
|
||||
|
||||
uv_loop_t *loop = uv_default_loop();
|
||||
uv_async_t async;
|
||||
|
||||
void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
|
||||
uv_async_send(&async);
|
||||
}
|
||||
|
||||
static void asyncSendHandler(uv_async_t *handle) {
|
||||
auto isolate = Isolate::GetCurrent();
|
||||
v8::HandleScope scope(isolate);
|
||||
auto context = isolate->GetCurrentContext();
|
||||
auto global = context->Global();
|
||||
|
||||
const int argc = 0;
|
||||
v8::Handle<v8::Value> argv[argc];
|
||||
|
||||
auto fn = Local<v8::Function>::New(isolate, _cb);
|
||||
fn->Call(global, argc, argv);
|
||||
}
|
||||
|
||||
void _OnDidChangeKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
|
||||
uv_async_init(loop, &async, (uv_async_cb)asyncSendHandler);
|
||||
|
||||
auto isolate = Isolate::GetCurrent();
|
||||
v8::Handle<v8::Function> arg0 = v8::Handle<v8::Function>::Cast(args[0]);
|
||||
v8::Persistent<v8::Function> cb(isolate, arg0);
|
||||
_cb = cb;
|
||||
|
||||
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
|
||||
|
||||
// add an observer
|
||||
CFNotificationCenterAddObserver(center, NULL, notificationCallback,
|
||||
kTISNotifySelectedKeyboardInputSourceChanged, NULL,
|
||||
CFNotificationSuspensionBehaviorDeliverImmediately
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace vscode_keyboard
|
||||
|
|
|
@ -351,4 +351,8 @@ void _GetCurrentKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args)
|
|||
args.GetReturnValue().Set(result);
|
||||
}
|
||||
|
||||
void _OnDidChangeKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
|
||||
}
|
||||
|
||||
} // namespace vscode_keyboard
|
||||
|
|
|
@ -232,4 +232,8 @@ void _GetCurrentKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args)
|
|||
XCloseDisplay(display);
|
||||
}
|
||||
|
||||
void _OnDidChangeKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
|
||||
}
|
||||
|
||||
} // namespace vscode_keyboard
|
||||
|
|
|
@ -21,6 +21,7 @@ using v8::Value;
|
|||
void init(Local<Object> exports) {
|
||||
NODE_SET_METHOD(exports, "getKeyMap", _GetKeyMap);
|
||||
NODE_SET_METHOD(exports, "getCurrentKeyboardLayout", _GetCurrentKeyboardLayout);
|
||||
NODE_SET_METHOD(exports, "onDidChangeKeyboardLayout", _OnDidChangeKeyboardLayout);
|
||||
}
|
||||
|
||||
NODE_MODULE(addon, init)
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#define KEYMAPPING_H_
|
||||
|
||||
#include <node.h>
|
||||
#include <uv.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "../deps/chromium/keyboard_codes.h"
|
||||
|
@ -34,6 +35,7 @@ typedef struct {
|
|||
|
||||
void _GetKeyMap(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void _GetCurrentKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void _OnDidChangeKeyboardLayout(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
} // namespace vscode_keyboard
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче