在JavaFX中检测键盘不活动/空闲状态

huangapple 未分类评论44阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2020年4月6日 16:17:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/61055570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定