From 68ea4a3796fc3b61526af7e202874a2d9a2628d4 Mon Sep 17 00:00:00 2001 From: Maifee Ul Asad Date: Thu, 8 Sep 2022 15:01:50 +0600 Subject: [PATCH] window focus detection//wip --- FocusLogger/FocusLogger/FocusDetection.cs | 83 ++++++++++++++++++++++ FocusLogger/FocusLogger/FocusLogger.csproj | 1 + FocusLogger/FocusLogger/Service1.cs | 33 ++++++--- 3 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 FocusLogger/FocusLogger/FocusDetection.cs diff --git a/FocusLogger/FocusLogger/FocusDetection.cs b/FocusLogger/FocusLogger/FocusDetection.cs new file mode 100644 index 0000000..58cba1a --- /dev/null +++ b/FocusLogger/FocusLogger/FocusDetection.cs @@ -0,0 +1,83 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace FocusLogger +{ + class FocusDetection + { + delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, + IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); + + [DllImport("user32.dll")] + static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr + hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, + uint idThread, uint dwFlags); + + [DllImport("user32.dll")] + static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + + + [DllImport("user32.dll")] + static extern bool UnhookWinEvent(IntPtr hWinEventHook); + + + + const uint EVENT_SYSTEM_FOREGROUND = 3; + const uint WINEVENT_OUTOFCONTEXT = 0; + + + static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc); + + public FocusDetection() + { + IntPtr hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, + procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); + + File.Create("log.txt"); + + + // Application.Run(); + + UnhookWinEvent(hhook); + } + + static void WinEventProc(IntPtr hWinEventHook, uint eventType, + IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) + { + Console.WriteLine("Foreground changed to {0:x8}", hwnd.ToInt32()); + //Console.WriteLine("ObjectID changed to {0:x8}", idObject); + //Console.WriteLine("ChildID changed to {0:x8}", idChild); + GetForegroundProcessName(); + + } + static void GetForegroundProcessName() + { + IntPtr hwnd = GetForegroundWindow(); + + if (hwnd == null) + return; + + uint pid; + GetWindowThreadProcessId(hwnd, out pid); + + foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) + { + if (p.Id == pid) + { + Console.WriteLine("Pid is: {0}", pid); + Console.WriteLine("Process name is {0}", p.ProcessName); + File.AppendAllText("log.txt", p.ProcessName + Environment.NewLine); + + return; + } + } + + Console.WriteLine("null"); + } + + } +} diff --git a/FocusLogger/FocusLogger/FocusLogger.csproj b/FocusLogger/FocusLogger/FocusLogger.csproj index fabcbc1..4836ccf 100644 --- a/FocusLogger/FocusLogger/FocusLogger.csproj +++ b/FocusLogger/FocusLogger/FocusLogger.csproj @@ -44,6 +44,7 @@ + Component diff --git a/FocusLogger/FocusLogger/Service1.cs b/FocusLogger/FocusLogger/Service1.cs index 863508a..08e90be 100644 --- a/FocusLogger/FocusLogger/Service1.cs +++ b/FocusLogger/FocusLogger/Service1.cs @@ -1,17 +1,13 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Diagnostics; -using System.Linq; -using System.ServiceProcess; -using System.Text; -using System.Threading.Tasks; +using System.ServiceProcess; +using System.Threading; namespace FocusLogger { public partial class Service1 : ServiceBase { + Thread Worker; + AutoResetEvent StopRequest = new AutoResetEvent(false); + public Service1() { InitializeComponent(); @@ -19,15 +15,34 @@ public Service1() protected override void OnStart(string[] args) { + + // Start the worker thread + Worker = new Thread(DoWork); + Worker.Start(); + + } protected override void OnStop() { + StopRequest.Set(); + Worker.Join(); } public void onDebug() { OnStart(null); } + + private void DoWork(object arg) + { + // Worker thread loop + for (; ; ) + { + + // new FocusDetection(); + if (StopRequest.WaitOne(10000)) return; + } + } } }