Search code examples
c++qtkeyboard

Implementing Backspace and Enter Keys on a Qt Virtual Keyboard


I'm implementing a virtual keyboard as in input panel widget based on the example here. I've able to have characters appear in a text widget by emitting a signal like so emit characterGenerated( char ) -- please refer to buttonClicked(QWidget *w) method in the example. Unfortunately this approach does not work for the enter and backspace keys. I'd appreciate advise on possible workarounds.

If it matters, I'm developing in C++.


Solution

  • Consider MyInputPanelContext::sendCharacter function in the linked example. When QKeyEvent keyPress object is constructed, character.unicode() is used as the second argument. However, you can also use Qt::Key values as the second argument. For example:

    QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
    

    You should rewrite this method to accept int argument that will be passed to QKeyEvent constructor and change characterGenerated(QChar) signal to keyGenerated(int) so you can send a special key (Qt::Key_Backspace) or a character key (character.unicode()) using the same signal and slot.