-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
135 lines (108 loc) · 4.62 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using ConsoleHotKey;
using System.Text.RegularExpressions;
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll")]
static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
const uint WM_GETTEXT = 0x000D;
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x0101;
HotKeyManager.RegisterHotKey(Keys.M, KeyModifiers.Alt | KeyModifiers.Control | KeyModifiers.Shift);
HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
Console.WriteLine("Listening on hotkey CTRL+SHIFT+ALT+M..");
Console.WriteLine("Exit with CTRL+c");
await Task.Delay(Timeout.Infinite).ConfigureAwait(false);
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
DateTime start = DateTime.UtcNow;
//Console.WriteLine("finding window..");
Process? p = findProcess();
DateTime afterFindProcess = DateTime.UtcNow;
//Console.WriteLine("findProcess: {0} ms", Convert.ToInt32((afterFindProcess - start).TotalMilliseconds));
Console.WriteLine("\nTeams process: {0}", p?.Id);
if (p != null)
{
IntPtr? w = findCallWindow(p);
DateTime afterFindWindow = DateTime.UtcNow;
//Console.WriteLine("findWindow: {0} ms", Convert.ToInt32((afterFindWindow - afterFindProcess).TotalMilliseconds));
if (w != null)
{
Console.WriteLine("found call window: {0}", w);
//Thread.Sleep(200);
SetForegroundWindow((IntPtr)w);
//Thread.Sleep(200);
SendKeys.SendWait("+^M");
SendKeys.Flush();
DateTime finish = DateTime.UtcNow;
Console.WriteLine("sendKeys: {0} ms", Convert.ToInt32((finish - afterFindWindow).TotalMilliseconds));
/*
PostMessage((IntPtr)w, WM_KEYDOWN, (IntPtr)(Keys.Control), IntPtr.Zero);
PostMessage((IntPtr)w, WM_KEYDOWN, (IntPtr)(Keys.E), IntPtr.Zero);
Thread.Sleep(500);
PostMessage((IntPtr)w, WM_KEYUP, (IntPtr)(Keys.E), IntPtr.Zero);
PostMessage((IntPtr)w, WM_KEYUP, (IntPtr)(Keys.Control), IntPtr.Zero);
*/
// TODO switch back to original window?
}
else
{
Console.WriteLine("no teams window found!");
}
}
else
{
Console.WriteLine("no teams process found!");
}
}
static Process? findProcess() => Process.GetProcesses()
.Where(p => p.ProcessName == "Teams")
.FirstOrDefault();
static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
{
var handles = new List<IntPtr>();
foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
EnumThreadWindows(thread.Id,
(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
return handles;
}
/// returns window actual teams window that is in foreground or else any teams window
static IntPtr? findCallWindow(Process p)
{
Regex rx = new Regex(@" Microsoft Teams$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
IEnumerable<IntPtr> windows = EnumerateProcessWindowHandles(p.Id);
/*foreach (IntPtr windowHandle in windows)
{
StringBuilder message = new StringBuilder(1000);
SendMessage(windowHandle, WM_GETTEXT, message.Capacity, message);
if (message.Length > 0 && message.ToString().Length > 0)
Console.WriteLine("{0}: '{1}'", windowHandle, message);
} */
IEnumerable<IntPtr> teamsWindows = windows.Where(w =>
{
StringBuilder message = new StringBuilder(1000);
SendMessage(w, WM_GETTEXT, message.Capacity, message);
//Console.WriteLine("Considering {0}: '{1}'", w, message);
return rx.IsMatch(message.ToString());
});
IntPtr foregroundWindow = GetForegroundWindow();
//Console.WriteLine("ForgroundWindow: {0}", foregroundWindow);
IntPtr teamsForegroundWindow = teamsWindows.Where(w => w == foregroundWindow).FirstOrDefault(IntPtr.Zero);
if (teamsForegroundWindow != IntPtr.Zero)
{
return teamsForegroundWindow;
}
else
{
return teamsWindows.Any() ? teamsWindows.First() : null;
}
}
delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);