/// <summary> /// Adds a key down to the list of <see cref="INPUT"/> messages. /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/>.</param> /// <returns>This <see cref="InputBuilder"/> instance.</returns> public InputBuilder AddKeyDown(DirectInputKeyCode dikCode) { var down = new INPUT { Type = (UInt32)InputType.Keyboard, Data = { Keyboard = new KEYBDINPUT { KeyCode = 0, Scan = (UInt16)dikCode, Flags = (UInt32)(((UInt16)dikCode > 0x7F) ? KeyboardFlag.ScanCode | KeyboardFlag.ExtendedKey : KeyboardFlag.ScanCode), Time = 0, ExtraInfo = IntPtr.Zero } } }; _inputList.Add(down); return(this); }
/// <summary> /// Adds a key up to the list of Input messages. /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/>.</param> /// <returns>This <see cref="InputBuilder"/> instance.</returns> public InputBuilder AddKeyUp(DirectInputKeyCode dikCode) { var up = new INPUT { Type = (uint)InputType.Keyboard, Data = { Keyboard = new KEYBDINPUT { KeyCode = 0, Scan = (ushort)dikCode, Flags = (uint)(((ushort)dikCode > 0x7F) ? KeyboardFlag.ScanCode | KeyboardFlag.KeyUp | KeyboardFlag.ExtendedKey : KeyboardFlag.ScanCode | KeyboardFlag.KeyUp), Time = 0, ExtraInfo = IntPtr.Zero } } }; _inputList.Add(up); return(this); }
private void SimulateKeyStroke(DirectInputKeyCode[] keyStrokes, DirectInputKeyCode keyCode) { while (_keyPressed) { if (settings.ForcedKeydown) { foreach (var keystroke in keyStrokes) { iis.Keyboard.KeyDown(keystroke); } iis.Keyboard.KeyDown(keyCode); } else { iis.Keyboard.ModifiedKeyStroke(keyStrokes, keyCode); } Thread.Sleep(30); } if (settings.ForcedKeydown) { iis.Keyboard.KeyUp(keyCode); foreach (var keystroke in keyStrokes) { iis.Keyboard.KeyUp(keystroke); } } }
/// <summary> /// Calls the Win32 SendInput method to simulate a KeyUp. /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/> to lift up</param> public IKeyboardSimulator KeyUp(DirectInputKeyCode dikCode) { var inputList = new InputBuilder().AddKeyUp(dikCode).ToArray(); SendSimulatedInput(inputList); return(this); }
public override void Run(DuckyScript d) { foreach (char c in s) { short result = NativeMethods.VkKeyScan(c); uint keyCode = (uint)result & 0xFF; var modifiers = new List <DirectInputKeyCode>(); if ((result & 0x0100) != 0) { modifiers.Add(DirectInputKeyCode.DIK_LSHIFT); } if ((result & 0x0200) != 0) { modifiers.Add(DirectInputKeyCode.DIK_LCONTROL); } if ((result & 0x0400) != 0) { modifiers.Add(DirectInputKeyCode.DIK_LALT); } if ((result & 0x0800) != 0) { // TODO: What is the key code for the hankaku key? Possibilities DIK_CONVERT, DIK_NOCONVERT, DIK_KANA, DIK_KANJI ; } DirectInputKeyCode kc = (DirectInputKeyCode)NativeMethods.MapVirtualKey(keyCode, NativeMethods.MapVirtualKeyMapTypes.MAPVK_VK_TO_VSC); d.sim.Keyboard.ModifiedKeyStroke(modifiers, kc); System.Threading.Thread.Sleep(KeystrokeDelay); } }
/// <summary> /// Calls the Win32 SendInput method with a KeyUp message /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/> to press</param> /// <param name="delay">Delay in ms between keydown and keyup of final keyCode. 50ms should be minimum</param> public IKeyboardSimulator DelayedKeyPressUp(DirectInputKeyCode dikCode, int delay) { var inputList2 = new InputBuilder().AddKeyUp(dikCode).ToArray(); SendSimulatedInput(inputList2); return(this); }
private void SimulateKeyDown(DirectInputKeyCode keyCode) { while (_keyPressed) { iis.Keyboard.KeyDown(keyCode); Thread.Sleep(30); } iis.Keyboard.KeyUp(keyCode); // Release key at the end }
/// <summary> /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS. /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/> to press</param> /// <param name="delay">Delay in ms between keydown and keyup of final keyCode. 50ms should be minimum</param> public IKeyboardSimulator DelayedKeyPress(DirectInputKeyCode dikCode, int delay) { DelayedKeyPressDown(dikCode, delay); Thread.Sleep(delay); DelayedKeyPressUp(dikCode, delay); return(this); }
public DirectInputKeyCode ConvertLocaleScanCode(DirectInputKeyCode scanCode) { //german // http://kbdlayout.info/KBDGR/shiftstates+scancodes/base // french // http://kbdlayout.info/kbdfr/shiftstates+scancodes/base // usa // http://kbdlayout.info/kbdusx/shiftstates+scancodes/base if (Program.Bindings.KeyboardLayout != "en-US") { Logger.Instance.LogMessage(TracingLevel.INFO, scanCode.ToString() + " " + ((ushort)scanCode).ToString("X")); int lpdwProcessId; IntPtr hWnd = GetForegroundWindow(); int WinThreadProcId = GetWindowThreadProcessId(hWnd, out lpdwProcessId); var hkl = GetKeyboardLayout(WinThreadProcId); Logger.Instance.LogMessage(TracingLevel.INFO, ((long)hkl).ToString("X")); //hkl = (IntPtr)67568647; // de-DE 4070407 // Maps the virtual scanCode to key code for the current locale var virtualKeyCode = MapVirtualKeyEx((ushort)scanCode, 3, hkl); if (virtualKeyCode > 0) { // map key code back to en-US scan code : hkl = (IntPtr)67699721; // en-US 4090409 var virtualScanCode = MapVirtualKeyEx((ushort)virtualKeyCode, 4, hkl); if (virtualScanCode > 0) { Logger.Instance.LogMessage(TracingLevel.INFO, "keycode " + virtualKeyCode.ToString("X") + " scancode " + virtualScanCode.ToString("X") + " keyboard code " + hkl.ToString("X")); return((DirectInputKeyCode)(virtualScanCode & 0xff)); // only use low byte } } } return(scanCode); }
/// <summary> /// Simulates a modified keystroke where there is one modifier and multiple keys like CTRL-K-C where CTRL is the modifierKey and K and C are the keys. /// The flow is Modifier KeyDown, Keys Press in order, Modifier KeyUp. /// </summary> /// <param name="modifierDikCode">The modifier key</param> /// <param name="dikCodes">The list of keys to simulate</param> public IKeyboardSimulator ModifiedKeyStroke(DirectInputKeyCode modifierDikCode, IEnumerable <DirectInputKeyCode> dikCodes) { ModifiedKeyStroke(new[] { modifierDikCode }, dikCodes); return(this); }
/// <summary> /// Simulates a modified keystroke where there are multiple modifiers and multiple keys like CTRL-ALT-K-C where CTRL and ALT are the modifierKeys and K and C are the keys. /// The flow is Modifiers KeyDown in order, Keys Press in order, Modifiers KeyUp in reverse order. /// </summary> /// <param name="modifierDikCodes">The list of modifier keys</param> /// <param name="dikCode">The list of keys to simulate</param> /// <param name="delay">Delay in ms between keydown and keyup of final keyCode. 50ms should be minimum</param> public IKeyboardSimulator DelayedModifiedKeyStroke(IEnumerable <DirectInputKeyCode> modifierDikCodes, DirectInputKeyCode dikCode, int delay) { foreach (var keyCode in modifierDikCodes) { var inputList = new InputBuilder().AddKeyDown(keyCode).ToArray(); SendSimulatedInput(inputList); } KeyDown(dikCode); Thread.Sleep(delay); KeyUp(dikCode); foreach (var keyCode in modifierDikCodes.Reverse()) { var inputList = new InputBuilder().AddKeyUp(keyCode).ToArray(); SendSimulatedInput(inputList); } return(this); }
/// <summary> /// Simulates a simple modified keystroke like CTRL-C where CTRL is the modifierKey and C is the key. /// The flow is Modifier KeyDown, Key Press, Modifier KeyUp. /// </summary> /// <param name="modifierDikCode">The modifier key</param> /// <param name="dikCode">The key to simulate</param> public IKeyboardSimulator ModifiedKeyStroke(DirectInputKeyCode modifierDikCode, DirectInputKeyCode dikCode) { ModifiedKeyStroke(new[] { modifierDikCode }, new[] { dikCode }); return(this); }
/// <summary> /// Simulates a modified keystroke where there are multiple modifiers and one key like CTRL-ALT-C where CTRL and ALT are the modifierKeys and C is the key. /// The flow is Modifiers KeyDown in order, Key Press, Modifiers KeyUp in reverse order. /// </summary> /// <param name="modifierDikCodes">The list of modifier keys</param> /// <param name="dikCode">The key to simulate</param> public IKeyboardSimulator ModifiedKeyStroke(IEnumerable <DirectInputKeyCode> modifierDikCodes, DirectInputKeyCode dikCode) { ModifiedKeyStroke(modifierDikCodes, new[] { dikCode }); return(this); }
public DirectKeyPressCommand(DirectInputKeyCode keyCode) : this(null, keyCode) { }
/// <summary> /// Simulates a modified keystroke where there is one modifier and multiple keys like CTRL-K-C where CTRL is the modifierKey and K and C are the keys. /// The flow is Modifier KeyDown, Keys Press in order, Modifier KeyUp. /// </summary> /// <param name="modifierDikCode">The modifier key</param> /// <param name="dikCodes">The list of keys to simulate</param> public IKeyboardSimulator ModifiedKeyStroke(DirectInputKeyCode modifierDikCode, IEnumerable<DirectInputKeyCode> dikCodes) { ModifiedKeyStroke(new[] { modifierDikCode }, dikCodes); return this; }
public DirectKeyPressCommand(DirectInputKeyCode modifierKeyCode, string arg) : this(new DirectInputKeyCode[] { modifierKeyCode }, StringToDirectInputKeyCode(arg)) { }
public DirectKeyPressCommand(IEnumerable <DirectInputKeyCode> modifiers, DirectInputKeyCode key) { this.modifiers = modifiers; this.key = key; }
/// <summary> /// Simulates a modified keystroke where there are multiple modifiers and one key like CTRL-ALT-C where CTRL and ALT are the modifierKeys and C is the key. /// The flow is Modifiers KeyDown in order, Key Press, Modifiers KeyUp in reverse order. /// </summary> /// <param name="modifierDikCodes">The list of modifier keys</param> /// <param name="dikCode">The key to simulate</param> public IKeyboardSimulator ModifiedKeyStroke(IEnumerable<DirectInputKeyCode> modifierDikCodes, DirectInputKeyCode dikCode) { ModifiedKeyStroke(modifierDikCodes, new[] { dikCode }); return this; }
/// <summary> /// Calls the Win32 SendInput method to simulate a KeyDown. /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/> to press</param> public IKeyboardSimulator KeyDown(DirectInputKeyCode dikCode) { var inputList = new InputBuilder().AddKeyDown(dikCode).ToArray(); SendSimulatedInput(inputList); return this; }
/// <summary> /// Simulates a modified keystroke where there are multiple modifiers and multiple keys like CTRL-ALT-K-C where CTRL and ALT are the modifierKeys and K and C are the keys. /// The flow is Modifiers KeyDown in order, Keys Press in order, Modifiers KeyUp in reverse order. /// </summary> /// <param name="modifierDikCodes">The list of modifier keys</param> /// <param name="dikCode">The list of keys to simulate</param> /// <param name="delay">Delay in ms between keydown and keyup of final keyCode. 50ms should be minimum</param> public IKeyboardSimulator DelayedModifiedKeyStroke(IEnumerable <DirectInputKeyCode> modifierDikCodes, DirectInputKeyCode dikCode, int delay) { DelayedModifiedKeyStrokeDown(modifierDikCodes, dikCode, delay); Thread.Sleep(delay); DelayedModifiedKeyStrokeUp(modifierDikCodes, dikCode, delay); return(this); }
/// <summary> /// Adds a key press to the list of Input messages which is equivalent to a key down followed by a key up. /// </summary> /// <param name="dikCode">The <see cref="DirectInputKeyCode"/>.</param> /// <returns>This <see cref="InputBuilder"/> instance.</returns> public InputBuilder AddKeyPress(DirectInputKeyCode dikCode) { AddKeyDown(dikCode); AddKeyUp(dikCode); return(this); }
/// <summary> /// Simulates a simple modified keystroke like CTRL-C where CTRL is the modifierKey and C is the key. /// The flow is Modifier KeyDown, Key Press, Modifier KeyUp. /// </summary> /// <param name="modifierDikCode">The modifier key</param> /// <param name="dikCode">The key to simulate</param> public IKeyboardSimulator ModifiedKeyStroke(DirectInputKeyCode modifierDikCode, DirectInputKeyCode dikCode) { ModifiedKeyStroke(new[] { modifierDikCode }, new[] { dikCode }); return this; }