public void SendKeys(KeyEventSequence keys) { Log(string.Format("Sending {0}", keys.ToString())); uint result = Win32.SendInput(keys, true, TimeSpan.FromMilliseconds(20)); Log("SendInput reports it sent this many events: " + result.ToString()); ShowKeysSending(); //SetIcon("green-yellow-yellow", TimeSpan.FromMilliseconds(125), "green-yellow-black"); }
public static uint SendInput(KeyEventSequence keys, bool sendKeysIndividually, Nullable<TimeSpan> pauseBetweenKeys) { if (IntPtr.Size == 4) { INPUT32[] inputs = keys.ToInputArray32(); if (sendKeysIndividually) { INPUT32[] chunkedInput = new INPUT32[1]; uint total = 0; for (int i = 0; i < inputs.Length; ++i) { chunkedInput[0] = inputs[i]; total += Win32.SendInput32(1, chunkedInput, Marshal.SizeOf(typeof(INPUT32))); if (pauseBetweenKeys.HasValue) { Thread.Sleep(pauseBetweenKeys.Value); } } return total; } else { return Win32.SendInput32((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT32))); } } else if (IntPtr.Size == 8) { INPUT64[] inputs = keys.ToInputArray64(); if (sendKeysIndividually) { INPUT64[] chunkedInput = new INPUT64[1]; uint total = 0; for (int i = 0; i < inputs.Length; ++i) { chunkedInput[0] = inputs[i]; total += Win32.SendInput64(1, chunkedInput, Marshal.SizeOf(typeof(INPUT64))); if (pauseBetweenKeys.HasValue) { Thread.Sleep(pauseBetweenKeys.Value); } } return total; } else { return Win32.SendInput64((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT64))); } } else { throw new NotSupportedException("Can't figure out platform bitness. Pointers have this width: " + IntPtr.Size.ToString()); } }
public static KeyEventSequence Parse(string specification) { try { List<ParseNode> parseTree = new List<ParseNode>(); ParseNode root = new ParseNode(); Parse(specification, root); KeyEventSequence keyEventSequence = new KeyEventSequence(); AddNode(keyEventSequence, root); return keyEventSequence; } catch (KeySpecificationParseException) { return null; } }
private static void AddNode(KeyEventSequence keyEventSequence, ParseNode node) { if (node.HasContent) { if (node.Direction != KeyDirection.Up) { keyEventSequence.Add(node.ToKeyEvent(KeyDirection.Down)); } } foreach (ParseNode child in node.Children) { AddNode(keyEventSequence, child); } if (node.HasContent) { if (node.Direction != KeyDirection.Down) { keyEventSequence.Add(node.ToKeyEvent(KeyDirection.Up)); } } }
private void Test(string input, params KeyEventSequence[] expectedSequences) { KeyEventSequence expectedSequence = new KeyEventSequence(); foreach (KeyEventSequence sequence in expectedSequences) { expectedSequence.AddRange(sequence); } AssertEqual(expectedSequence, KeySequenceParser.Parse(input)); }
private KeyEventSequence MakeExpectedKeySequence(params Keys[] keys) { KeyEventSequence sequence = new KeyEventSequence(); for (int i = 0; i < keys.Length; i++) { sequence.Add(new KeyEvent { Direction = KeyDirection.Down, Key = keys[i] }); } for (int i = keys.Length - 1; i >= 0; i--) { sequence.Add(new KeyEvent { Direction = KeyDirection.Up, Key = keys[i] }); } return sequence; }
private void AssertEqual(KeyEventSequence expected, KeyEventSequence actual) { Assert.IsNotNull(actual, "Checking that parsed key sequence was not null."); Assert.AreEqual(expected.Count, actual.Count, "Checking that lengths are equal."); for (int i = 0; i < expected.Count; i++) { KeyEvent expectedEvent = expected[i]; KeyEvent actualEvent = actual[i]; AssertEqual(expectedEvent, actualEvent); } }