How to detect inactivity/idle time since last keyp

2019-08-21 14:23发布

I have an application with a lot of screens (followed by MVC pattern), and I want to be able to receive in a fashion way the information that last key was pressed x seconds ago (120 sec let's say). Is there standard way to do this or I have to start a timer and every time when I pressed a key I have to override a variable and in the timer I have to check the difference time between that time and current time?

标签: java-me
1条回答
Emotional °昔
2楼-- · 2019-08-21 14:49

Yes, just record the system timer when a key is pressed.

long epoch = System.currentTimeMillis();

When a key is pressed again, you need to check the time difference to see how long it's been idle for.

If you need to trigger things without keypresses, then you need to start a thread which wakes now and again to check the elapsed time, and trigger an event of some kind when the time period has elapsed.

查看更多
登录 后发表回答