-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardCommand.cs
More file actions
175 lines (157 loc) · 5.69 KB
/
KeyboardCommand.cs
File metadata and controls
175 lines (157 loc) · 5.69 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//-------------------------------------------------------------------
// Copyright © 2012 Kindel Systems, LLC
// http://www.kindel.com
// charlie@kindel.com
//
// Published under the MIT License.
// Source control on SourceForge
// http://sourceforge.net/projects/mcecontroller/
//-------------------------------------------------------------------
using System;
using System.Globalization;
using System.Xml.Serialization;
using WindowsInput;
using WindowsInput.Native;
namespace MCEControl
{
/// <summary>
/// Simulates a keystroke including shift, ctrl, alt, and windows key
/// modifiers.
/// </summary>
[Serializable]
public class SendInputCommand : Command
{
[XmlAttribute("Alt")]
public bool Alt;
[XmlAttribute("Ctrl")]
public bool Ctrl;
[XmlAttribute("Shift")]
public bool Shift;
[XmlAttribute("Win")]
public bool Win;
[XmlAttribute("vk")]
public string Vk;
public SendInputCommand()
{
}
public SendInputCommand(string vk, bool shift, bool ctrl, bool alt)
{
Vk = vk;
Shift = shift;
Ctrl = ctrl;
Alt = alt;
Win = false;
}
public SendInputCommand(string vk, bool shift, bool ctrl, bool alt, bool win)
{
Vk = vk;
Shift = shift;
Ctrl = ctrl;
Alt = alt;
Win = win;
}
public override void Execute(Reply reply)
{
try
{
VirtualKeyCode vkcode;
if (!Vk.ToUpper().StartsWith("VK_") ||
(!Enum.TryParse(Vk.ToUpper(), true, out vkcode) &&
!Enum.TryParse(Vk.ToUpper().Substring(3), true, out vkcode)))
{
// Not a VK_ string
// Hex?
ushort num;
if ((!Vk.ToUpper().StartsWith("0X") ||
!ushort.TryParse(Vk.Substring(2), NumberStyles.HexNumber,
CultureInfo.InvariantCulture.NumberFormat, out num)) &&
!ushort.TryParse(Vk, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat,
out num))
{
// bad format. barf.
MainWindow.AddLogEntry(string.Format("Cmd: Invalid VK: {0}", Vk));
return;
}
vkcode = (VirtualKeyCode)num;
}
string s;
if (vkcode > VirtualKeyCode.HELP && vkcode < VirtualKeyCode.LWIN)
s = char.ToUpper((char)vkcode).ToString();
else
s = "VK_" + vkcode.ToString();
if (Alt) s = "Alt-" + s;
if (Ctrl) s = "Ctrl-" + s;
if (Shift) s = "Shift-" + s;
if (Win) s = "Win-" + s;
MainWindow.AddLogEntry(string.Format("Cmd: Sending VK: '{0}' (0x{1:x2})", s, (ushort)vkcode));
var sim = new KeyboardSimulator();
if (Shift)
{
sim.KeyDown(VirtualKeyCode.SHIFT);
}
if (Ctrl)
{
sim.KeyDown(VirtualKeyCode.CONTROL);
}
if (Alt)
{
sim.KeyDown(VirtualKeyCode.MENU);
}
if (Win)
{
sim.KeyDown(VirtualKeyCode.LWIN);
}
sim.KeyPress(vkcode);
// Key up shift, ctrl, and/or alt
if (Shift)
{
sim.KeyUp(VirtualKeyCode.SHIFT);
}
if (Ctrl)
{
sim.KeyUp(VirtualKeyCode.CONTROL);
}
if (Alt)
{
sim.KeyUp(VirtualKeyCode.MENU);
}
if (Win)
{
sim.KeyUp(VirtualKeyCode.LWIN);
}
}
catch (Exception e)
{
MainWindow.AddLogEntry("Cmd: SendInput failed:" + e.Message);
}
}
public static void ShiftKey(string key, Boolean down)
{
MainWindow.AddLogEntry(string.Format("Cmd: {0} {1}", key, (down ? "down" : "up")));
var sim = new InputSimulator();
switch (key)
{
case "shift":
if (down) sim.Keyboard.KeyDown(VirtualKeyCode.SHIFT);
else sim.Keyboard.KeyUp(VirtualKeyCode.SHIFT);
break;
case "ctrl":
if (down) sim.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
else sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
break;
case "alt":
if (down) sim.Keyboard.KeyDown(VirtualKeyCode.MENU);
else sim.Keyboard.KeyUp(VirtualKeyCode.MENU);
break;
case "lwin":
if (down) sim.Keyboard.KeyDown(VirtualKeyCode.LWIN);
else sim.Keyboard.KeyUp(VirtualKeyCode.LWIN);
break;
case "rwin":
if (down) sim.Keyboard.KeyDown(VirtualKeyCode.RWIN);
else sim.Keyboard.KeyUp(VirtualKeyCode.RWIN);
break;
}
}
}
}