Results 1 to 5 of 5

Thread: Fake key events

  1. #1

    Default Fake key events

    I made a basic QT application which has say 4 QPushButtons and 2 QCheckBoxes.

    I want to implement such behavior:
    1-st push button acts like "Tab"
    2-nd button acts like "Tab+Shift"
    3-rd button acts like "Space"
    4-th button as "Enter"

    All buttons are set with focusPolicy=NoFocus
    So only checkboxes could be selected with focus.

    This way I want to select one of the checkboxes (pressing 1-st and 2-nd buttons) then open its pop-up with 3-rd and select option by 4-th.

    Actions for buttons are like that:

    Qt Code:
    1. void my_app::on_pb_control_0_clicked ()
    2. {
    3. QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "Tab", false, 0 );
    4. QApplication::sendEvent(this, &key);
    5. }
    6.  
    7. void my_app::on_pb_control_1_clicked ()
    8. {
    9. QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "Tab-Shift", false, 0 );
    10. QApplication::sendEvent(this, &key);
    11. }
    12.  
    13. void my_app::on_pb_control_2_clicked ()
    14. {
    15. QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Return, Qt::NoModifier, "Return", false, 0 );
    16. QApplication::sendEvent(this, &key);
    17. }
    18.  
    19. void my_app::on_pb_control_3_clicked ()
    20. {
    21. QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, "Space", false, 0 );
    22. QApplication::sendEvent(this, &key);
    23. }
    To copy to clipboard, switch view to plain text mode 

    As a result I have the desired behavior only for "Tab" and "Tab-Shift" pushbuttons (pb_control_0 and _1).

    And nothing happens while I press pb_control_2 and _3.

    But if I press "Enter" or "Space" on the real keyboard Checkbox widget acts in the right way (on Space popup opens and on Enter one of the options selects and popup closes).

    So, what is wrong in my actions?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Fake key events

    I wouldn't recommend abusing key events like this. For example, if you want to move the focus, call QWidget::focusNextPrevChild() instead of sending tab/shift-tab key events. As for your enter/return/space problem, you are sending the event to a wrong widget, aren't you? The receiver should be the currently focused child widget, not the parent window behind them. But again, in this case I'd also probably use signals and slots to do the preferred actions instead of sending fake key events here and there.
    J-P Nurmi

  3. #3

    Default Re: Fake key events

    Thanks a lot for your answer.
    In the real application there will be no pushbuttons I described. All input events will be delivered by UART (through qextserialport lib). So basically I want to implement a sort of mouseless device driven by external buttons and it looks like those fake key events are all I need.

    My thoughts were like that: If I can control my application run on my PC without mouse - why don't implement the same behavior in the embedded device by fake keys.

    Maybe my real problem now is those buttons (which are supposed to be on the external device) I click by my mouse. So when I click one of them something happens with focus (my target widget looses it for a moment for example).

    Anyway I'll try to send some events through UART to the my application and fire the same fake key events. I hope it will work.

  4. #4

    Default Re: Fake key events

    Thanks once again. I've found the issue and how to fix it.
    Issue was: the event was sent to an object which was not in focus.
    Resolution:
    Before event sending I should have been get a pointer to the widget. And send event directly to this widget.
    Qt Code:
    1. void my_app::key_press_release(int key, Qt::KeyboardModifiers modifier)
    2. {
    3. QWidget * widget = QApplication::focusWidget();
    4.  
    5. QKeyEvent key_press(QKeyEvent::KeyPress, key, modifier, NULL, false, 0 );
    6. QApplication::sendEvent(widget, &key_press);
    7.  
    8. QKeyEvent key_release(QKeyEvent::KeyRelease, key, modifier, NULL, false, 0 );
    9. QApplication::sendEvent(widget, &key_release);
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Fake key events

    Hi All:

    Thanks for the keyevent informations.

    But I meet a lot of difficulty..

    I want to press a "A" PushButton on the dialog and send event to TextEdit.

    QApplication::sendEvent(ui->textEdit, new QKeyEvent(QKeyEvent::KeyPress, Qt::Key_A, Qt::NoModifier, text, false, 1));

    I discover whether I key any Qt::Key, TextEdit contents will change follow QKeyEvent parameter 4 "text"!

    Anyone know how can I solve the problem.
    Btw.. I want coding a virtual keyboard dialog on Windows system

Similar Threads

  1. synchronizing events
    By zaphod.b in forum Qt Programming
    Replies: 0
    Last Post: 14th July 2009, 16:29
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. Replies: 9
    Last Post: 22nd June 2008, 22:26
  4. How to suppress user defined events in processEvents()
    By Artschi in forum Qt Programming
    Replies: 5
    Last Post: 5th July 2007, 10:17
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.