Catch a “double” hotkey

前端 未结 1 345
一生所求
一生所求 2021-01-21 13:10

I have to catch a hotkey of Ctrl+Alt+C, C (meaning, press Ctrl+Alt+C, release only C and press it again). Here is what I\'m trying to do:<

相关标签:
1条回答
  • 2021-01-21 14:04

    You can register a global hotkey only once, but you can receive its events in the handler many times. So the basic idea is to save the last time your saw this key and if two are coming between certain delay, you have a double click:

        var last = 0l
        val listener = new HotKeyListener() {
          def onHotKey(hotKey: HotKey): Unit = {
            hotKey.keyStroke match {
              case `ctrlC` =>
                if (System.currentTimeMillis() - last < 700) // arbitrary delay of 700 ms
                  println("We have a double click!")
                else last = System.currentTimeMillis()
            }
          }
        }
    

    if you want something without var, I guess you can wrap it in a Promise or something.

    0 讨论(0)
提交回复
热议问题