c++ - How to synthesize key press events? -
i able key value's hal through call function in qt. created event key qkeyevent *event = new qkeyevent (qevent::keypress, inputkey.keyvalue, qt::nomodifier);
note: inputkey.keyvalue key value received hal layer.
now need register key event in qt, if key press happened in ir remote in respective form, keypressevent(e) or event(e) invoke. , based on key press, specific action execute.
note: more 1 form there, key press event trigger , more 1 keys there "page_up, page_down, ok key etc....."
tried invoke postevent() , connect(.......) nothing helped me. keypressevent() not getting executed.
e.g. this:
// receiver pointer qobject qcoreapplication::postevent (receiver, event);
you can find more info here.
you can reimplement qobject::event()
or qwidget::keypressevent
in widget receive key events. visit link or link more information. see example code below consists of 2 buttons , label. clicking pushbutton
sends 'enter pressed' , pushbutton_2
sends 'letter pressed'. key events received in event()
function , label updated accordingly.
mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); connect(ui->pushbutton, signal(clicked()), this, slot(sendkeyevent())); connect(ui->pushbutton_2, signal(clicked()), this, slot(sendkeyevent())); } void mainwindow::sendkeyevent() { qobject* button = qobject::sender(); if (button == ui->pushbutton) { qkeyevent *event = new qkeyevent (qevent::keypress, qt::key_enter, qt::nomodifier); qcoreapplication::postevent (this, event); } else if (button == ui->pushbutton_2) { qkeyevent *event = new qkeyevent (qevent::keypress, qt::key_a, qt::nomodifier); qcoreapplication::postevent (this, event); } } bool mainwindow::event(qevent *event) { if (event->type() == qevent::keypress) { qkeyevent *keyevent = static_cast<qkeyevent *>(event); if (keyevent->key() == qt::key_enter) { ui->label->settext("enter received"); return true; } else if (keyevent->key() == qt::key_a) { ui->label->settext("a received"); return true; } } return qwidget::event(event); }
Comments
Post a Comment