how to measure time between button down and button up?

Get help with using AutoHotkey and its commands and hotkeys
densch
Posts: 84
Joined: 29 May 2018, 15:10

how to measure time between button down and button up?

20 Jun 2019, 05:12

the thing is in (another) game, I wanna walk a certain distance down and then that same distance up again (cause that makes me exist and reenter the room, respawning stuff).

so since I cant measure distance I thought:
1. measure how much time the down button has to pressed (aka how long to walk down).
doing this manually, having some script measure the rough time betwenn me pressing the down arrow button and letting it go.
from the stuff here, I know that button down and button up are separate events, so that shouldnt be impossible

2. Automate the walking, aka pressing the down button the above time
pribably like
buttom down
sleep, t
button up

and do the same for upwards movement.

so yeah, i just need a thingie to count time between button events.
a timer or something that starts when I press the button down and stops when I let go of the button, msgboxing me the counted time.

any good function or something for that? :-)


Edit: Aside from that, is there some good hotkey controlled timer availlable?
you know stop wathc style? I press b, it starts from 0.
pressing b stops the stop watch.

same as above, but triggered with a hotkey instead of an event/events
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 12:30

really nobody any idea how to catch a button down or button up event?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: how to measure time between button down and button up?

20 Jun 2019, 12:49

Try this to measure how long the Up arrow button was held down:

Code: Select all

Up:: StartTime := A_TickCount
Up up:: MsgBox, % A_TickCount - StartTime " millis"
I hope that helps.
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: how to measure time between button down and button up?

20 Jun 2019, 13:14

wolf_II wrote:
20 Jun 2019, 12:49
Try this to measure how long the Up arrow button was held down:

Code: Select all

Up:: StartTime := A_TickCount
Up up:: MsgBox, % A_TickCount - StartTime " millis"
Wouldn't the first hotkey in this case fire over and over resetting the start time?

Might need to add something like this.

Code: Select all

Up:: 
	If(!Active){
		StartTime := A_TickCount
		Active:=1
	}
	return
Up up:: 
	MsgBox, % A_TickCount - StartTime " millis"
	Active:=0
	return
	
	
;or

Up:: 
	StartTime := A_TickCount
	keywait,up
	return
	
Up up:: MsgBox, % A_TickCount - StartTime " millis"
	
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 13:28

Hey I jsut wrote this code.
but it told me that Up is an invalid hotkey.


how do I refer to he up arrow key then?
and do i understand it right that the b hotkey for example triggers upon presiing down the b button? and doesnt care about if b is released?
so only the down event counts?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

start:=0
end:=0

Up ::
  start=A_TickCount
return

Up up::
  end=A_TickCount-start
  Tooltip, %end%
return


q::ExitApp
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: how to measure time between button down and button up?

20 Jun 2019, 13:32

densch wrote:
20 Jun 2019, 13:28
Hey I jsut wrote this code.
but it told me that Up is an invalid hotkey.

Code: Select all

Up ::
Remove the space between the keys name and the dbl colon

Code: Select all

Up::
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 13:34

okay, found the issue.
a single empty space behind the Up somehow rendered it unusable (who the f*ck made that convention?).

anyways the new code is:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

start:=0
end:=0

Up::
  start=%A_TickCount%
return

Up up::
  end=%A_TickCount%-%start%
  Tooltip, %end%
return


q::ExitApp
it does now do a thing. when pressing down, nothing happens (intended) when releasing, it show something like 3235346345734-3235346344163

but I wanted only the result of that calculation, not the 2 numbers with a minus in between :-/
whats wrong here?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: how to measure time between button down and button up?

20 Jun 2019, 13:48

original quest wrote:msgboxing me the counted time.
this is how you can change to tooltipping:

Code: Select all

Up:: StartTime := A_TickCount
Up up:: ToolTip, % A_TickCount - StartTime " millis"
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 13:54

Next try:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

start:=0
end:=0

Up::
  start:=A_TickCount
return

Up up::
  end:=A_TickCount-start
  Tooltip, %end%
  start:=0
  end:=0
return


q::ExitApp

I'll never understand when to use := and = and %...% and ...

needlessly complicated stuff.

anyways, it now finally shows just a single number.
yet these numbers don't meke much sense.
when pressing the key only shortly it gives me 40,,, when pressing longer it gives me 15.
when pressing in the same speed, it also sometimes give me 435 or something similar.

so it jsut throws random numbers at me.
why? :O
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: how to measure time between button down and button up?

20 Jun 2019, 13:57

densch wrote:
20 Jun 2019, 13:54
anyways, it now finally shows just a single number.
yet these numbers don't meke much sense.
when pressing the key only shortly it gives me 40,,, when pressing longer it gives me 15.
when pressing in the same speed, it also sometimes give me 435 or something similar.

so it jsut throws random numbers at me.
why? :O


Because of this:
Hellbent wrote:
20 Jun 2019, 13:14
wolf_II wrote:
20 Jun 2019, 12:49
Try this to measure how long the Up arrow button was held down:

Code: Select all

