抓住“双重”热键 [英] Catch a "double" hotkey

查看:128
本文介绍了抓住“双重”热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须抓住热键+ Ctrl + Alt + C,C (意思是,按 Ctrl + Alt + C ,仅发布 C 并再次按下它。以下是我要做的事情:

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:

import com.tulskiy.keymaster.common._
import java.awt.event._
import javax.swing.KeyStroke

class KeysCatcher {

  val provider = Provider.getCurrentProvider(true)
  val ctrlC = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK + ActionEvent.ALT_MASK)

  val listener = new HotKeyListener() {
    def onHotKey(hotKey: HotKey): Unit = {
      hotKey.keyStroke match {
        case `ctrlC` =>
          println("Ctrl+Alt+C 1 was pressed")

          val listener2 = new HotKeyListener() {
            def onHotKey(hotKey: HotKey): Unit = {
              hotKey.keyStroke match {
                case `ctrlC` => println("Ctrl+Alt+C 2 was pressed")
              }
            }
          }

          provider.register(ctrlC, listener2)
      }
    }
  }

  provider.register(ctrlC, listener)
}

我有一个想法,一旦按下 Ctrl + Alt + C ,我必须再次注册相同的热键并抓住它。因为第二次按 C 应该很快就会涉及一个计时器。但我认为我采取了错误的方式,因为它会变得非常复杂。

I had the idea that once Ctrl+Alt+C is pressed I have to register the same hotkey again and catch it. I was going to involve a timer since the second press of C should be pretty quick. But I think I took the wrong way since it would make pretty complicated.

你的想法?
P.S.那里没有窗口,它会捕获全局热键。我也尝试了很多来自互联网的东西,它没有按照我的意愿工作,所以在提供任何代码之前请先测试一下。

Your thoughts? P.S. There is no window there, it catches a global hotkey. I've also tried much stuff from the Internet and it didn't work as I wanted, so before providing me any code please test it.

虽然这个例子是Scala ,Java也可以。

Although this example is Scala, Java would be ok also.

依赖关系:

https://github.com/tulskiy/jkeymaster

//build.scala
val jkeymaster = "com.github.tulskiy" % "jkeymaster" % "1.1" 


推荐答案

您只能注册一次全局热键,但是您可以在处理程序很多次。所以基本的想法是保存你上次看到这个键的时间,如果两个在一定的延迟之间,你有双击:

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()
        }
      }
    }

如果你想要没有东西var,我想你可以用 Promise 或其他东西包装它。

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

这篇关于抓住“双重”热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