Results 1 to 2 of 2

Thread: QML virtual keyboard + TextEdit

  1. #1
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QML virtual keyboard + TextEdit

    Hi all,

    I'm attempting to build a virtual keyboard in my QML application (Qt 4.8.5 and QML 1.1).
    What I really need is to simulate key events when pressing a key buttons of virtual keyboard.

    I searched online for some solutions but I don't find a clear one.
    The solution most listed seems to be to create a custom c++ class (with an Invokable method) that should simulate the key press and release event.

    Surprisingly I did not find any example about it so I tried to build it but, into sendEvent call (bool QCoreApplication::sendEvent ( QObject * receiver, QEvent * event ) [static]), I don't know what set as receiver... or better I don't know how to pass the QML TextEdit "instance" as receiver.

    How to do it?
    Or otherwise there is another way to do it?


    Part of my class code:

    Qt Code:
    1. void KeyEmitter::simulateKey(QString keyName)
    2. {
    3. // getting selected keycode
    4. Qt::Key keycode = _getKeyCodeFromName(keyName);
    5. if ( keycode == Qt::Key_unknown )
    6. {
    7. #ifdef DEBUG_ENABLED
    8. qDebug() << "Cannot resolve keycode associated to " << keyName;
    9. #endif
    10. return;
    11. }
    12.  
    13. QObject * receiver = NULL; // ???
    14.  
    15. if (keyName.length() == 1 && keyName[0].unicode() >= 'A' && keyName[0].unicode() <= 'Z')
    16. {
    17. QKeyEvent keyPressEvent(QEvent::KeyPress, keycode, Qt::ShiftModifier, keyName);
    18. QApplication::sendEvent(receiver, &keyPressEvent);
    19.  
    20. QKeyEvent keyReleaseEvent(QEvent::KeyRelease, keycode, Qt::ShiftModifier, keyName);
    21. QApplication::sendEvent(receiver, &keyReleaseEvent);
    22. }
    23. else
    24. {
    25. QKeyEvent keyPressEvent(QEvent::KeyPress, keycode, Qt::NoModifier, keyName);
    26. QApplication::sendEvent(receiver, &keyPressEvent);
    27.  
    28. QKeyEvent keyReleaseEvent(QEvent::KeyRelease, keycode, Qt::NoModifier, keyName);
    29. QApplication::sendEvent(receiver, &keyReleaseEvent);
    30. }
    31.  
    32. #ifdef DEBUG_ENABLED
    33. qDebug() << "Simulated key " << qPrintable(keyName);
    34. #endif
    35. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance


    UPDATE

    I found a similar issue with mouse event in this post : http://stackoverflow.com/questions/7...-clicks-in-qml

    I tried to pass the viewer as receiver in my application and it works!

    Summarizing....
    Into main:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. [...]
    4. QmlApplicationViewer viewer;
    5. [...]
    6. QScopedPointer<KeyEmitter> keyEmitter(new KeyEmitter(&viewer));
    7. viewer.rootContext()->setContextProperty("keyEmitter", keyEmitter.data());
    8. [...]
    9. }
    To copy to clipboard, switch view to plain text mode 

    Into KeyEmitter class:
    Qt Code:
    1. class KeyEmitter : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. KeyEmitter*(QObject * receiver*, QObject * parent = NULL); // Added passing of receiver
    8.  
    9. Q_INVOKABLE void simulateKey(QString keyName);
    10. [...]
    11.  
    12. private:
    13.  
    14. QObject * _receiver;
    15. [...]
    16. };
    To copy to clipboard, switch view to plain text mode 

    into QML:
    Qt Code:
    1. IconButton // is an implementation of a "standard" button
    2. {
    3. id: abutton
    4. text: "a"
    5. onClicked: keyEmitter.simulateKey(text)
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nick85; 14th January 2014 at 11:10.

  2. #2
    Join Date
    Sep 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QML virtual keyboard + TextEdit

    Hi,

    thanks for posting your solution after you got it working I've got a few difficulties with my KeyEmitter class. Have you still got access to your code and could post the whole class code?

    Thank you in advance

Similar Threads

  1. Virtual Keyboard
    By NoRulez in forum Qt Programming
    Replies: 9
    Last Post: 5th August 2010, 07:02
  2. How to get the virtual keyboard?
    By ramesh.bs in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 2nd June 2010, 11:58
  3. like virtual keyboard?
    By triperzonak in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2008, 16:42
  4. Virtual Keyboard
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2007, 19:59
  5. virtual keyboard
    By sar_van81 in forum Qt Programming
    Replies: 5
    Last Post: 22nd December 2006, 13:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.