QKeyEvent奇怪的现象
我用消息过滤器响应按键消息
bool CMainWindow::eventFilter(QObject *target, QEvent *event)
{
if(QEvent::KeyPress == event->type())
{
std::cout<<"press"<<std::endl;
return true;
}
else if(QEvent::KeyRelease == event->type())
{
std::cout<<"release"<<std::endl;
return;
}
return false;
}
// If we have a record, it means that the key is already pressed, the state is the same
// so, we have an auto-repeating key
if (rec) {
if (code < Qt::Key_Shift
[解决办法]
code > Qt::Key_ScrollLock) {
k0 = q->sendKeyEvent(widget, grab, QEvent::KeyRelease, code,
Qt::KeyboardModifier(state), rec->text, true, 0,
scancode, msg.wParam, nModifiers);
k1 = q->sendKeyEvent(widget, grab, QEvent::KeyPress, code,
Qt::KeyboardModifier(state), rec->text, true, 0,
scancode, msg.wParam, nModifiers);
}
}
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
if(QEvent::KeyPress == event->type())
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug()<<"press event" <<keyEvent->isAutoRepeat();
return true;
}
else if(QEvent::KeyRelease == event->type())
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug() <<"release event" << keyEvent->isAutoRepeat();
return true;
}
return false;
}