UPDATE: Fixed typo, to fix infinite loop.
mainwindow.h
#include <QMainWindow>
#include <QKeyEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget * parent = 0);
~MainWindow();
public slots:
void keyPressEvent(QKeyEvent*);
void keyReleaseEvent(QKeyEvent*);
//...
}
mainwindow.cpp
#include <QDebug>
#include "mainwindow.h"
void MainWindow::keyPressEvent(QKeyEvent* ke)
{
qDebug() << Q_FUNC_INFO;
qDebug() << "mw" << ke->key() << "down";
QMainWindow::keyPressEvent(ke); // base class implementation
}
void MainWindow::keyReleaseEvent(QKeyEvent* ke)
{
qDebug() << Q_FUNC_INFO;
qDebug() << "mw" << ke->key() << "up";
if(ke->key() == Qt::Key_Back)// necessary for Q_OS_ANDROID
{
// Some code that handles the key press I want to use
// Note: default handling of the key is skipped
ke->accept();
return;
}
else
{
// allow for the default handling of the key
QMainWindow::keyReleaseEvent(ke); // aka the base class implementation
}
}
Hope that helps.