AutoHotkey的。如何使“热键”和“热键+键”工作?

时间:2015-09-12 15:29:51

标签: autohotkey

我想在我的脚本中有两个热键。即LWin UpLWin+LAlt Up。我试过这样做:

LAlt & LWin Up:: ;I've also tried commenting out the first
LWin & LAlt Up:: ;or the second line
LWin Up::
  msgbox, % A_ThisHotkey
return

但输出取决于按下和释放按键的顺序。释放第一个和第二个键之间的时间也会影响结果。有时我得到两个MessageBoxes,有时只有一个,有时甚至根本没有(第一行被注释掉,按alt,按下win,释放win,释放alt)。我如何使其工作?要明确:我只想获得一个MessageBox。

在答案中,很高兴看到一个脚本提供有关按下的热键的完整信息以及按下和释放按键的顺序。 *释放a hotkey+key组合后,只能触发一个热键。

2 个答案:

答案 0 :(得分:2)

你不能完全放弃它们 你应该使用这样的东西:

altPressed := false

LAlt & LWin Up::
    msgbox, % A_ThisHotkey
return

LWin & LAlt Up::
    msgbox, % A_ThisHotkey
    altPressed := true
return

LWin Up::
    if !altPressed {
        msgbox, % A_ThisHotkey
    }
    altPressed := false
return

如果您想要执行的操作而不是msgbox在我的代码中过于分散,您可以使用下面的代码:

SetTimer, toDo, 10

toDo:
    if doWhatEver {
        ;; HERE DESCRIBE WHAT TO DO
        doWhatEver := false
    }
return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

altPressed := false
doWhatEver := false

LAlt & LWin Up::
    doWhatEver := true
return

LWin & LAlt Up::
    doWhatEver := true
    altPressed := true
return

LWin Up::
    if !altPressed {
        doWhatEver := true
    }
    altPressed := false
return

答案 1 :(得分:0)

TechJS的答案并没有完全符合我的意愿,所以这是我的剧本。它可以检测按下和释放按键的顺序,并且不依赖于按下/释放按键之间的时间。

global firstPressed := ""

LAlt::
  altDown := true
  if (winDown)
    return
  firstPressed := "!"
return

LWin::
  winDown := true
  if (altDown)
    return
  firstPressed := "#"
return

LAlt Up::
  altDown := false
  if (!winDown) {
    if (comboJustUp) {
      comboJustUp := false
      return
    }
    msgbox !
  }
  if (winDown) {
    comboJustUp := true
    if (firstPressed = "#") 
      msgbox #!!.
    if (firstPressed = "!")
      msgbox !#!.
  }
return

LWin Up::
  winDown := false
  if (!altDown) {
    if (comboJustUp) {
      comboJustUp := false
      return
    }
    msgbox #
  }
  if (altDown) {
    comboJustUp := true
    if (firstPressed = "!") ; \here is one bug though. If you switch theese
      msgbox !##.           ; /two lines
    if (firstPressed = "#") ; \with theese
      msgbox #!#.           ; /two. It won't work correctly for some reason
  }
return
相关问题