For KeepassFX I needed a way to close the UI when the UI has not been used for a certain amount of time (15 minutes by default) This turned out to be much easier then I thought as I only have one stage to keep track off:
First in the App.kt override the start method:
override fun start(stage: Stage) {
super.start(stage)
idleTimer = fixedRateTimer(name = "idle-timer", period = 1000) {
if (!FX.primaryStage.isIconified && System.currentTimeMillis() > (idleTime + 1000)) {
println("lock screen!")
}
}
stage.addEventFilter(InputEvent.ANY) {
updateTimer()
}
}
This creates a timer that will run every second and check if the idletime is older then current time minus 1
second(this is a proof of concept :) ) It will print lock screen!
if so.
It also sets a filter on any type of input (keyboard or mouse) that will update the idleTime
if an event has occurred.
The updateTimer is very simple:
private fun updateTimer() {
idleTime = System.currentTimeMillis()
}
Done.
Don’t forget to stop the Timer when you end the program:
override fun stop() {
idleTimer.cancel()
}