Qt Key Pressevent Enter

戏子无情 提交于 2019-12-06 00:56:11

问题


void LoginModle::keyPressEvent(QKeyEvent *event)
{
    qDebug() << event->key() << "\t" << Qt::Key_Enter << "\t" << QKeyEvent::Enter;
    if( event->key() == Qt::Key_Enter)
        OKButtonClicked();
    else
        QDialog::keyPressEvent(event);
}

This code is very simple, class LoginModle inherits from QWidget. run this code and when I press Enter, it shows:

16777220     16777221    10

It means that my Enter in keyboard is 16777220, but in Qt, it was defined as 16777221.

My system is Elementary OS (Freya), which is based on Ubuntu 14.04.

Is there something wrong with my driver or just the program's mistake ?


回答1:


16777220(dec) = 1000004(hex), so according to this list, pressed key is "Return". Look at the Wiki - Enter key is in numeric keypad, key used by you is called in Qt "Return key".




回答2:


The Enter key referred to by Qt::Key_Enter is the Enter key on the numeric keypad. You are pressing the "Enter" key that's next to the letters on your keyboard. That's known as the Return key, and its value is represented by Qt::Key_Return, which equals 16777220.

So, in order to support both key presses, you would modify the if statement as follows:

if( (event->key() == Qt::Key_Enter) || (event->key() == Qt::Key_Return))
        OKButtonClicked();
    else
        QDialog::keyPressEvent(event);



回答3:


It is just the problem of carriage return(CR) and line feed(LF). CR is encoded as 13 (0D in hexadecimal), denoted by '\r' in C and C++; and LF is encoded as 10 (0A in hexadecimal), denoted by '\n' in C and C++. Qt::Key_Enter stands for CR, while Qt::Key_Return stands for LF. When you press Enter key on the keyboard, the program may receive LF in fact, rather than CR. You can test by the getchar() function - you press the Enter key, but it returns 10. As a result, you should read the Qt::Key_Return event, rather than the Qt::Key_Enter event.

If you really, really care about the reason, please check the history of the typewriter, because CR and LF stand for two different operations in the typewriter.



来源:https://stackoverflow.com/questions/25637171/qt-key-pressevent-enter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!