-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathKeyboard.cs
More file actions
221 lines (185 loc) · 9.68 KB
/
Keyboard.cs
File metadata and controls
221 lines (185 loc) · 9.68 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TestStack.White.UIItems.Actions;
using TestStack.White.WindowsAPI;
using Action = TestStack.White.UIItems.Actions.Action;
namespace TestStack.White.InputDevices
{
//BUG: KeysConverter
/// <summary>
/// Represents Keyboard attachment to the machine.
/// </summary>
public class Keyboard : IKeyboard
{
private static readonly List<KeyboardInput.SpecialKeys> scanCodeDependent = new List<KeyboardInput.SpecialKeys>
{
KeyboardInput.SpecialKeys.RIGHT_ALT,
KeyboardInput.SpecialKeys.INSERT,
KeyboardInput.SpecialKeys.DELETE,
KeyboardInput.SpecialKeys.LEFT,
KeyboardInput.SpecialKeys.HOME,
KeyboardInput.SpecialKeys.END,
KeyboardInput.SpecialKeys.UP,
KeyboardInput.SpecialKeys.DOWN,
KeyboardInput.SpecialKeys.PAGEUP,
KeyboardInput.SpecialKeys.PAGEDOWN,
KeyboardInput.SpecialKeys.RIGHT,
KeyboardInput.SpecialKeys.LWIN,
KeyboardInput.SpecialKeys.RWIN,
KeyboardInput.SpecialKeys.NUMPAD_0,
KeyboardInput.SpecialKeys.NUMPAD_1,
KeyboardInput.SpecialKeys.NUMPAD_2,
KeyboardInput.SpecialKeys.NUMPAD_3,
KeyboardInput.SpecialKeys.NUMPAD_4,
KeyboardInput.SpecialKeys.NUMPAD_5,
KeyboardInput.SpecialKeys.NUMPAD_6,
KeyboardInput.SpecialKeys.NUMPAD_7,
KeyboardInput.SpecialKeys.NUMPAD_8,
KeyboardInput.SpecialKeys.NUMPAD_9
};
[DllImport("user32", EntryPoint = "SendInput")]
private static extern int SendInput(uint numberOfInputs, ref Input input, int structSize);
[DllImport("user32", EntryPoint = "SendInput")]
private static extern int SendInput64(int numberOfInputs, ref Input64 input, int structSize);
[DllImport("user32.dll")]
private static extern IntPtr GetMessageExtraInfo();
[DllImport("user32.dll")]
private static extern short VkKeyScan(char ch);
[DllImport("user32.dll")]
private static extern ushort GetKeyState(uint virtKey);
private readonly List<KeyboardInput.SpecialKeys> heldKeys = new List<KeyboardInput.SpecialKeys>();
/// <summary>
/// Use Window.Keyboard method to get handle to the Keyboard. Keyboard instance got using this method would not wait while the application
/// is busy.
/// </summary>
public static readonly Keyboard Instance = new Keyboard();
private readonly List<int> keysHeld = new List<int>();
private Keyboard()
{
}
public virtual void Enter(string keysToType)
{
Send(keysToType, new NullActionListener());
}
public virtual void Send(string keysToType, IActionListener actionListener)
{
if (heldKeys.Count > 0) keysToType = keysToType.ToLower();
CapsLockOn = false;
foreach (char c in keysToType)
{
short key = VkKeyScan(c);
if (c.Equals('\r')) continue;
if (ShiftKeyIsNeeded(key)) SendKeyDown((short) KeyboardInput.SpecialKeys.SHIFT, false);
if (CtrlKeyIsNeeded(key)) SendKeyDown((short) KeyboardInput.SpecialKeys.CONTROL, false);
if (AltKeyIsNeeded(key)) SendKeyDown((short) KeyboardInput.SpecialKeys.ALT, false);
Press(key, false);
if (ShiftKeyIsNeeded(key)) SendKeyUp((short) KeyboardInput.SpecialKeys.SHIFT, false);
if (CtrlKeyIsNeeded(key)) SendKeyUp((short) KeyboardInput.SpecialKeys.CONTROL, false);
if (AltKeyIsNeeded(key)) SendKeyUp((short) KeyboardInput.SpecialKeys.ALT, false);
}
actionListener.ActionPerformed(Action.WindowMessage);
}
public virtual void PressSpecialKey(KeyboardInput.SpecialKeys key)
{
PressSpecialKey(key, new NullActionListener());
}
public virtual void PressSpecialKey(KeyboardInput.SpecialKeys key, IActionListener actionListener)
{
Send(key, true);
actionListener.ActionPerformed(Action.WindowMessage);
}
public virtual void HoldKey(KeyboardInput.SpecialKeys key)
{
HoldKey(key, new NullActionListener());
}
internal virtual void HoldKey(KeyboardInput.SpecialKeys key, IActionListener actionListener)
{
SendKeyDown((short) key, true);
heldKeys.Add(key);
actionListener.ActionPerformed(Action.WindowMessage);
}
public virtual void LeaveKey(KeyboardInput.SpecialKeys key)
{
LeaveKey(key, new NullActionListener());
}
public virtual void LeaveKey(KeyboardInput.SpecialKeys key, IActionListener actionListener)
{
SendKeyUp((short) key, true);
heldKeys.Remove(key);
actionListener.ActionPerformed(Action.WindowMessage);
}
private void Press(short key, bool specialKey)
{
SendKeyDown(key, specialKey);
SendKeyUp(key, specialKey);
}
private void Send(KeyboardInput.SpecialKeys key, bool specialKey)
{
Press((short) key, specialKey);
}
private static bool ShiftKeyIsNeeded(short key)
{
return ((key >> 8) & 1) == 1;
}
private static bool CtrlKeyIsNeeded(short key)
{
return ((key >> 8) & 2) == 2;
}
private static bool AltKeyIsNeeded(short key)
{
return ((key >> 8) & 4) == 4;
}
private void SendKeyUp(short b, bool specialKey)
{
if (!keysHeld.Contains(b)) throw new InputDeviceException(string.Format("Cannot unpress the key {0}, it has not been pressed", b));
keysHeld.Remove(b);
KeyboardInput.KeyUpDown keyUpDown = GetSpecialKeyCode(specialKey, KeyboardInput.KeyUpDown.KEYEVENTF_KEYUP, b);
SendInput(GetInputFor(b, keyUpDown));
}
private static KeyboardInput.KeyUpDown GetSpecialKeyCode(bool specialKey, KeyboardInput.KeyUpDown key, short b)
{
if (specialKey && scanCodeDependent.Contains((KeyboardInput.SpecialKeys) b)) key |= KeyboardInput.KeyUpDown.KEYEVENTF_EXTENDEDKEY;
return key;
}
private void SendKeyDown(short b, bool specialKey)
{
if (keysHeld.Contains(b)) throw new InputDeviceException(string.Format("Cannot press the key {0} as its already pressed", b));
keysHeld.Add(b);
KeyboardInput.KeyUpDown keyUpDown = GetSpecialKeyCode(specialKey, KeyboardInput.KeyUpDown.KEYEVENTF_KEYDOWN, b);
SendInput(GetInputFor(b, keyUpDown));
}
private static void SendInput(Input input)
{
// Added check for 32/64 bit
if (IntPtr.Size == 4)
SendInput(1, ref input, Marshal.SizeOf(typeof(Input)));
else
{
var input64 = new Input64(input);
SendInput64(1, ref input64, Marshal.SizeOf(typeof(Input)));
}
}
private static Input GetInputFor(short character, KeyboardInput.KeyUpDown keyUpOrDown)
{
return InputFactory.Keyboard(new KeyboardInput(character, keyUpOrDown, GetMessageExtraInfo()));
}
public virtual bool CapsLockOn
{
get
{
ushort state = GetKeyState((uint) KeyboardInput.SpecialKeys.CAPS);
return state != 0;
}
set { if (CapsLockOn != value) Send(KeyboardInput.SpecialKeys.CAPS, true); }
}
public virtual KeyboardInput.SpecialKeys[] HeldKeys
{
get { return heldKeys.ToArray(); }
}
public virtual void LeaveAllKeys()
{
new List<KeyboardInput.SpecialKeys>(heldKeys).ForEach(LeaveKey);
}
}
}