Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Morse: find hotkey press-and-hold patterns


  • Please log in to reply
9 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
With different hotkey press patterns many more actions can be triggered than just with a hotkey press itself. For example, the key "a" sends "a", two short presses sends "ä", a long press sends "á", and so on. Like Morse codes for different actions.

The following Morse function determines the base key from the current hotkey, and starts monitoring it. If it is released after longer time than timeout (default to 400ms), "1" is appended to the Pattern variable. If the key is released sooner, "0" is appended.

Then the scripts waits for the key to be pressed down again. If it is not pressed within the timeout period, the 0-1 pattern of the last key presses are returned, otherwise the next key hold time will be appended to the Pattern. Usage examples are at the end of the script, for Alt-Z and BackSpace.
Morse(timeout = 400) { ;
   tout := timeout/1000
   key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
   Loop {
      t := A_TickCount
      KeyWait %key%
      Pattern .= A_TickCount-t > timeout
      KeyWait %key%,DT%tout%
      If (ErrorLevel)
         Return Pattern
   }
}

BS::MsgBox % "Morse press pattern " Morse()

!z::
   p := Morse()
   If (p = "0")
      MsgBox Short press
   Else If (p = "00")
      MsgBox Two short presses
   Else If (p = "01")
      MsgBox Short+Long press
   Else
      MsgBox Press pattern %p%
Return
Edit 20070224: Fixed key hold time, not to include key-up time

Fantastic!!!!
  • Guests
  • Last active:
  • Joined: --
Fantastic!!!!
Thank you very much!!!! :D :D :D

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Fixed in the original post the key hold time, not to include key-up time. It is more accurate now.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Superb Laszlo, thank you very much !

I tried with LButton and timout=150 which seems more natural
Posted Image

  • Guests
  • Last active:
  • Joined: --
Wow. Very useful script!