Jump to content
Sign in to follow this  

Windows 7 - Record Window Events

Recommended Posts



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

    ;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
        if FileGetSize($latest_file_name) == 0 Then
        $timer = TimerInit()
        $latest_file_name = $log_dir & @YEAR & @MON & @MDAY & "_" & @HOUR & @MIN & @SEC & "_window_log.txt"
        $file = FileOpen($latest_file_name, 1)

    ;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)

    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
                ;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
        if $is_complete_2 == True Then ExitLoop


Edited by ame1011

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]

Share this post

Link to post
Share on other sites

I don't know about title changed, but=


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

Maybe useful?

Share this post

Link to post
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]

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.