[Python] ouput unicode character by suppressing original keystroke using evdev python in linux

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Kali 2022 (Linux) with xfce 4.16. I am experimenting with python evdev module. What I am trying to achieve is when pressing certain key on the keyboard (like letter 'a'), instead of letter 'a', I want to post a specific Unicode character on the focus element.In other words, I want to map key_A with a different unicode character which is not 'a'.

    In Kali Linux, I can print Unicode character manually by pressing ctrl+shit+u then type the Unicode hex-code for the character. But when I simulate this keystrokes using uinput in python,the simulated keystrokes post the indivisual characters (like u0985) in focus element. linux for some reason is not showing the unicode character represented by it.What is the problem in my code, and how it can print the actual Unicode.

    import evdev
    from evdev import UInput, ecodes as e
    import time

    # Open the input device (replace with your actual device)
    dev = evdev.InputDevice('/dev/input/event2')
    dev.grab() # Grab the device to receive all events

    # Create a virtual uinput device with capabilities
    cap = {e.EV_KEY: [e.KEY_A, e.KEY_B,e.KEY_G,e.KEY_U,e.KEY_0,e.KEY_9,e.KEY_8,e.KEY_5,e.KEY_ENTER]}
    ui = UInput(cap, name='my_virtual_keyboard')

    for event in dev.read_loop():

    if event.type == e.EV_KEY:

    key_event = evdev.categorize(event)
    if key_event.keystate == evdev.KeyEvent.key_down:

    # Simulate pressing Ctrl+Shift+U
    ui.write(event.type, e.KEY_LEFTCTRL, 1)
    ui.write(event.type, e.KEY_LEFTSHIFT, 1)
    ui.write(event.type, e.KEY_U, 1)
    ui.write(event.type, e.KEY_U, 0) # Release U
    ui.write(event.type, e.KEY_LEFTSHIFT, 0) # Release Shift
    ui.write(event.type, e.KEY_LEFTCTRL, 0) # Release Ctrl
    ui.syn() # Synchronize events
    time.sleep(0.1) # Small delay for the system to register the input mode


    # This would involve simulating individual key presses for '0', '9', '8', '5'
    # ... (code to simulate these key presses) ...
    ui.write(event.type, e.KEY_0, 1)
    ui.write(event.type, e.KEY_0, 0)

    ui.write(event.type, e.KEY_9, 1)
    ui.write(event.type, e.KEY_9, 0)

    ui.write(event.type, e.KEY_8, 1)
    ui.write(event.type, e.KEY_8, 0)

    ui.write(event.type, e.KEY_5, 1)
    ui.write(event.type, e.KEY_5, 0)
    ui.syn()
    time.sleep(0.1) # Small delay for the system to register the input mode

    # Simulate pressing Enter to confirm the Unicode input
    ui.write(event.type, e.KEY_ENTER, 1)
    ui.write(event.type, e.KEY_ENTER, 0)
    ui.syn()

    elif key_event.keystate == evdev.KeyEvent.key_up:
    pass

    else:
    # Re-inject other event types (e.g., EV_SYN)
    ui.write(event.type, event.code, event.value)

    #ungrab and close
    dev.ungrab()
    ui.close()

    Continue reading...

Compartilhe esta Página