Up:: StartTime := A_TickCount
Up up:: MsgBox, % A_TickCount - StartTime " millis"
Wouldn't the first hotkey in this case fire over and over resetting the start time?

Might need to add something like this.

Code: Select all

Up:: 
	If(!Active){
		StartTime := A_TickCount
		Active:=1
	}
	return
Up up:: 
	MsgBox, % A_TickCount - StartTime " millis"
	Active:=0
	return
	
	
;or

Up:: 
	StartTime := A_TickCount
	keywait,up
	return
	
Up up:: MsgBox, % A_TickCount - StartTime " millis"
	
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 14:02

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

start:=0
end:=0

Up::
	If(!Active){
		start:=A_TickCount
		Active:=1
	}
return

Up up::
  end:=A_TickCount-start
  Tooltip, % A_TickCount - start " millis"
  Active:=0
return


q::ExitApp

seriously why the fuck would it keep firing?
the button down events hasn't been triggered when having the button pressed down.

Seriously this language sucks and is so full of flaws. -.-

at least it is finally able to do something simple like count the time between 2 events -.-

thanks for the help guys anyways.
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 14:22

sorry but I have yet another question:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


/* battle time: 53719 millis
walk time: 3922 millis
*/




a::
Send {Y down}
Send {X down}

/* the actual fight and moving stuff*/
while (!q){
;walking up
Send{Up}
Sleep, 4000
Send{Up up}
; fighting
Sleep, 55000

;walking down
Send{Down}
Sleep,4000
Send{Down up}

Sleep,100
}
return 


q::ExitApp
what I want it to do:
have y and x pressed the whole time.
never releasing them.

and while they are pressed, move upwards for the specified time , wait there (stuff happens in that time)
and move down again.
and repeat that cycle for all eternity.

well he doesnt move at all.
but the script is executed cause as I manually move around, engage fights, he runs and does auto attack 8which happens due to the pressed y and x).

also i realized that the q used for the exitapp doesnt do anything anymore.

so had to manually exit the script.


also instead of !q, i just used while true{...} which had the same result:
none. but the game seems to register pressed buttons, otherwise the y and x would be pressed down and kept either.

any idea why these things happen, so the up and down movement not happening and the exitapp thing not working depsite pressing q?

I jsut retried it with SendEvent instead of Send (which I had just remembered had worked with up arrow in another script) but not here.
Might it be that it just ain't possible to have more than 3 keys pressed/used at a time?
(not that this wouldnt work when I do it manually though)

thanks in advance
Last edited by densch on 20 Jun 2019, 14:34, edited 1 time in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: how to measure time between button down and button up?

20 Jun 2019, 14:31

/* the actual fight and moving stuff*/
this is not correct syntax, the remainder of the script is ignored!
use */ on a line of its own, same as first time where you used it.
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

20 Jun 2019, 14:55

good to know, even though that's some weird syntax.

now it finally does the job perfectly.

Thanks again to everyone! :-)
User avatar
Hellbent
Posts: 1061
Joined: 23 Sep 2017, 13:34

Re: how to measure time between button down and button up?

20 Jun 2019, 15:02

Just a little heads up.

This line here is not doing what I assume you think it's doing.

Code: Select all

while (!q)  ;<---- here you are saying that while a variable named q has no value
I think you want.

Code: Select all

While(!GetKeystate("q"))  ;<------ loop until you press q (in your case q is your exitApp hotkey so it doesn't matter, but if that were to change...)
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

21 Jun 2019, 13:48

My dear friends, I have a question bulding up on the above things:
the program works as intended.
but it would be even greater if this could work in the background as well.
so I wouldn't have to have the window at the front and could rather do other stuff.
either the window not having focus or being completely minimized.

so I thought controlsend would be a good way to go but I don't really get how to use it.

here's my code at the current stage:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


/* battle time: 53719 millis
walk time: 3922 millis
*/



a::
Send {Y down}
Send {X down}

/* the actual fight and moving stuff

*/
while true{
;walking up
Controlsend, ,{Up down},ZSNES
; Send, {Up down}
Sleep, 4100
Send, {Up up}

; fighting
Tooltip, start of fight
Sleep, 46000

; 44700 for magus, chrono and marle
;45500 for ayla, robot and lucca
Tooltip, end of fight

;walking down
Send, {Down down}
Sleep,4100
Send, {Down up}

Sleep,100
}
return


q:: ExitApp



And here is a pic:
https://ibb.co/wrzX8WS

Left side is the game, right side is what window spy shows me when window focused and cursor on the game.

what info would I need to use where in the controlsent command?
just as I did with the normal sent command, I want to use controlsend for down down, down up, etc.
how to do that?
densch
Posts: 84
Joined: 29 May 2018, 15:10

Re: how to measure time between button down and button up?

22 Jun 2019, 12:55

how do I use controlsend without knowing class_nn?
What's the syntax, can you please give me an example? (I want to press down arrow in that inactive window)

Return to “Ask For Help”

Who is online

Users browsing this forum: artur1884, Google [Bot], gwarble, linearsoulchild, mikeyww, TAC109, unknownerrors, WeedTrek and 33 guests