MAC: Use CAPS LOCK key to toggle IME

Source: Internet
Author: User
Tags lua modifier

MAC: Use CAPS LOCK key to toggle IMEMotive

The Caps LOCK key is one of the fewest keys used on my keyboard. Say is one, one is because my keyboard also has a shutdown key use frequency and it has a spell, second, because of its strategic location is often mistakenly pressed.

In fact, in the Chromebook, the CAPS LOCK key is replaced by Google's more commonly used "search key", in addition, there are vimer to use the CAPS LOCK key as the ESC key, the effect tension pile.

According to personal habits, I finally decided to change the CAPS LOCK key to the input method toggle key, one is because as a Chinese IME switch is one of the most used one of the shortcut keys, another reason is because the key on the light can also be used as input Method indicator light, simply perfect.

Change Key Bindings
    • Turn on System Preferences-keyboard-keyboard-modifier keys , set the CAPS LOCK key to "no action"
    • Download Seil (Conscience software, conscience author), open after the CAPS LOCK key is mapped to key Code 80 (or another non-existent key, 80 represents F19 )
    • Open System Preferences-keyboard-shortcuts-input source , set the shortcut key for switching IME to F19 (press caps Lock after selecting)

done! But now the problem: caps locked lights never light, this is not beautiful! We want to change the indicator light to be able to indicate the input method status, that is, in the English state is not bright, in the Pinyin/Wubi and other input method status light. Google has discovered that OS X provides the underlying API for controlling keyboard lights, which can be manually controlled, as described in Maclight. This is good, so I tried the following several solutions in turn:

    • Write a shell script switch Input method (via AppleScript analog keystroke) + toggle LED status, create a new "service" by Automator and then bind the CAPS LOCK key to run the script. However, after testing the delay is too large ( ~200ms ), give up.
    • Use Objective-c write a program called the underlying API to switch the input method (through the TISSelectInputSource series API) + Toggle LED status, found that the call API switch Input method needs to switch to the next input window to take effect, and the delay is still very large, give up.
    • Write a background app with Objective-c, and NSDistributedNotificationCenter change the LEDs according to the status by receiving input method change events. Science!

Finally, this scientific approach was adopted. Of course, this background application only need to be the command line application can be, and launchctl so on automatically start the boot. However, due to obsessive-compulsive disorder (easy to start, exit, easy to add to the login startup) or write the occupation in the status bar of the application, and named IMLight , such as:

Download Link: Point me or Fork me at GitHub

Update for MacOS Sierra

After upgrading MacOS Sierra, Seil does not work properly (Imlight does not affect), see this issue on GitHub, and because of the large changes in the system interface, womb may not have repair updates.

It has also been mentioned in issue that you can use another project Karabiner-elements for Sierra that the author is developing, but this project has a few questions for me:

    • Conflict with imlight (although not necessarily his problem, but I do not know how to fix ...) )
    • Will invalidate the modifier keys for multiple keyboards in System preferences (such as the inability to swap alt and CMD for an external keyboard), the author says it cannot be repaired

Another solution is to set the CAPS lock to CTRL (or something else) in System preferences and then remap with other software, such as keyboard Maestro (changing the Caps lock thing to the bottom, requiring kernel-level modifications, Listening to CTRL and other keys is a simple matter. I use the free Hammerspoon to achieve:

Local M={}Local Events= HS. Eventtap. event. typesm. log= HS. Logger. New(' Caps_remap ',' Info ') M. last_flags_1={}m. last_flags_0={}m. last_time_1=0M. last_time_0=0M. timeout=0.15M. Key="Ctrl" M. Action=function() HS. Eventtap. Keystroke({},"F19")EndLocalfunction _dict_has_no_other_key(DIC)For KVInchPairs(DIC)DoIf k~= M. KeyThenReturnFalseEndEndReturnTrueEndfunction M. event_callback(E)Local Typ= E:gettype()Local code= E:getkeycode()Local flags= E:getflags()Local now= HS. timer. Secondssinceepoch()If _dict_has_no_other_key(Flags)andNot flags[M. Key]and _dict_has_no_other_key(M. last_flags_0)and M. last_flags_0[M. Key]and _dict_has_no_other_key(M. last_flags_1)andNot M. last_flags_1[M. Key]And now-M. last_time_0< M. timeoutThen M. log. I("Fire caps Action")If M. ActionThen M. Action()EndEnd M. last_flags_1= M. Last_flags_0 M. last_flags_0= Flags M. last_time_1= M. Last_time_0 M. last_time_0= NowReturnFalseEndfunction M. Init(Options)If options. KeyThen M.key = options.key end if Options.timeout then m.timeout = options.timeout end if options.action then M.action = options.action end M .watcher = hs.eventtap.new ( {events.flagschanged}, M.event_callback) M< Span class= "OT" >.watcher:start () endreturn M    

That is, quickly pressing CTRL (that is, caps Lock) triggers F19, while other key combinations that contain CTRL do not, and can meet the requirements.

MAC: Use CAPS LOCK key to toggle IME

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.