Search code examples
qt5keyboard-layout

Override keyboard map in Qt application


How can I override system keyboard layout in a Qt application? I need to change some character codes. The basic problem is that SHIFT+SPACE does not put ZWNJ (U+200C) in Qt text engine (Standard Persian layout has 200C for SHIFT+SPACE and it’s working in all places except Qt application).

This is certainly a bug, but I can’t wait for patch versions. I need to to this manually.

Qt version: 5.3.1
Operating system: Windows (XP upto 8)


Solution

  • You need to subclass the GUI class (I assume QLineEdit or QTextEdit and implement your own version of keyPressEvent to detect when the user has pressed Shift+Space. Then you can insert the ZWNJ with insertPlainText.

    void MyTextEdit::keyPressEvent(QKeyEvent *e)
    {
        QTextEdit::keyPressEvent(e);
        if( e->key() == 0x200c )
        {
            insertPlainText( QChar(0x200C) );
        }
    }
    

    In the above code, e->key() == 0x200c should not work. QKeyEvent::key() should return a value from Qt::Key, rather than the Unicode value of ZWNJ (Qt::Key has no element 0x200c). Nevertheless, that's how Qt 5.3 works for me.