Here are the event method:
//**************************************************************************
void MainWindow
::keyPressEvent(QKeyEvent* event
) //**************************************************************************
{
qDebug() << Q_FUNC_INFO;
Qt::Key key;
int keyInt(0);
if (event
->type
() == QEvent::KeyPress) {
keyEvent = static_cast<QKeyEvent*>(event);
keyInt = keyEvent->key();
key = static_cast<Qt::Key>(keyInt);
if(key == Qt::Key_unknown){
qDebug() << "Unknown key from a macro probably";
return;
}
}
// the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if(key == Qt::Key_Control ||
key == Qt::Key_Shift ||
key == Qt::Key_Alt ||
key == Qt::Key_Meta ||
key == Qt::Key_Return ||
key == Qt::Key_Enter )
{
qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta or Enter/Return";
return;
}
// check for a combination of user clicks
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
QString keyText
= keyEvent
->text
();
// if the keyText is empty than it's a special key like F1, F5, ...
qDebug() << "Pressed Key:" << keyText;
if(modifiers & Qt::ShiftModifier)
keyInt += Qt::SHIFT;
if(modifiers & Qt::ControlModifier)
keyInt += Qt::CTRL;
if(modifiers & Qt::AltModifier)
keyInt += Qt::ALT;
if(modifiers & Qt::MetaModifier)
keyInt += Qt::META;
}
//**************************************************************************
void MainWindow::keyPressEvent(QKeyEvent* event)
//**************************************************************************
{
qDebug() << Q_FUNC_INFO;
Qt::Key key;
int keyInt(0);
QKeyEvent *keyEvent;
if (event->type() == QEvent::KeyPress)
{
keyEvent = static_cast<QKeyEvent*>(event);
keyInt = keyEvent->key();
key = static_cast<Qt::Key>(keyInt);
if(key == Qt::Key_unknown){
qDebug() << "Unknown key from a macro probably";
return;
}
}
// the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
if(key == Qt::Key_Control ||
key == Qt::Key_Shift ||
key == Qt::Key_Alt ||
key == Qt::Key_Meta ||
key == Qt::Key_Return ||
key == Qt::Key_Enter )
{
qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta or Enter/Return";
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
return;
}
// check for a combination of user clicks
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
QString keyText = keyEvent->text();
// if the keyText is empty than it's a special key like F1, F5, ...
qDebug() << "Pressed Key:" << keyText;
if(modifiers & Qt::ShiftModifier)
keyInt += Qt::SHIFT;
if(modifiers & Qt::ControlModifier)
keyInt += Qt::CTRL;
if(modifiers & Qt::AltModifier)
keyInt += Qt::ALT;
if(modifiers & Qt::MetaModifier)
keyInt += Qt::META;
qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText);
}
To copy to clipboard, switch view to plain text mode
Bookmarks