Last active
February 18, 2023 22:56
-
-
Save paulo-raca/0e772864013b88de205a to your computer and use it in GitHub Desktop.
Using UInput to enter unicode text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from evdev import UInput, ecodes as e | |
import time | |
# This class sends CTRL+SHIFT+U+<HEXCODE> sequences to produces arbitrary unicode characters. | |
# It works fine on Gnome applications, but not on KDE | |
# in https://en.wikipedia.org/wiki/Unicode_input#In_X11_.28Linux_and_other_Unix_variants.29 | |
class UnicodeInput: | |
def __init__(self): | |
keys = [ | |
e.KEY_LEFTCTRL, | |
e.KEY_LEFTSHIFT, | |
e.KEY_U, | |
e.KEY_0, | |
e.KEY_1, | |
e.KEY_2, | |
e.KEY_3, | |
e.KEY_4, | |
e.KEY_5, | |
e.KEY_6, | |
e.KEY_7, | |
e.KEY_8, | |
e.KEY_9, | |
e.KEY_A, | |
e.KEY_B, | |
e.KEY_C, | |
e.KEY_D, | |
e.KEY_E, | |
e.KEY_F | |
] | |
self.uinput = UInput(events={e.EV_KEY: keys}, name="Unicode Virtual Input Device") | |
def close(self): | |
self.uinput.close() | |
def __enter__(self): | |
return self | |
def __exit__(self, type, value, tb): | |
self.close() | |
def write(self, string, delay=0.02): | |
for c in string: | |
self.write_char(c) | |
time.sleep(delay) | |
def write_char(self, char): | |
ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 1) | |
ui.syn() | |
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 1) | |
ui.syn() | |
ui.write(e.EV_KEY, e.KEY_U, 1) | |
ui.syn() | |
ui.write(e.EV_KEY, e.KEY_U, 0) | |
ui.syn() | |
for hex_digit in '%X' % ord(char): | |
keycode = getattr(e, 'KEY_%s' % hex_digit) | |
ui.write(e.EV_KEY, keycode, 1) | |
ui.syn() | |
ui.write(e.EV_KEY, keycode, 0) | |
ui.syn() | |
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 0) | |
ui.syn() | |
ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 0) | |
ui.syn() | |
if __name__ == "__main__": | |
string = """ | |
Mary had a little lamb | |
Зарегистрируйтесь сейчас на Десятую Международную Конференцию по | |
გთხოვთ ახლავე გაიაროთ რეგისტრაცია Unicode-ის მეათე საერთაშორისო | |
แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช พระปกเกศกองบู๊กู้ขึ้นใหม่ | |
ሰማይ አይታረስ ንጉሥ አይከሰስ። | |
ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ | |
ℕ ⊆ ℕ₀ ⊂ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ, ⊥ < a ≠ b ≡ c ≤ d ≪ ⊤ ⇒ (A ⇔ B), | |
""" | |
with UnicodeInput() as keyboard: | |
keyboard.write(string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have since come to read something that says Ctrl-Shift-U might also work with
fcitx
, although a documentation page forfcitx
says the default is Ctrl-Shift-Alt-U.That's interesting that it would work in GTK apps without
ibus
installed at all. Have you verified thatibus
is not present on your system?What I can tell you is that when I set up
ibus
and chose it as the input manager withim-chooser
, this Unicode entry method started working in KDE applications like Kate. So maybe the GTK apps are making themselves compatible with this method independent ofibus
, or they install and useibus
on their own, but if you choose to useibus
in place of whatever KDE uses by default it will also make it work in the KDE apps. I've tested this in Kubuntu.