I know I can read single keyboard key pressure from QT C++ program using
void keyPressEvent(QKeyEvent*);
function.
But what can I do, if I want to assign an action on the two consecutive pressures of the same keyboard key, separated by no more than 100ms(or any other fixed timeout)?
Of course, I imply that some another action is assigned to a single pressure of the same key, and I have to execute some another routine if timeout expires without second pressure.
Is there any simpler solution, than creating a second thread with a timer?
I hate an idea of creation one more thread for a such paltry task.
You can use a numPress counter for the number of key pressing. Then start a singleShot right after the first key press for 500 ms. After 500 ms, you can check the number of key pressing and decide which function you should call.
void SO_Qt::keyPressEvent( QKeyEvent* key)
{
if (key->key() == Qt::Key_K)
{
numPress_++;
if (numPress_ == 1)
{
QTimer::singleShot(500, this, SLOT(keyKPressed()));
}
}
}
void SO_Qt::keyKPressed()
{
if (numPress_ == 1) {
func_1();
} else if (numPress_ == 2) {
func_2();
}
numPress_ = 0;
}
void SO_Qt::func_1()
{
QMessageBox::information(this, "1","1");
}
void SO_Qt::func_2()
{
QMessageBox::information(this, "2","2");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.