-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyCommandExecutor.cs
More file actions
146 lines (119 loc) · 4.66 KB
/
KeyCommandExecutor.cs
File metadata and controls
146 lines (119 loc) · 4.66 KB
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
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PCRemoteControlServer
{
public static class KeyCommandExecutor
{
public static Action<string>? Logger;
public static void Log(string msg)
{
Logger?.Invoke(msg);
}
public static void ExecuteCommand(string command)
{
if (string.IsNullOrWhiteSpace(command))
{
Log("Empty command.");
return;
}
var cmd = command.Trim().ToLowerInvariant();
if (IsOwnProcessForegroundWindow() && cmd != "alt_tab")
{
Log("Ignored command [" + command + "] because server window is active.");
return;
}
Log("Command: " + cmd);
const ushort VK_MENU = 0x12; // Alt
const ushort VK_TAB = 0x09; // Tab
const ushort VK_MEDIA_PLAY_PAUSE = 0xB3;
const ushort VK_MEDIA_NEXT_TRACK = 0xB0;
const ushort VK_MEDIA_PREV_TRACK = 0xB1;
const ushort VK_VOLUME_UP = 0xAF;
const ushort VK_VOLUME_DOWN = 0xAE;
const ushort VK_VOLUME_MUTE = 0xAD;
try
{
switch (cmd)
{
case "alt_tab":
KeySender.SendKeyDown(VK_MENU);
KeySender.SendKey(VK_TAB);
KeySender.SendKeyUp(VK_MENU);
break;
case "media_play_pause":
KeySender.SendKey(VK_MEDIA_PLAY_PAUSE);
break;
case "media_next":
KeySender.SendKey(VK_MEDIA_NEXT_TRACK);
break;
case "media_prev":
KeySender.SendKey(VK_MEDIA_PREV_TRACK);
break;
case "vol_up":
KeySender.SendKey(VK_VOLUME_UP);
break;
case "vol_down":
KeySender.SendKey(VK_VOLUME_DOWN);
break;
case "vol_mute":
KeySender.SendKey(VK_VOLUME_MUTE);
break;
case "shift_period":
KeySender.SendKeyDown(0x10); // Shift key
KeySender.SendKey(0xBE); // '.' key
KeySender.SendKeyUp(0x10); // Release Shift key
break;
case "shift_comma":
KeySender.SendKeyDown(0x10); // Shift key
KeySender.SendKey(0xBC); // ',' key
KeySender.SendKeyUp(0x10); // Release Shift key
break;
case "arrow_left":
KeySender.SendKey(0x25); // Left Arrow key
break;
case "arrow_right":
KeySender.SendKey(0x27); // Right Arrow key
break;
case "arrow_up":
KeySender.SendKey(0x26); // Up Arrow key
break;
case "arrow_down":
KeySender.SendKey(0x28); // Down Arrow key
break;
case "esc":
KeySender.SendKey(0x1B); // Escape key
break;
case "f":
KeySender.SendKey(0x46); // 'F' key
break;
case "m":
KeySender.SendKey(0x4D); // 'M' key
break;
case "space":
KeySender.SendKey(0x20); // Space key
break;
default:
Log("Unknown command: " + cmd);
break;
}
}
catch (Exception ex)
{
Log("Error executing command: " + ex.Message);
}
}
private static bool IsOwnProcessForegroundWindow()
{
IntPtr hwnd = GetForegroundWindow();
if (hwnd == IntPtr.Zero)
return false;
_ = GetWindowThreadProcessId(hwnd, out uint pid);
return pid == (uint)Process.GetCurrentProcess().Id;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
}