• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Python
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

compose key for macos

Compose Key On Macos

gen-compose - generates compose key keybindings for macos.

A compose key (sometimes called multi key) is a key on a computer keyboard that indicates that the following (usually 2 or more) keystrokes trigger the insertion of an alternate character, typically a precomposed character or a symbol. https://en.wikipedia.org/wiki/Compose_key

Compose keys lets you insert complex character by entering multiple characters in a succession:

screencast demonstration of compose key

e.g. <compose_key> + d + o + w + n will insert 👇

Mac os doesn't come with a compose key feature built-in. However there's a short hack to make it work:

  1. Keys can be rebound in mac via ~/Library/KeyBindings/DefaultKeyBinding.dict dictionary file.
  2. The rebound keys can be chained like compose keys e.g. pressing abcd can be made to insert AlphaBetaCharlieDad
  3. Modifier keys cannot be rebound

With these three rules we can replicate compose key and even set it to work with a mod key!

Install

gen-compose can be installed via python manager with py3.6+ versions:

$ pip3 install --user gen-compose
$ gen-compose --help
Usage: gen-compose [OPTIONS] [MAPPINGS]...

  Generate macos rebind file from compose json mapping

Options:
  --key TEXT  key to use as compose key  [default: §]
  -r, --raw   just keymap without prefix
  --help      Show this message and exit.

Preconfig

  1. First lets fix modifier key issue by forcing modifier to be a character. For example to use right_options key we need to use karabiner elements and remap it to some unused key like non_us_backslash: karabiner compose screenshot

  2. Now we have the compose key ready: if we click right_options it should insert § character
    However we cannot compose anything yet as we have no compose mappings yet. For that we need to modify keybindings dictionary located in ~/Library/KeyBindings/DefaultKeyBinding.dict.
    It's written in some cryptic hard to edit format and here's where gen-compose comes in and lets you write yaml files instead!

Usage

  1. Create yaml mappings file (e.g. mappings/readme.yaml):
    cat: "(^≗ω≗^)"
    "+1": 👍
    "-1": 👎
    ":(": "my face is sad"
    This map defines key combinations and texts that will be inserted, e.g. <compose_key><plus><number 1> will insert thumbs up.
    note: see mappings directory for some built in mappings
  2. Using gen-compose we generated .dict keybind file file from our yaml configuration:
    $ gen-compose mappings/readme.yaml
    {"§" = {
      "c" = {
        "a" = {
          "t" = ("insertText:", "(^≗ω≗^)");
        };
      };
      "+" = {
        "1" = ("insertText:", "👍");
      };
      "-" = {
        "1" = ("insertText:", "👎");
      };
      ":" = {
        "(" = ("insertText:", "my face is sad");
      };
    };}
    note: multiple mappings can be used to generate a single keymap: $ gen-compose map1.yaml map2.yaml
  3. Now save it directly to keybinds file:
    $ gen-compose mappings/readme.yaml > ~/Library/KeyBindings/DefaultKeyBinding.dict
  4. Restart your programs and type §+1 and you'll see 👍!
  5. Customize your own mapping or see /mappings for some existing configurations and have fun!

note: Some programs need a hard reboot to take in the map, like kill -9 sort of reboot to start working.

Using XCompose Mappings

Linux's xcompose mappings are supported via experimental conversion:

$ gen-compose-convert xcompose --help
Usage: gen-compose-convert xcompose [OPTIONS] [FILES]...

  Convert xcompose file, that follows format like: <Multi_key> <parenleft>
  <period> <1> <parenright>: "⑴"

Options:
  -c, --keep-comments  keep inline comments
  --help               Show this message and exit.

For example:

$ head mappings/example.compose
<Multi_key> <B> <bar> : "₿" U20BF  # BITCOIN SIGN
...
$ gen-compose-convert xcompose mappings/xcompose.compose --keep-comments > example.yaml
$ cat example.yaml
"B|": "₿"  # BITCOIN SIGN
...

Exporting to XCompose

gen-compose-export command can be used export yaml configuration back to XCompose file:

$ gen-compose-export --help
Usage: gen-compose-export [OPTIONS] YAMLFILE

  Export .yaml config file back to XCompose file i.e. this is reverse of
  gen-compose-convert

Options:
  --help  Show this message and exit.

For example:

