├── zsh.png ├── fn.plist ├── fn.py └── README.md /zsh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tonku321/fn-lang-switcher/HEAD/zsh.png -------------------------------------------------------------------------------- /fn.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Label 6 | fn.py 7 | ProgramArguments 8 | 9 | /usr/bin/python3 10 | /Users/norflin/fn.py 11 | 12 | KeepAlive 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /fn.py: -------------------------------------------------------------------------------- 1 | import os 2 | from pynput import keyboard 3 | 4 | def on_press(key): 5 | key_str = '{0}'.format(key) 6 | if (key_str == '<179>'): 7 | stream = os.popen('/usr/local/bin/issw') 8 | output = stream.read().strip() 9 | if (output == 'com.apple.keylayout.ABC'): 10 | os.system('/usr/local/bin/issw com.apple.keylayout.Russian') 11 | else: 12 | os.system('/usr/local/bin/issw com.apple.keylayout.ABC') 13 | 14 | 15 | with keyboard.Listener(on_press=on_press, on_release=None) as listener: 16 | listener.join() 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # THE PROBLEM: 2 | ![ezgif-5-86271457e6](https://user-images.githubusercontent.com/33498670/167284292-2fe06593-0e47-4c7e-8086-8abd2237466c.gif) 3 | 4 | Not only its slow, but when mouse cursor happens to be in the center of the screen, it selects the wrong language for you! 5 | 6 |        7 | # THE SOLUTION (get rid of the pop up): 8 | 9 | Install ["issw"](https://github.com/vovkasm/input-source-switcher) a small utility for macos to switch input sources from a command line. 10 | ------------ 11 | 12 | git clone https://github.com/norflin321/input-source-switcher.git 13 | cd input-source-switcher 14 | mkdir build && cd build 15 | cmake .. 16 | make 17 | make install 18 | 19 | By default executable will be installed as `/usr/local/bin/issw` 20 | 21 | Create fn.py file in ~/. Or clone the repository. 22 | ------------ 23 | 24 | import os 25 | from pynput import keyboard 26 | 27 | def on_press(key): 28 | key_str = '{0}'.format(key) 29 | if (key_str == '<179>'): 30 | stream = os.popen('/usr/local/bin/issw') 31 | output = stream.read().strip() 32 | if (output == 'com.apple.keylayout.ABC'): 33 | os.system('/usr/local/bin/issw com.apple.keylayout.Russian') 34 | else: 35 | os.system('/usr/local/bin/issw com.apple.keylayout.ABC') 36 | 37 | 38 | with keyboard.Listener(on_press=on_press, on_release=None) as listener: 39 | listener.join() 40 | `<179>` is key code for `fn`. Don't forget to run `issw -l` in terminal to get list of available input sources and modify script above if needed! 41 | 42 | Set this to "Do Nothing" 43 | ------------ 44 | ![ezgif-5-aeb126ae5e](https://user-images.githubusercontent.com/33498670/167285047-18f7a509-b56d-4f1f-896a-963c034947dc.jpeg) 45 | 46 | Setup auto run of the script every time you log in (python3 should be installed). 47 | ------------ 48 | 1. Install `pynput` python module: 49 | `/usr/bin/python3 -m pip install pynput` 50 | or, if you are using your own python installation: 51 | `/your/python3/executable/path -m pip install pynput` 52 | 2. Inside `fn.plist` file, change paths to the python executable (if you are using custom python installation) and the script file. Mine is `/Users/norflin/fn.py`. Paths should be full. 53 | 3. Copy the plist file to special directory: `cp -R fn.plist ~/Library/LaunchAgents/`. 54 | 4. Then run this command: `launchctl load ~/Library/LaunchAgents/fn.plist` - it will tell mac to run this file every time you log in. If you want to stop it run `launchctl unload ~/Library/LaunchAgents/fn.plist` and remove the file `rm -rf ~/Library/LaunchAgents/fn.plist`. 55 | 5. Mac might ask you to grant permission for python to monitor input from your keyboard and `Accessibility`. Generally macOS asking about `Input Monitoring`, add your python3 executable to `Accessibility` if no popup with this showed. 56 | 6. Restart. Log in. It should work. 57 | 58 | P.S. Don't forget to reinstall `pynput` after upgrades. Also see [issue #2](https://github.com/norflin321/fn-lang-switcher/issues/2). 59 | 60 |        61 | # RESULT: 62 | You can toggle input source with "fn" button, but without showing the pop up! 63 | 64 | --------------------------------------------------------------------------------