Homework monitor

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RicKami
Posts: 29
Joined: 08 Oct 2020, 06:55

Homework monitor

Post by RicKami » 18 Oct 2021, 11:07

Hi, my small brother claims he is not playing computer games during the time of the day he promised my parents he would do homework. I want to make a script that counts the exact amount of minutes a window has been open and keep a log (which is easy to do) but I want it to count only time within a specific time. What I mean is that I want it to count it only if the window is open within lets say 16:00 to 18:00. If the window is opened at 19:30 it wound not count. But if it would be opened at 15:45 and kept open until 18:20, the script would count and log 20 min (only from 18:00) rather than 35.
I guess I could do with a timer or loop and a series of "if A_Hour > 18" and so on, but isnt there a more accurate or less rudimental way?
Thank you!

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Homework monitor

Post by mikeyww » 18 Oct 2021, 11:58

Here are some ideas for logging, though needs some adjustments & times added.

Code: Select all

; #NoTrayIcon
DllCall("RegisterShellHookWindow", "UInt", A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "window")
Return

window(wParam, lParam) {                                 ; Action, HWND
 Static games := "notepad,EXCEL", prog := {}
 Switch wParam {
  Case 1:                                                ; Window was created
   If (A_Hour A_Min > 1800)
    Return
   WinGet, pname, ProcessName, ahk_id %lParam%
   SplitPath, pname,,,, fnBare
   If fnBare in %games%
    prog[lParam, "name"] := fnBare, prog[lParam, "start"] := A_Now, log(prog[lParam], "Opened")
  Case 2:                                                ; Window was destroyed
   If prog.HasKey(lParam)
    log(prog[lParam], "Closed"), prog.Delete(lParam)
 }
}

log(program, openClose) {
 Static logFile := A_ScriptDir "\games.log"
 FormatTime, time, % now := A_Now, yyyy/MM/dd,HH:mm
 pname := program.name
 If (openClose = "Closed") {
  elapsed := now
  elapsed -= program.start, S                            ; Elapsed time in seconds
  elapsed := "," elapsed
 }
 FileAppend, %time%`,%pname%`,%openClose%%elapsed%`n, %logFile%
}

RicKami
Posts: 29
Joined: 08 Oct 2020, 06:55

Re: Homework monitor

Post by RicKami » 19 Oct 2021, 05:07

I think this would do, looks amazing! Thank you!!!!!!!!!

Post Reply

Return to “Ask for Help (v1)”