Anthony
Anthony

Reputation: 12736

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.

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

Upvotes: 6

Views: 1762

Answers (2)

Smilin Brian
Smilin Brian

Reputation: 990

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);

Upvotes: 5

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

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;
}

Upvotes: 1

Related Questions