PDA

View Full Version : cant seem to catch keyboard events for key_return, key_space, key_tab, key_enter



swankster
27th June 2018, 16:58
I have dialog with space and return references. I want to also be able to catch the physical keystrokes on my keyboard. everything appears to be work except for.
key_return, key_space, key_tab, key_enter. I am focusing on key_Return not the numpad key_enter.

the following will not trigger any event for any of the above keys.

.h
protected
void keyPressEvent(QKeyEvent *);



.cpp
void KeyBoard::keyPressEvent(QKeyEvent *event)
{
qDebug()<<"event";
}


I have also tried the following with no event trigger. all other in keystrokes are working fine.


.h
protected
bool eventFilter(QObject *obj, QEvent *event);



.cpp
bool KeyBoard::eventFilter(QObject* obj, QEvent* event)
{
qDebug()<<"event";
if(event->type() == QEvent::KeyPress) {
QKeyEvent *key = static_cast<QKeyEvent *>(event);
qDebug() << "key " << key->key() << "from" << obj;
}
return QObject::eventFilter(obj, event);
}


I have been struggling with this for a couple days now.

d_stranz
27th June 2018, 17:53
You don't tell us anything about what kind of class "Keyboard" is, nor do you say anything about where you have installed the event filter. My guess is you are either looking for events at too high a level (after a lower level widget has eaten the event) or have installed your event filter on the wrong object.

In Qt, most widgets that capture the input focus, like buttons, line edits, etc. will eat "special" keystrokes like return, tab, and so forth, so you will never see those events at a higher level. Read the Qt Event System (https://doc.qt.io/qt-5/eventsandfilters.html) documentation, especially the sections on Event Handlers and Event FIlters.

swankster
29th June 2018, 12:39
thank you for the direction d_stranz.

I was installing my event filter incorrectly.