当前位置:网站首页>QT keyboard event (II) -- long press the key to trigger the event event repeatedly, and the problem is solved
QT keyboard event (II) -- long press the key to trigger the event event repeatedly, and the problem is solved
2022-07-24 16:25:00 【User 6557940】
introduction
Qt Keyboard events may encounter unresponsive arrow keys 、 There are problems such as pressing or releasing events all the time , How to solve it ?Jungle The notes will answer for you .
01
Problem description
stay Jungle In my last article (Qt Keyboard events ( One )—— Detect key input ),Jungle Simple realization of utilization qt Detect the user's key operation and press the key \ The release operation is printed on Qt A small program on the interface . But there will be a phenomenon during the test , That is to press and hold a key , The interface has been refreshing press、release、press、release……( Here's the picture )
in other words , Long press a key without releasing ,keyPressEvent and keyReleaseEvent Events will continue to be triggered . Even though it is Qt Design and implement a good event mechanism , But in terms of user experience , This is unreasonable . What we want to achieve is : Press a key ( such as Tab key ), The interface only prints once "Key_Tab Press"; When the hand is released , Only print on the interface "Key_Tab Release".
02
Problem specification
So ,Jungle The query Qt Official documents and several blogs . Official documents mention a QKeyEvent Member function of isAutoRepeat:
You can see , When event From auto-repeating key,isAutoRepeat return true; When event The event comes from the initial key , be sAutoRepeat return false. That may not be easy to understand , Then you might as well Jungle Do a little test :
stay keyPressEvent Internal printing isAutoRepeat Return value
operation : Long press Tab key , stay keyPressEvent Internal printing isAutoRepeat Return value , Release Tab Post key , Press and hold again Tab key , Let go again .
void QKeyBoard::keyPressEvent(QKeyEvent *event){
switch(event->key()){
case Qt::Key_Tab:
if(event->isAutoRepeat()==true){
this->ui.textEdit_press->append("true");
}
else{
this->ui.textEdit_press->append("false");
}
this->ui.textEdit_press->append("Key_Tab Press");
break;
/*default:
this->ui.textEdit->append("KeyEvent");*/
}
}The test results are as follows :
As you can see from the test results , Long press Tab The key is triggered for the first time keyPressEvent When an event is isAutoRepeat return false, Then long press isAutoRepeat The return values are true. Press and hold again after releasing the key ,isAutoRepeat return false, Then long press isAutoRepeat The return values are true. That is, only the first press Tab Key time ,isAutoRepeat The return value is false. Combined with this result Qt Description of official documents , It seems to be better understood .
stay keyReleaseEvent Internal printing isAutoRepeat Return value
Again ,Jungle stay keyReleaseEvent Internal printing isAutoRepeat Return value , The operation results are shown in the figure above ( The code is slightly ). You can see , Long press Tab key , Automatic triggering keyReleaseEvent When an event is isAutoRepeat return true, Trigger after releasing the key keyReleaseEvent When an event is isAutoRepeat return true.
Test summary
combination Qt Official documents and the above tests , It can be concluded as follows :
- Key triggered keyPressEvent event ,isAutoRepeat return false; Automatically triggered keyPressEvent event ,isAutoRepeat return true;
- Loose key triggered keyReleaseEvent event ,isAutoRepeat return true; Automatically triggered keyReleaseEvent event ,isAutoRepeat return false.
03
Problem solving
When the real key press and key release events are triggered , Plus right isAutoRepeat Judgment of return value , Specific judgments are as follows 2 Section summary , The sample code is as follows :
void QKeyBoard::keyPressEvent(QKeyEvent *event){
switch(event->key()){
case Qt::Key_Tab:
if(!event->isAutoRepeat()){
this->ui.textEdit_press->append("Key_Tab Press");
/* add your code here*/
}
break;
/*default:
this->ui.textEdit->append("KeyEvent");*/
}
}
void QKeyBoard::keyReleaseEvent(QKeyEvent *event){
switch(event->key()){
case Qt::Key_Tab:
if(!event->isAutoRepeat()){
this->ui.textEdit_release->append("Key_Tab Release");
/* add your code here*/
}
break;
/*default:
this->ui.textEdit->append("KeyEvent");*/
}
}In some blogs , The author may have added a variable to mark whether the key is pressed , And update the mark when pressing and releasing the key . but Jungle I don't think it's necessary , Just add judgment as above . Please visit Jungle Of GitHub Home page :https://github.com/FengJungle/Qt_Project
边栏推荐
- Software recommendation - office software
- 如何在 PHP 中防止 XSS
- Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
- 多线程(基础)
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
- Enter a URL to this page to show it. What happened in this process?
- JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
- ARP 入门
- MySQL converts strings to numeric types and sorts them
- Intel plans to sell baseband chip business, but Apple has given up?
猜你喜欢

Talk about C pointer

253 Conference Room II

矩阵的秩和图像的秩的一些了解

Who is the "roll" king of the prefabricated vegetable track?

Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st

【南京农业大学】考研初试复试资料分享

Creation and inheritance of JS class

Introduction to bermudagrass

Leetcode 223. rectangular area

124 maximum path sum in binary tree
随机推荐
About SQL data query statements
Enter a URL to this page to show it. What happened in this process?
15. ARM embedded system: how to debug single board with PC
Jia Yueting's Faraday will receive another financing of US $225million in the future, and ff91 will be mass produced soon!
Multithreading (basic)
随笔记:同步、异步和微任务、宏任务的打印顺序
OpenMP入门
简单工厂模式都不会,你真应该去工厂搬砖!
文件浏览器?Qt也可以实现!
Educational codeforces round 100 (rated for Div. 2) B. find the array solution
【LeetCode】Day103-搜索二维矩阵 II
Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st
31 next spread
【LeetCode】Day102-螺旋矩阵 II
Wentai technology's revenue in the first quarter soared by 184.6% year-on-year, and its net profit soared by 256.21%!
Quickly view the version of redis in the server
Custom view - Custom button
Ligerui table control grid changes the color of rows (cells) according to the content
Machine learning notes - building a recommendation system (5) feedforward neural network for collaborative filtering
Leetcode:162. looking for peak [two points looking for peak]