Jump to content

Windows 7 - Record Window Events


Recommended Posts

Hi,

Currently I'm creating a script for my workplace that monitors window titles and saves them to a local file.

Every 10 minutes this local file is uploaded to an external server using http post.

The logic of the script is as follows:

  • Get window titles using WinList
  • If WinList titles are not in 'current_window_titles' array, add them, log an event in the file
  • If window titles array has an entry that is not in WinList, remove it from current open windows as it is probably closed

As you can see, I want to log every time a new window title appears - this happens when a new window is open or changed (i.e. a different website is visited)

My code works fine for a few hours but I notice it will eventually break and log all windows every iteration of the loop

My main question is: is there a WinAPI function somewhere that will fire an event every time a window is opened or it's title has changed?

I would think that this method would be better.

If not, I've considered changing the 'current_window_titles' array so that it keeps track of window titles that have been opened in the last minute or two. If, after 1 or 2 minutes it is no longer open, remove it from the list.

Anyone have any thoughts on this? Code below..

#include <Array.au3>
#include "WinHttp.au3"

Global $current_open_windows[1]
$current_open_windows[0] = "TEMP_WINDOW_NAME"
Global $excluded = GetExlusionList()
Global $log_dir = ''
Global $upload_frequency = 10
Global $latest_file_name = $log_dir & @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC & "_window_log.txt"
Global $file = FileOpen($latest_file_name, 1)
Global $timer = TimerInit()

while 1
    local $winList = WinList()
    local $winTitle
    local $winHandle
    local $search1
    local $search2
    local $is_complete_2
    _ArraySort($current_open_windows)

    ;if enough time has elapsed: upload old file (if size > 0), reset timer, create a new file
    if TimerDiff($timer) >= 1000*60*$upload_frequency Then
        FileClose($file)
        if FileGetSize($latest_file_name) == 0 Then
            FileDelete($latest_file_name)
        Else
            HttpPostFile()
        EndIf
        $timer = TimerInit()
        $latest_file_name = $log_dir & @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC & "_window_log.txt"
        $file = FileOpen($latest_file_name, 1)
    EndIf

    ;go through current open windows, add it to array if not in array
    For $i = 1 to $winList[0][0]
        If $winList[$i][0] <> "" AND IsVisible($winList[$i][1]) Then
            $winTitle = $winList[$i][0]
            $winHandle = $winList[$i][1]
            $search = _ArrayBinarySearch($current_open_windows, $winTitle)
            $search2 = _ArrayBinarySearch($excluded, $winTitle)
            if $search >= 0 Then
                ;do nothing since item is already in array
            Elseif $search == -1 AND $search2 == -1 Then
                _ArrayAdd($current_open_windows, $winTitle)
                FileWriteLine($file, @YEAR &"-"& @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "***" & $winTitle)
            EndIf
        EndIf
    Next

    while 1
        $is_complete_2 = True
        ;go through array, if window found that isn't in current open windows, remove it
        For $i = 0 to UBound($current_open_windows) - 1 Step 1
            $search = _ArraySearch($winList, $current_open_windows[$i])
            if $search >= 0 Then
                ;do nothing since array item is in current window list
            Else
                ;delete element and start loop again since order of array elements has changed
                ConsoleWrite($current_open_windows[$i] & " is no longer open and will be deleted" & @CRLF)
                _ArrayDelete($current_open_windows, $i)
                $is_complete_2 = False
                ExitLoop
            EndIf
        Next
        if $is_complete_2 == True Then ExitLoop
    WEnd

    sleep(50)

WEnd
Edited by ame1011
[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
Link to comment
Share on other sites

I don't know about title changed, but=

$HSHELL_WINDOWCREATED, $HSHELL_WINDOWACTIVATED, $SHELL_WINDOWDESTROYED

Notifies when some window is created, activated or destroyed. Gives window handle.

Maybe useful?

Link to comment
Share on other sites

I don't know about title changed, but=

Maybe useful?

Thanks! This is exactly what I needed.

As for title changed, that is handled by the REDRAW event.

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...