Need help displaying Timer

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Need help displaying Timer

16 Jan 2020, 15:16

I want the script to do the following:

wait for notepad to be open
set and display a timer for 5 minutes
with a hotkey to add 5 minutes to the timer and display
exit notepad and script when timer runs out

here is what I have so far it works without the display

Code: Select all

WinWaitActive, ahk_class Notepad
start := A_TickCount, maxActiveTime := 300000

SetTimer, Check, 1000
return

x::
	maxActiveTime += 300000 
return

Check:
	if (WinExist("ahk_class Notepad")) {
		if ( A_TickCount-start >= maxActiveTime )
			WinClose, ahk_class Notepad

	} else { 
		start := A_TickCount, maxActiveTime := 300000
		
	}
return

ExitApp
esc::ExitApp
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

16 Jan 2020, 16:21

Just a simple example

Code: Select all

SetTitleMatchMode 2
WinWaitActive, ahk_class Notepad
start := A_TickCount, maxActiveTime := 30000

SetTimer, Check, 1000
return

~x::
	maxActiveTime += 300000 
return

Check:
    elapsedTime := A_TickCount - start
	if (WinExist("ahk_class Notepad")) {
        ToolTip, % Ceil((maxActiveTime/1000+1) - (elapsedTime/1000))    ; display tooltip, Seconds
		if ( elapsedTime >= maxActiveTime )
			WinClose, ahk_class Notepad

	} else { 
        Tooltip ; clear tooltip
		start := A_TickCount, maxActiveTime := 300000
		
	}
return

ExitApp
esc::ExitApp
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 16:26

That works great with a tooltip

is there anyway to use Gui to display the countdown in 00:00 format
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

16 Jan 2020, 20:16

Code: Select all

SetTitleMatchMode 2

Title := "Notepad"  ; window title
maxActiveTime := 30000

Gui +AlwaysOnTop +ToolWindow
Gui Add, Text, w80 Vtimer

SetTimer Check, 1000
toggle := 1 ; control var

Check:
    if WinActive(Title) {
        if toggle {
            start := A_TickCount
            Gui Show, NoActivate
            toggle := 0
        }
        elapsedTime := A_TickCount - start
        if ( elapsedTime >= maxActiveTime ) {
            WinClose Notepad
            Gui Hide
        } else {
            GuiControl, Text, timer, % ms2hms(elapsedTime)
        }
    } else {
        toggle := 1
        Gui Hide
    }
    return

; Function By TheIrishThug
; From https://autohotkey.com/board/topic/29053-how-to-convert-milliseconds-to-hourminutesecond/
ms2hms(milli, ByRef hours=0, ByRef mins=0, ByRef secs=0, secPercision=0)
{
    SetFormat, FLOAT, 0.%secPercision%
    milli /= 1000.0
    secs := mod(milli, 60)
    SetFormat, FLOAT, 0.0
    milli //= 60
    mins := mod(milli, 60)
    hours := milli //60
    return hours . ":" . mins . ":" . secs
}
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 21:04

You are awesome, this is working.

But its counting up, is there a way to count down to zero?
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

16 Jan 2020, 21:17

Modify this line.

Code: Select all

GuiControl, Text, timer, % ms2hms(maxActiveTime-elapsedTime)
or this, Count to 0

Code: Select all

GuiControl, Text, timer, % ms2hms(maxActiveTime-1000-elapsedTime)
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 21:51

Thank you so much. it works
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 22:29

is there a way to adapt this to the code you made

Code: Select all

displayedTime := Format2Digits(h) ":" Format2Digits(m) ":" Format2Digits(s)

Format2Digits(_val)
{
	_val += 100
	StringRight _val, _val, 2
	Return _val
}
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 22:45

I'm trying to always have 2 digits like 00:00:00
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

16 Jan 2020, 22:54

Modify the return value of the ms2hms function.

Code: Select all

return Format("{:02}",hours) . ":" . Format("{:02}",mins) . ":" . Format("{:02}",secs)
about Format() Look this

If width is prefixed by 0, leading zeros are added until the minimum width is reached. For example, Format("{:010}", 1) returns 0000000001. If both 0 and - appear, the 0 is ignored. If 0 is specified as an integer format (i, u, x, X, o, d) and a precision specification is also present - for example, {:04.d} - the 0 is ignored.

If omitted, no padding occurs.
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 23:15

thank you again,
I really appreciate it.
I Learn so much when I come on this forum, a great community with people willing to help.
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

16 Jan 2020, 23:20

Code: Select all

return (Format("{:02}",hours) ? Format("{:02}",hours) ":" : "") Format("{:02}",mins) . ":" . Format("{:02}",secs)
With your help, I was able to figure out how to hide the hours until hours are added.
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

16 Jan 2020, 23:35

yeah, looks good! :P :bravo:
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

16 Jan 2020, 23:45

You can simplify it because if the value is insufficient, minute and hour variables are flase.

Code: Select all

return (hours ? Format("{:02}",hours) . ":" : "") . (mins ? Format("{:02}",mins) . ":" : "") . Format("{:02}",secs)
societyab
Posts: 11
Joined: 16 Jan 2020, 15:08

Re: Need help displaying Timer

17 Jan 2020, 02:00

I ended up using

Code: Select all

return (hours ? hours ":" : "") . mins . ":" . Format("{:02}",secs)
Because It will mostly be used under an hour so it displays as 0:00 unless there is an hour then it looks weird like 1 hour 4 minutes looks like 1:4:00
it there a way to display Format("{:02}" for mins only when there are hours?
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: Need help displaying Timer

17 Jan 2020, 02:19

you mean?

Code: Select all

return (hours ? hours . ":" : "") . (hours ? Format("{:02}",mins) . ":" : mins ? mins . ":" : "") . Format("{:02}",secs)
or this, same effect

Code: Select all

return (hours ? hours . ":" : "") . (mins ? hours ? Format("{:02}",mins) . ":" : mins . ":" : "") . Format("{:02}",secs)
Last edited by telanx on 17 Jan 2020, 02:35, edited 1 time in total.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Google [Bot] and 77 guests