Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

observeCurrentKeyboardLayout not working when invoke by node #38

Open
chemzqm opened this issue Apr 2, 2018 · 4 comments
Open

observeCurrentKeyboardLayout not working when invoke by node #38

chemzqm opened this issue Apr 2, 2018 · 4 comments

Comments

@chemzqm
Copy link

chemzqm commented Apr 2, 2018

I have code

const KeyboardLayout = require('keyboard-layout')

let subscription = KeyboardLayout.observeCurrentKeyboardLayout((layout) => console.log(layout))

and run node test.js
I can get the output after first run, but no more when I change keyboard layout.

@chemzqm
Copy link
Author

chemzqm commented Apr 2, 2018

I've figured out the callback notificationHandler would never get called

@chemzqm chemzqm changed the title observeCurrentKeyboardLayout not working on MacOS high sierra observeCurrentKeyboardLayout not working when invoke by node Apr 2, 2018
@chemzqm
Copy link
Author

chemzqm commented Apr 2, 2018

Ok, I find out that the CFNotificationCenterAddObserver of Mac not works when using command line, it works with app.

@chemzqm
Copy link
Author

chemzqm commented Feb 25, 2019

I've got the fix:

//uv_loop_t *loop = uv_default_loop();
//uv_async_t async;

static void notificationHandler(CFNotificationCenterRef center, void *manager, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  //async.data = manager;
  //uv_async_send(&async);
  //manager->HandleKeyboardLayoutChanged();
  //callback->Call(0, NULL);
  (static_cast<IMSelectManager *>(manager))->HandleKeyboardLayoutChanged();
}

static void asyncSendHandler(uv_async_t *handle) {
  (static_cast<IMSelectManager *>(handle->data))->HandleKeyboardLayoutChanged();
}

IMSelectManager::IMSelectManager(Nan::Callback *callback) : callback(callback) {
  //uv_async_init(loop, &async, (uv_async_cb) asyncSendHandler);

  CFNotificationCenterAddObserver(
      CFNotificationCenterGetDistributedCenter(),
      this,
      &notificationHandler,
      kTISNotifySelectedKeyboardInputSourceChanged,
      NULL,
      CFNotificationSuspensionBehaviorDeliverImmediately
  );

  CFRunLoopRun();
}

Key point is use CFRunLoopRun instead of uv_default_loop

@wirpet
Copy link

wirpet commented Mar 13, 2020

Have you anyone tried this "observeCurrentKeyboardLayout" API on Win32? I got the same issue. Changing keyboard it never trigger the callback. I'm trying to do the same here on Win32. Any suggestion or help?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants