Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X: Intercept keyboard layout change

I have a problem. I have two keyboard layouts in my Mac because I have to type in two different languages while communicating with different people. I use the keyboard shortcut Cmd+Space to switch from one layout (language) to another.

I wonder if I can run custom script when Cmd+Space is pressed? I know there is an app called Punto Switcher that can do that.

My idea is to change keyboard highlighting level to indicate current language.

  • Bright = German (or Russian or whatever)
  • Dim = English

The question is where to find API that can

  1. intercept keyboard layout in Mac OS X
  2. change brightness of the keyboard highlight

enter image description here

like image 658
Antonio Avatar asked Mar 01 '13 10:03

Antonio


2 Answers

Neat pointer to the LED brightness stuff from @Anoop Vaidya -- looks interesting!

The system sends a notification when the input method changes.

First, declare a function to receive the notification:

void theKeyboardChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    NSLog(@"Keyboard/input method changed.");
}

Then register for the change notification:

CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
    myContextInfo, theKeyboardChanged,
    kTISNotifySelectedKeyboardInputSourceChanged, NULL,
    CFNotificationSuspensionBehaviorDeliverImmediately);
like image 142
Smilin Brian Avatar answered Oct 05 '22 00:10

Smilin Brian


I found a blog of Amit Singh, where he gave idea as in undocumented APIs , he used C, for this, you can surely find some sort of help from this.

Experimenting With Light.

Or you can try with these codes:

UInt64 lightInsideGetLEDBrightness(){
    kern_return_t kr = 0;
    IOItemCount   scalarInputCount  = 1;
    IOItemCount   scalarOutputCount = 1;
    UInt64        in_unknown = 0, out_brightness;
    kr = IOConnectCallScalarMethod(dataPort, kGetLEDBrightnessID, &in_unknown, scalarInputCount, &out_brightness, &scalarOutputCount);
    return out_brightness;
}
like image 33
Anoop Vaidya Avatar answered Oct 05 '22 00:10

Anoop Vaidya