View Full Version : Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F
ShapeShiftme
26th May 2011, 14:05
Good DAy im trying to catch CTRL + F Key in my code and i can cath if f is pressed by doing the following
void MainWindow::keyPressEvent(QKeyEvent *e)
{
if ( (e->key() == Qt::Key_F) ) //&& ( e->state() & ControlButton )
{
qDebug()<<" Correct Key";
}
else
{
qDebug()<<"Not Correct Key";
}
}
However I need to kknow if ctrl is pressed as well. Could someone please help me with this regard
FelixB
26th May 2011, 14:09
try
QApplication::keyboardModifiers() & Qt::ControlModifier
MarekR22
27th May 2011, 10:16
just check documentation of QKeyEvent.
ShapeShiftme
27th May 2011, 17:33
try
QApplication::keyboardModifiers() & Qt::ControlModifier
Thank you that worked great and for anyone that wants to know where to insert the code do the following
void frm_web::keyPressEvent(QKeyEvent *e)
{
if ( (e->key() == Qt::Key_F) && QApplication::keyboardModifiers() && Qt::ControlModifier)
{
qDebug()<<" Correct Key";
//connect my signal and slot
}
else
{
qDebug()<<"Not Correct Key";
}
}
Thanks Again
tiho_d
4th November 2013, 00:44
Your line number 3) has a bug. You Control key check must be a field manipulation operator(&) and not (&&). The correct line of code would be:
if ((e->key() == Qt::Key_F) && (QApplication::keyboardModifiers() & Qt::ControlModifier))
Another way to do the same for people who are not that familiar with field manipulation functions is the following line identical to the line above:
if ((e->key() == Qt::Key_F) && (QApplication::keyboardModifiers().testFlag(Qt::Co ntrolModifier)))
I would also recommend using the modifiers coming from the key event itself and not the global app keyboard modifiers. i.e. the best way to write what you need is:
if ((e->key() == Qt::Key_F) && (e->modifiers().testFlag(Qt::ControlModifier)))
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.