How do I trigger inputMethodEvent() in a QTextEdit object?
I have created the backend code for my keyboard driver. The driver will take roman characters as input and output Tibetan Unicode.
I would like to embed this driver in my application and I am currently trying to understand the best way to achieve this.
I know I could hack together something where I handle events for every single character that might be inputted, but this seems like a really ugly hack where I'm trying to poorly reinvent functionality that seems to already be available in Qt, namely through QInputMethodEvent, since it seems to handle intermediate states where a partially typed string will not yield valid output from my driver.
I have subclassed QTextEdit, called the object's .setAttribute(Qt.WA_InputMethodEnabled) and tried to implement inputMethodEvent() and see if this event is triggered by keypresses, but inputMethodEvent() isn't called at all.
I found that there's also QInputContext, which can send a QInputMethodEvent, but since I'm not very familiar with Qt yet and trying to learn by doing this, I'm not sure how I utilize QInputContext.
Basically I would like to be able to do the following:
Intercept keyboard events and pass them to a buffer
Process the buffer with my driver.
- Represent intermediate states in the QTextEdit widget.
- Replace the intermediate output in the QTextEdit widget with the completed Tibetan Unicode word
I am completely lost, so any help whatsoever would be of tremendous benefit. Just knowing how to trigger the inputMethodEvent() would be a great start.
I need this to work on Linux, OSX and Windows, using Qt 4.8.
deu439 last edited by
I think you are having similar problem as me. Although if you only want to use the "keyboard driver" in your own qt application you could probably use QInputContext to filter and modify the keyboard input.
This may be helpful:
You subclass the QInputContext class and implement the filterEvent() method where you detect the pressed key from QEvent *event. You translate the detected key to your tibetian encoding and send if further to the widget using either QInputContext::sendEvent() or QApplication::sendEvent.
And finally, cant you just select tibetian keyboard layout in your os?
Thank you SO much for the game plan. I really was clueless there, so thank you so much for sharing.
The answer to your question is basically 'yes', but I want to provide a unique driver where there's not a 1:1 mapping of keyboard keys and characters, but rather more complex "translation" of strings of roman characters. I am also writing software for computer illiterate users, who might have trouble setting up Tibetan input on their OS.
deu439, I've tried to implement filterEvent, but again I am faced with the same problem .. it is never called. Can you please help me understand why?
I have subclassed QInputContext and called the QApplication.setInputContext(myIC), but filterEvent is never called.
deu439 last edited by
Well I dont see why this happens, maybe you could share your source code with me? What I work on now is qt plugin that would process input keystrokes into a string as you say in rather complex way. What I have now is plugin that work with all qt applications and which recieves the keyboard events before they arrive to the widget, I can process it and send it further into the widget.
Hope it helps you a little bit:
This is just a little example which allows you only to input one character into any widget.
To make it work you just build it and copy the .so library into QTDIR/plugins/inputmethods