Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SCRIPT] Accented character inserter for English keyboards
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Havin_it
Veteran
Veteran


Joined: 17 Jul 2005
Posts: 1247
Location: Edinburgh, UK

PostPosted: Tue Jul 30, 2019 11:06 am    Post subject: [SCRIPT] Accented character inserter for English keyboards Reply with quote

Background: If you have a plain ol' US/GB keyboard but need to type in many European languages with their accented characters, you need to remember some arcane AltGr key nonsense for each one.

MS Office has/had a slightly more convenient method of dealing with this: a series of keyboard shortcuts that are sort of mnemonic (e.g. Ctrl+' followed by a vowel to insert that letter with an acute accent). Much simpler!

This paradigm was brought to Firefox by the Zombie Keys addon, but sadly this like many useful addons was doomed by Firefox Quantum's move to WebExtensions. Boo!

The withdrawal led me to come up with this little script. It works as closely analogous to the MS technique as I could think to make it (within the limitations of a shell script). Saved as a global keyboard-shortcut in your desktop of choice (I rock KDE but I'm sure other desktops have this feature ... right?) it becomes available in all apps!

Dependencies

  • x11-misc/xclip - Controls the X clipboard
  • x11-misc/xdotool - Simulates input actions


Usage

  1. Save the script below on your system and set a global keyboard shortcut pointing to it in your desktop's settings.
  2. When you're typing and need an accented character, type the letter you want (upper- or lower-case) followed by the special character for the type of accent you want (see the lines at the top of the script for a guide).
  3. Hit your magic keyboard shortcut to unleash the script.
  4. Voilà! ;)


Code:
#!/bin/sh
#
# Read a shortcode from in front of caret (e.g. a' = a-acute)
# and insert the appropriate accented character in its place

declare -A alc=(["'"]=á ["\`"]=à ["^"]=â ["~"]=ã [":"]=ä)
declare -A auc=(["'"]=Á ["\`"]=À ["^"]=Â ["~"]=Ã [":"]=Ä)
declare -A clc=(["?"]=ç)
declare -A cuc=(["?"]=Ç)
declare -A elc=(["'"]=é ["\`"]=è ["^"]=ê [":"]=ë)
declare -A euc=(["'"]=É ["\`"]=È ["^"]=Ê [":"]=Ë)
declare -A ilc=(["'"]=í ["\`"]=ì ["^"]=î [":"]=ï)
declare -A iuc=(["'"]=Í ["\`"]=Ì ["^"]=Î [":"]=Ï)
declare -A nlc=(["~"]=ñ)
declare -A nuc=(["~"]=Ñ)
declare -A olc=(["'"]=ó ["\`"]=ò ["^"]=ô ["~"]=õ [":"]=ö)
declare -A ouc=(["'"]=Ó ["\`"]=Ò ["^"]=Ô ["~"]=Õ [":"]=Ö)
declare -A slc=(["z"]=ß)
declare -A suc=(["z"]=ß)
declare -A ulc=(["'"]=ú ["\`"]=ù ["^"]=û [":"]=ü)
declare -A uuc=(["'"]=Ú ["\`"]=Ù ["^"]=Û [":"]=Ü)
declare -A ylc=(["'"]=ý [":"]=ÿ)
declare -A yuc=(["'"]=Ý [":"]=Ÿ)

# Wait 1 second for you to release your shortcut keypress
sleep 1

# Highlight and cut to clipboard your last 2 characters typed
xdotool key --clearmodifiers shift+Left
xdotool key --clearmodifiers shift+Left
xdotool key --clearmodifiers ctrl+x
IN=$(xclip -selection c -o)

L=${IN:0:1}
ACC=${IN:1}

# See if your letter is upper- or lower-case
CAS=uc
if [ "${L,,}" == "${L}" ]; then
        CAS=lc
fi
declare -nl LTR="${L,,}${CAS}"

# Put your chosen accented character onto the clipboard
echo -n "${LTR[$ACC]}" | xclip -selection c -i

# Paste the character in place of the shortcode
xdotool key --clearmodifiers ctrl+v


Notes

  1. This is pretty rough'n'ready so has no real error-checking as such: if the shortcode you enter isn't valid, it just won't do anything.
  2. You mustn't do anything to move your caret (text cursor) away from where you typed the shortcode (where you want the accented character to be inserted) in the short time the script takes to run.
  3. Whatever shortcut key combo you use to launch the script, make sure to release it within 1 second. xdotool can get confused if modifier-keys are depressed when it runs (like it can leave your X server afterwards thinking a key is still held down when it isn't). If you need more time (or are super-fast and want less), change the number after "sleep".
  4. The "special" characters used in the shortcodes are mostly the same as the MS versions, but not all, and some I left out because I'm lazy and didn't need them :oops: If you can see how the mapping works above, then you can always add/modify them as you like.
  5. The action sequence is obviously a bit different from the MSOffice original: that was <CTRL+special> <letter>, mine is <letter> <special> <your shortcut>. I could have made it <your shortcut> (short timeout) <special> <letter> to be closer to the MS model, but it would still be different and anyway I didn't like having an arbitrary timeout in which to type the shortcode. If you'd prefer it that way, easy enough to move the code-blocks around to achieve this.
  6. I'm pretty confident using this script won't make you sterile, explode the children you already have, or make the word "badger" disappear from existence. But you'd be a fool to take my word for it. Use entirely at your own risk.

    Just me scratching a personal itch, but I hope someone finds it helpful. If there is an existing app that already does this (which is usually the first comment when I post one of these lol), I'm happy to hear about it as this was fun to do but normally I prefer to leave this stuff to the pros ;)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum