简体   繁体   English

QT双击键盘事件

[英]QT double click keyboard event

I know I can read single keyboard key pressure from QT C++ program using 我知道我可以使用QT C ++程序读取单键盘键压力

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)? 但是,如果我想在相同键盘键的两个连续压力上分配一个动作,相隔不超过100毫秒(或任何其他固定超时),我该怎么办?

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. 您可以使用numPress计数器输入按键次数。 Then start a singleShot right after the first key press for 500 ms. 然后在第一次按下500 ms后立即启动singleShot。 After 500 ms, you can check the number of key pressing and decide which function you should call. 500毫秒后,您可以检查按键次数并确定应拨打的功能。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM