AHK Gui - measuring the duration of a button press.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

AHK Gui - measuring the duration of a button press.

29 Jun 2023, 18:56

I want to know how long a button on a gui is pressed. I tried code like this:

Code: Select all

Gui, add, button, w100 h50 default gpush, Push Me
Gui, show

return

Push()
{
	enow := A_tickcount
		
	keywait lbutton
	keywait enter

	msgbox, 4096, Duration,% A_tickcount - enow
	exitapp
}
This works when enter is pressed but not for the click on the button itself since the time only starts getting measured after the button is released. How can I measure the length of the button press? Thanks.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: AHK Gui - measuring the duration of a button press.

29 Jun 2023, 20:12

I've found if I use text or a picture control instead of a button my code will work. It would still be useful to know how to measure the length of a press on a normal button.
User avatar
mikeyww
Posts: 27373
Joined: 09 Sep 2014, 18:38

Re: AHK Gui - measuring the duration of a button press.

29 Jun 2023, 20:13

Code: Select all

; This script determines the duration of activating (holding) a GUI button
#Requires AutoHotkey v1.1.33
Global start
WM_KEYDOWN     := 0x0100
WM_KEYUP       := 0x0101
WM_LBUTTONDOWN := 0x0201
WM_LBUTTONUP   := 0x0202
Gui Font, s10
Gui Add, Button, w230 Default, OK
Gui Show,, Test
OnMessage(WM_KEYDOWN    , "WM_KEYDOWN")
OnMessage(WM_KEYUP      , "WM_KEYUP")
OnMessage(WM_LBUTTONDOWN, "WM_KEYDOWN")
OnMessage(WM_LBUTTONUP  , "WM_KEYUP")
Return

WM_KEYDOWN(wParam, lParam) {
 keyName := GetKeyName("VK" Format("{:x}", wParam))
 If (keyName = "LButton") {
  MouseGetPos,,,, ctl
  If !Instr(ctl, "Button")
   Return
 }
 If !start && keyName ~= "i)^(Space|LButton|Enter)$" {
  start := A_TickCount
  SoundBeep 1500
 }
}

WM_KEYUP(wParam, lParam) {
 If start {
  elapsed := A_TickCount - start
  start   := 0
  MsgBox 64, Elapsed time, % elapsed " ms"
 }
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: effel, pond_pop and 127 guests