PDA

View Full Version : Fake key events



Bolick
15th July 2009, 15:17
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:



void my_app::on_pb_control_0_clicked ()
{
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier, "Tab", false, 0 );
QApplication::sendEvent(this, &key);
}

void my_app::on_pb_control_1_clicked ()
{
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier, "Tab-Shift", false, 0 );
QApplication::sendEvent(this, &key);
}

void my_app::on_pb_control_2_clicked ()
{
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Return, Qt::NoModifier, "Return", false, 0 );
QApplication::sendEvent(this, &key);
}

void my_app::on_pb_control_3_clicked ()
{
QKeyEvent key(QKeyEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, "Space", false, 0 );
QApplication::sendEvent(this, &key);
}


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?

jpn
15th July 2009, 22:00
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.

Bolick
16th July 2009, 10:41
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.

Bolick
16th July 2009, 18:55
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.


void my_app::key_press_release(int key, Qt::KeyboardModifiers modifier)
{
QWidget * widget = QApplication::focusWidget();

QKeyEvent key_press(QKeyEvent::KeyPress, key, modifier, NULL, false, 0 );
QApplication::sendEvent(widget, &key_press);

QKeyEvent key_release(QKeyEvent::KeyRelease, key, modifier, NULL, false, 0 );
QApplication::sendEvent(widget, &key_release);
}

Kin
21st August 2009, 04:29
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