Longpress detect at AutoHotkey v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cdull
Posts: 38
Joined: 11 Jan 2020, 07:34

Longpress detect at AutoHotkey v2

07 Feb 2021, 08:18

I don't find out how to detect longpress at AutoHotkey v2:

The working code in ahk 1.1:

Code: Select all

F7::

   KeyWait, F7, T0.2 ; Long press
   if (Errorlevel)
   {
      msgbox, 1
      return
   }
   else ; Single press
   {
      msgbox, 2
      return
   }
   return

My try to get it running at ahk 2.0:

Code: Select all

F7::

   KeyWait "F7", "T02" ; Long press
   if (0)
   {
      msgbox 1
      return
   }
   else ; Single press
   {
      msgbox 2
      return
   }
   return
In ahk 2.0 there is no error level and i don't know how to use the return of 0 or 1. Maybe someone can help.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: Longpress detect at AutoHotkey v2

27 Feb 2021, 15:59

@cdull

Read the docs (: KeyWait docs

Return Value
Type: Integer (boolean)

This function returns 0 (false) if the function timed out or 1 (true) otherwise.

Code: Select all

F7::
{
	if (KeyWait("F7", "T0.2") ; Long press)
		msgbox(1)
	else ; Single press
		msgbox(2)
}
In v2, you must enclose multi-line hotkey definitions in curly brackets.

ErrorLevel is bad - thats why in v2 its gone. Instead, most v2 functions have a return value that you can use instead of the old v1 ErrorLevel, which is much more flexible and useful.

Cheers,
tigerlilz
-TL
cdull
Posts: 38
Joined: 11 Jan 2020, 07:34

Re: Longpress detect at AutoHotkey v2

05 Mar 2021, 03:47

Hello Tigerlily,

thank you so much. That works. But one bracket is in your code in the comments.

Here is the corrected code

Code: Select all

F7::
{
	if (KeyWait("F7", "T0.2")) ; Long press
		msgbox(1)
	else ; Single press
		msgbox(2)
}
cdull
Posts: 38
Joined: 11 Jan 2020, 07:34

Re: Longpress detect at AutoHotkey v2

05 Mar 2021, 05:28

I have now created also a long press, single press and doublepress script in one:

Code: Select all

F5::doublepress_and_longpress() ; Function for single press, double press and long press (longpress longer than 0.2 seconds, double press within 0.2 seconds)

doublepress_and_longpress()
{
   if (KeyWait("F5", "T0.2")) 
   {
      if (KeyWait("F5", "D T0.2")) ; Double press
      {
         sendinput "b"
         return
      }
      else ; Single press
      {
         sendinput "a"
         return
      }
   }
   else ; Long press
   {
      sendinput "c"
      KeyWait ("F5")
      return
   }
   return
}
User avatar
submeg
Posts: 328
Joined: 14 Apr 2017, 20:39
Contact:

Re: Longpress detect at AutoHotkey v2

14 Oct 2023, 06:45

cdull wrote:
05 Mar 2021, 05:28
I have now created also a long press, single press and doublepress script in one:

Code: Select all

F5::doublepress_and_longpress() ; Function for single press, double press and long press (longpress longer than 0.2 seconds, double press within 0.2 seconds)

doublepress_and_longpress()
{
   if (KeyWait("F5", "T0.2")) 
   {
      if (KeyWait("F5", "D T0.2")) ; Double press
      {
         sendinput "b"
         return
      }
      else ; Single press
      {
         sendinput "a"
         return
      }
   }
   else ; Long press
   {
      sendinput "c"
      KeyWait ("F5")
      return
   }
   return
}
Hey @cdull, thank you for this! I just did some testing, and I notice that the Returns are not required; when commented out, the code doesn't change. Is there a reason you added them?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
asdc
Posts: 1
Joined: 18 Mar 2024, 16:32

Re: Longpress detect at AutoHotkey v2

18 Mar 2024, 16:41

Thanks to you all for this code - I've been trying to figure it out for some time. It works great when using a keyboard, however I am struggling to get it to work using my wacom pen

I have a wacom one tablet and pen and have bound the two pen keys as "[" and "]" in the wacom software. If I single/ double/ hold press the keys on the keyboard the script works perfectly. When I do the same on the pen buttons, only the single press action works.

I can't figure it out - the key history is pretty much identical except the key up is artificial when using the pen and I'm not entirely sure of the significance of this. See screenshot below when doing a double press- the first set is using the keyboard directly, whereas the second is using the pen.

Any ideas if I'm missing something?

Thanks!
Attachments
fa1cf0e699394d35a3451f05cded71cd.png
fa1cf0e699394d35a3451f05cded71cd.png (1.62 KiB) Viewed 232 times
gregster
Posts: 9100
Joined: 30 Sep 2013, 06:48

Re: Longpress detect at AutoHotkey v2

18 Mar 2024, 16:51

@asdc, I guess your uploaded screenshot is broken. If it is supposed to show the keyhistory, why not simply copy-paste its contents ?
But uploading a valid picture (eg png format) should also work.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, estochin, Hansielein, natto_tw and 44 guests