Skip to content

Instantly share code, notes, and snippets.

@itpcc
Last active December 3, 2023 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itpcc/f105ac295453ea19e043dabeb74a6720 to your computer and use it in GitHub Desktop.
Save itpcc/f105ac295453ea19e043dabeb74a6720 to your computer and use it in GitHub Desktop.
layoutFix.exe on Linux Mint using AutoKey

What

This snippet is intended for me, who using Thai Kedmanee and US English keyboard as a daily driver, to correct myself if I, for some reason, type any text in incorrect layout. For example, I want to type "สวัสดี" but I select US one, I'll instead got "l;ylfu" or "Hello" and got "็ำสสน". You get the idea how frustrate I am when it occur.

In Windows, I used to use a programme call "LayoutFix.exe" which, when highlight incorrect phrase and push <ctrl>+<backspace>, it'll copy that text, correct the layout, and paste the correct text in place instead. Save me countless times.

I "move" to Linux Mint and, since I miss that feature so bad, so I implement one.

How

  1. Obviously, install AutoKey
  2. Create a new script. Paste LayoutFix.py content.
  3. Set hotkey to <ctrl>+<backspace>
  4. ... and there was much rejoicing!

image

LOOKUP_TBL_US = '~!@#$%^&*()_+`1234567890-=QWERTYUIOP{}|qwertyuiop[]\ASDFGHJKL:"asdfghjkl;\'ZXCVBNM<>?zxcvbnm,./'
LOOKUP_TBL_TH = '%+๑๒๓๔ู฿๕๖๗๘๙_ๅ/-ภถุึคตจขช๐"ฎฑธํ๊ณฯญฐ,ฅๆไำพะัีรนยบลฃฤฆฏโฌ็๋ษศซ.ฟหกดเ้่าสวง()ฉฮฺ์?ฒฬฦผปแอิืทมใฝ'
origText = clipboard.get_selection()
# https://superuser.com/a/1432982
langCode = system.exec_command("xset -q | grep LED | awk '{ print $10 }'", getOutput=True)
crctText = ''
for ot in origText:
lkuPos = -1
# Current: US -> TH
if langCode == '00000002':
lkuPos = LOOKUP_TBL_US.find(ot)
if lkuPos != -1:
crctText = crctText + LOOKUP_TBL_TH[lkuPos]
# Current: TH -> US
elif langCode == '00001002':
lkuPos = LOOKUP_TBL_TH.find(ot)
if lkuPos != -1:
crctText = crctText + LOOKUP_TBL_US[lkuPos]
if lkuPos == -1:
crctText = crctText + ot
keyboard.send_key("<delete>")
clipboard.fill_clipboard(crctText)
time.sleep(0.1)
keyboard.send_keys("<ctrl>+v")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment