Skip to content

Commit

Permalink
Add onDidChangeKeyboardLayout on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima authored and VS Code committed Mar 21, 2017
1 parent 5293e94 commit 0e4ab55
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/build/
/node_modules/
npm-debug.log
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -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();

Expand All @@ -41,3 +49,6 @@ exports.getCurrentKeyboardLayout = function() {
exports.getKeyMap = function() {
return binding.getKeyMap();
};
exports.onDidChangeKeyboardLayout = function(callback) {
return binding.onDidChangeKeyboardLayout(callback);
}
40 changes: 40 additions & 0 deletions src/keyboard_mac.mm
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/keyboard_win.cc
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/keyboard_x.cc
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/keymapping.cc
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/keymapping.h
Expand Up @@ -7,6 +7,7 @@
#define KEYMAPPING_H_

#include <node.h>
#include <uv.h>
#include <string>
#include <vector>
#include "../deps/chromium/keyboard_codes.h"
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0e4ab55

Please sign in to comment.