问题
This question might be related to those questions:
- QEventLoop: get time when an event was scheduled
- Get the timestamp when a QInputEvent was put onto the queue of the QEventLoop in Qt 4.8
I would like to detect if a user input is older than a certain timeout and block such events if necessary. Imagine scenarios where the UI is not responsive and hangs in the main thread for a few seconds and the user keeps sending input events. Those events are queued and are then processed with a certain delay. I would like to catch this and be able to discard/ignore them. I know that this should never happen since a UI should always be responsive and long tasks should be moved to worker threads or similar concepts but I still want to be able to detect such rare scenarios. Note that the same could happen when the system itself freezes for a few seconds, maybe due to high burden on the system as a whole.
回答1:
I took a look into the code of the default implementations of the available input drivers in Qt for embedded and decided to write my own input concept to be able to read the timestamps from the Linux input files. I then used those timestamps to compare them with the current time to drop them if they were queued too long ago.
The code needed is very platform specific but it worked for my scenario. I guess that I have to accept my own answer although I had hoped for a more general solution.
来源:https://stackoverflow.com/questions/52817148/how-to-block-ignore-discard-user-input-events-that-are-older-than-a-certain-time