I sometimes type with the wrong input language; I like to be able to convert the text typed incorrectly into the correct input language programmatically. I currently use this approach:
##
export persian_chars="ضصثقفغعهخحجچشسیبلاتنمکگظطزرذدپو.ًٌٍَُِّْ][}{|ؤئيإأآة»«:؛كٓژٰٔء<>؟٬٫﷼٪×،)(ـ"
export en_chars="qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?@#\$%^&()_"
##
function per2en() {
gsed "y/$persian_chars/$en_chars/"
}
function en2per() {
gsed "y/$en_chars/$persian_chars/"
}
But this seems brittle and specific to my current keyboard layouts. Any better approachs?
PS: Some examples:
❯ echo آهو ئفشزنخرثقبمخص\! | per2en
Hi, Stackoverflow!
❯ echo 'sghl& nkdh' | en2per
سلام، دنیا
There is no platform-independant API that can do this. On Windows you can try to use
VkKeyScanEx
(char to scan code) andToUnicodeEx
(scan code to char) in a loop to do such conversion.There is Punto Switcher or Caramba Switcher software exist that is doing what you want as a separate tool. Works on Windows and Linux. Maybe other alternatives exist.