$ head mappings/xcompose.yaml 
"..": "…"  # HORIZONTAL ELLIPSIS
"v..": "⋮"  # VERTICAL ELLIPSIS
...
$ gen-compose-export mappings/xcompose.yaml > xcompose.compose
$ head xcompose.compose
<Multi_key> <period><period>:"…"
<Multi_key> <v><period><period>:"⋮"
<Multi_key> <c><period><period>:"⋯"
<Multi_key> <slash><period><period>:"⋰"
<Multi_key> <period><backslash><period>:"⋱"
<Multi_key> <2><period>:"‥"
<Multi_key> <c><1><period>:"·"
<Multi_key> <period><slash><period>:"⁒"
<Multi_key> <ampersand><at>:"⅋"
<Multi_key> <ampersand><7>:"⁊"
...

Notes and Issues

  • There's no way to really debug this other than trial and error and making sure applications are restarted after every update.
  • When multiple keymaps (eg compose-gen map1.yaml map2.yal) provided they will be joined and updated in order they are provided.
  • Keymap does not allow completely overlapping keys. e.g. egg and eggplant will break because it's impossible to map two overlapping values as keymap functionality has no timeout (i.e. break if nothing is typed after a second and insert egg, instead it will wait indefinitely for you to finish eggplant)
    If overlap happens compose-gen will choose the shorter form and discard the long one.
    To fix this add end terminator character such as space: egg<space> rather than egg in your mappings.
  • DefaultKeyBinding.dict map doesn't work in some popular programs:

Related Resources

https://github.com/gnarf/osx-compose-key
http://lolengine.net/blog/2012/06/17/compose-key-on-os-x
http://bob.cakebox.net/osxcompose.php
https://github.com/ttscoff/KeyBindings

More Repositories

1

playwright-stealth

JavaScript
119
star
2

plogbook

Console application for personal logging, which turns logs into readable html pages.
HTML
26
star
3

parsel-cli

cli for evaluating css and xpath selectors
Python
25
star
4

GosuGamers-API

Python unofficial API/Scraper for http://gosugamers.net
Python
20
star
5

xonsh-autoxsh

Automatically execute scripts for directories in Xonsh Shell.
Python
15
star
6

choosem

dropdown picker/launcher for mac os
Python
15
star
7

playwright-pool

Demonstration on how to use async python to control multiple playwright browsers for web-scraping
Python
11
star
8

bandcamp-downloader

Minimal cli app for downloading public albums from bandcamp
Python
9
star
9

.dotfiles

my .dotfiles
Python
8
star
10

Dictionary_com

Python API/Scraper for Dictionary.com
Python
6
star
11

ggmt

esport match ticker based on gosugamers.com data
Python
6
star
12

splash-stresstest

stress test for splash http javscript rendering service
Python
6
star
13

musicforprogramming-dl

Easy cli downloader for musicforprogramming.net
Python
6
star
14

xonsh-click-tabcomplete

Xonsh shell tab completion for python applications based on click
Python
3
star
15

xonsh-vox-tabcomplete

Vox tabcomplete support for Xonsh shell
Xonsh
3
star
16

bin

my ~/bin scripts and tools. Everything is under GPLv3+ where possible.
Python
3
star
17

pq

command line xml and json processor for xpath and css selectors.
Python
2
star
18

xonsh-scrapy-tabcomplete

Scrapy tabcomplete support for Xonsh shell
Python
2
star
19

line-sticker-dl

sticker file downloader for LINE chat application
Python
2
star
20

coinbitrage

Cli app to monitor for crypto-currency arbitrages
Python
2
star
21

scrapy-test

framework to test scrapy spiders
Python
2
star
22

terminal-brackets

CLI application for printing out competitive brackets from json data
Python
2
star
23

granitosaurus.github.io

Personal blog
Python
1
star
24

argsin

ArgsIn is an argument manager for user input.
Python
1
star
25

vscode-html-escape

vscode extension for html escaping and unescaping selected text
JavaScript
1
star
26

presentation-scrapy-stackoverflow

Jupyter Notebook
1
star
27

grindy

Grindy is a command line application for long term memory(LTM) training.
Python
1
star
28

wisdom.cat

video aggregator website that aggregates bite size videos which are in some way educational.
Python
1
star
29

steamformat

output steam game info to markdown.
Python
1
star
30

humblestore-crawler

simple crawler for humble bundle store with markdown output.
Python
1
star
31

englishprofile-scraper

scraper for https://www.englishprofile.org/wordlists/evp
Python
1
star
32

curr

currency converter cli app
Python
1
star
33

getproxies

free proxies scraper
Python
1
star