英文:
Detecting keyboard inactivity/idle state in JavaFX
问题
我目前正在使用JavaFX的按键监听器。我能够识别键盘输入,但我的算法需要检测键盘的不活动状态。
是否有一个键代码或其他对象可以识别不活动状态?
英文:
I'm currently working with JavaFX key listeners. I'm able to identify keyboard input but my algorithm requires detecting the inactivity on keyboard.
Is there a keycode or any other object that identifies inactivity?
答案1
得分: -1
好的,以下是翻译好的内容:
嗯,在稍微的研究之后,我找到了答案。
我们可以通过记录最后活动时间与当前时间之间的差异来检测非活动状态。
例如:
double activityTime;
if (event) {
activityTime = System.currentTimeMillis() * 1000; // *1000将毫秒转换为秒
}
if (System.currentTimeMillis() * 1000 - activityTime > 5) {
//在5秒的非活动时间后,将会触发这个代码块
//语句
}
英文:
Well, after a little research I found the answer.
We can detect the inactivity by recording the difference between the time of the last activity and the current time.
For example:
double activityTime;
if(event){
activityTime = System.currentTimeMillis()*1000; // *1000 will convert milliseconds to seconds
}
if(System.currentTimeMillis()*1000 - activityTime > 5){
//This block will be triggered after 5 seconds of inactivity
//statements
}
专注分享java语言的经验与见解,让所有开发者获益!
评论