C++ Qt:按键事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12558988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:22:24  来源:igfitidea点击:

Qt : KeyPress event

c++qt

提问by user1693462

i am starter in Qt and was implementing keypressevent. I want to handle keys in such a way that if 'A' is pressed it shoud print 'R' and press of other key i should print.

我是 Qt 的初学者,正在实施 keypressevent。我想以这样的方式处理键,如果按下“A”,它应该打印“R”并按下我应该打印的其他键。

How this type of activity can be handled in Qt...??

如何在 Qt 中处理这种类型的活动......?

回答by SingerOfTheFall

You can get the key that was pressed by using a key()function. The list of codes for the keys can be found at this doc page. So, if you want your Akey, you can either do

您可以使用key()函数获取按下的键。可以在此文档页面中找到密钥代码列表。所以,如果你想要你的A钥匙,你可以这样做

keyPressEvent( QKeyEvent * event )
{
    if( event->key() == Qt::Key_A )
    {
        // do your stuff here
    }
}

or use the key code directly:

或直接使用密钥代码:

if( event->key() == 0x41 )
{
    // do your stuff here
}