0
votes

I want to have two hotkeys in my script. Namely LWin Up and LWin+LAlt Up. I've tried to do it like that:

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

But the output depends on the order in which keys were pressed and released. The time between releasing the first and the second key also affects the result. Sometimes I get two MessageBoxes, sometimes just one and sometimes even none at all (the first line is commented out, press alt, press win, release win, release alt). How do I make it work? To be clear: I want to get only one MessageBox.

In the answer it would be great to see a script that provides full information about the hotkey pressed and the order in which the keys it consist of were pressed and released. *Only one hotkey should be triggered after releasing a hotkey+key combo.

2

2 Answers

2
votes

You can not put them altogether.
you should use something like this:

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

If what you want to do instead of msgbox is too spreaded in my code you can use the below one:

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
0
votes

TechJS's answer wasn't working exactly as I wish so here is my script. It detects the order in which the keys were pressed and released, and doesn't depend on time between keys pressed/released.

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