{"id":37084820,"url":"https://github.com/blep/win32_window_monitor","last_synced_at":"2026-01-14T10:24:43.941Z","repository":{"id":193455221,"uuid":"682073326","full_name":"blep/win32_window_monitor","owner":"blep","description":"Monitor/capture title and process of window global events using SetWinEventHook WIN32 API, making it easy to track the current focused window.","archived":false,"fork":false,"pushed_at":"2023-11-06T14:40:08.000Z","size":64,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-23T07:56:23.196Z","etag":null,"topics":["focus","package","python","setwineventhook","win32","windows"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blep.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-08-23T11:33:19.000Z","updated_at":"2025-03-22T12:04:54.000Z","dependencies_parsed_at":"2023-09-08T08:58:25.984Z","dependency_job_id":"4b52eaed-40f3-4a5c-8aee-6082ce1c606d","html_url":"https://github.com/blep/win32_window_monitor","commit_stats":null,"previous_names":["blep/win32_window_monitor"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/blep/win32_window_monitor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blep%2Fwin32_window_monitor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blep%2Fwin32_window_monitor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blep%2Fwin32_window_monitor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blep%2Fwin32_window_monitor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blep","download_url":"https://codeload.github.com/blep/win32_window_monitor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blep%2Fwin32_window_monitor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28417063,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T10:18:03.274Z","status":"ssl_error","status_checked_at":"2026-01-14T10:16:11.865Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["focus","package","python","setwineventhook","win32","windows"],"created_at":"2026-01-14T10:24:43.347Z","updated_at":"2026-01-14T10:24:43.935Z","avatar_url":"https://github.com/blep.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"win32-window-monitor\n====================\n\nMonitor global window `events \u003chttps://learn.microsoft.com/en-us/windows/win32/winauto/event-constants\u003e`_\non Windows O.S. using the `SetWinEventHook \u003chttps://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook\u003e`_\nWIN32 SDK API:\n\n- Reports the event's window HWND and PID (process identifier) to a Python callback.\n\n- Provides helper functions to easily retrieve the PID and executable path\n  from the callback parameters.\n\nUse cases\n=========\n\n- Tracks the focused window and its process\n- Tracks windows that capture the mouse or keyboard input\n- Tracks which process is causing your fullscreen game to lose focus (Kevin Turner's initial motivation for the gist)\n- ... (there are lots of UI Automation related events that could be useful)\n\nSince the standard HWND and PID are readily available, you can utilize\nexisting Python modules to interact with either the window or the process.\n\n\nlog_focused_window script\n=========================\n\n``win32_window_monitor/main.py`` shows how to use the API to produce the example output below. After\ninstalling the ``win32-window-monitor`` package, the script ``log_focused_window`` is installed (in\n``venv\\Scripts\\log_focused_window.exe``, which is added to the ``PATH`` when activating the venv).\n\n\n.. code-block:: batch\n\n   pip install win32-window-monitor\n   log_focused_window\n\nwhich produces the following output:\n\n.. code-block:: text\n\n   101307546:0.00  Capture         W:0x1014c       P:7440          T:7592          Windows\\explorer.exe    Running applications\n   101307687:0.14  Foreground      W:0x903ce       P:2508          T:1988          net\\firefox.exe blep/win32_window_monitor: Monitor change of the focused window on Windows O.S.. Reports the focused window HWND, pid and executable to a Python callback. — Mozilla Firefox\n   101307687:0.00  Focus           W:0x903ce       P:2508          T:1988          net\\firefox.exe blep/win32_window_monitor: Monitor change of the focused window on Windows O.S.. Reports the focused window HWND, pid and executable to a Python callback. — Mozilla Firefox\n   101312015:4.33  Show            W:0xc080c       P:7440          T:7592          Windows\\explorer.exe\n   101312015:0.00  Show            W:0x10150       P:7440          T:7592          Windows\\explorer.exe\n   101312031:0.02  Show            W:0x1065e       P:12428         T:11660         Notion\\Notion.exe       Chrome Legacy Window\n   101312312:0.28  Capture         W:0x1014c       P:7440          T:7592          Windows\\explorer.exe    Running applications\n   101312468:0.16  Foreground      W:0x20642       P:12428         T:11660         Notion\\Notion.exe       Python: automated CI/Release/doc/Wheel\n   101312484:0.02  Focus           W:0x20642       P:12428         T:11660         Notion\\Notion.exe       Python: automated CI/Release/doc/Wheel\n   101312484:0.00  Show            W:0x20642       P:12428         T:11660         Notion\\Notion.exe       Python: automated CI/Release/doc/Wheel\n   101314312:1.83  Capture         W:0x1014c       P:7440          T:7592          Windows\\explorer.exe    Running applications\n   101314421:0.11  Foreground      W:0x440820      P:16088         T:7192          System32\\conhost.exe    C:\\Windows\\System32\\cmd.exe - python  -m win32_window_monitor.main\n   101314437:0.02  Focus           W:0x440820      P:16088         T:7192          System32\\conhost.exe    C:\\Windows\\System32\\cmd.exe - python  -m win32_window_monitor.main\n\nColumns content:\n\n- event time_ms : elapsed seconds since last event\n- event\n- W: HWND, the window handle\n- P: process id\n- T: thread id\n- short process path\n- window title\n\nActions made to produce those events:\n\n- Bring Firefox window to focus by clicking on it in the Taskbar. Events with\n  ``explorer.exe`` are interactions with the Taskbar.\n- Bring Notion app to focus by clicking on it in the Taskbar.\n- Bring back cmd.exe to focus by clicking on it in the Taskbar.\n\n\nUsage example\n=============\n\n**IMPORTANT**: To track the current foreground window, you need at least ``HookEvent.SYSTEM_FOREGROUND``\nand ``HookEvent.SYSTEM_MINIMIZEEND`` (``HookEvent.SYSTEM_FOREGROUND`` is not sent when\nrestoring a minimized window).\n\n\n.. code-block:: python\n\n    # win32_window_monitor/main_usage_example.py\n    from win32_window_monitor import *\n    from ctypes import wintypes\n\n\n    def on_event(win_event_hook_handle: HWINEVENTHOOK, event_id: int, hwnd: wintypes.HWND,\n                 id_object: wintypes.LONG, id_child: wintypes.LONG,\n                 event_thread_id: wintypes.DWORD,\n                 event_time_ms: wintypes.DWORD):\n        # Called from the thread running the Windows message loop.\n        # HookEvent provides a nice str, and unlike enum.Enum accepts any integer value.\n        event_id = HookEvent(event_id)\n        title = get_window_title(hwnd)\n        process_id = get_hwnd_process_id(event_thread_id, hwnd)\n        exe_path = get_process_filename(process_id) if process_id else '?'\n        print(f'{event_time_ms} {event_id} P{process_id} {exe_path} {title}')\n\n\n    def main():\n        # - init_com(): Initialize Windows COM (CoInitialize)\n        # - post_quit_message_on_break_signal: Signal handlers to exit the\n        # application when CTRL+C or CTRL+Break are pressed.\n        with init_com(), post_quit_message_on_break_signal():\n            # We must keep the event_hook_handle alive. Failure to do that may\n            # cause a crash as the \"trampoline\" function generated by ctypes for\n            # the event hook would be deleted.\n            event_hook_handle = set_win_event_hook(on_event, HookEvent.SYSTEM_FOREGROUND)\n\n            # Run the Windows message loop until the WM_QUIT message is received\n            # (sent by signal handlers above). If you have a graphic UI (TkInter, Qt...), it is\n            # likely that your application already has a Windows message loop that\n            # should be used instead.\n            run_message_loop()\n\n            unhook_win_event(event_hook_handle)\n\n\n    if __name__ == '__main__':\n        main()\n\n\nAcknowledgments\n===============\n\nThis project core is heavily based on the work of others:\n\n- Kevin Turner: `GIST \u003chttps://gist.github.com/keturn/6695625\u003e`_ providing an excellent starting base for this project.\n- Eric Blade: `getting process name from window handle \u003chttps://mail.python.org/pipermail/python-win32/2009-July/009381.html\u003e`_\n- David Heffernan: `using WIN32 SetWinEventHook with ctypes \u003chttps://stackoverflow.com/questions/15849564/how-to-use-winapi-setwineventhook-in-python/15898768#15898768\u003e`_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblep%2Fwin32_window_monitor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblep%2Fwin32_window_monitor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblep%2Fwin32_window_monitor/lists"}