简体   繁体   中英

C# / Powershell send a combination of keys

I found this function that makes use of C# code to simulate key press via PowerShell.

I can simulate a WINDOWS keys like this :

[KBEmulator]::SendScanCode(0x5B)

However I want to send the combination WINDOWS+CTRL+SHIFT+B

Which, in terms of ScanCode is :

0x5B + 0x11 + 0x10 + 0x42 

Unfortunatly, i'm only fluent in PowerShell and clueless when it comes to C#.

Is there a way I could adapt this code to take a combination of keys instead of only one key at a time?

Function Keypress:

Function KeyPress {

Param (
    [Parameter(position=0, mandatory=$true, parametersetname='key')]
    [char]$Key,
    [Parameter(position=0, mandatory=$true, parametersetname='scancode')]
    [int]$ScanCode
)

$code = @"
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static class KBEmulator {    
    public enum InputType : uint {
        INPUT_MOUSE = 0,
        INPUT_KEYBOARD = 1,
        INPUT_HARDWARE = 3
    }

    [Flags]
    internal enum KEYEVENTF : uint
    {
        KEYDOWN = 0x0,
        EXTENDEDKEY = 0x0001,
        KEYUP = 0x0002,
        SCANCODE = 0x0008,
        UNICODE = 0x0004
    }

    [Flags]
    internal enum MOUSEEVENTF : uint
    {
        ABSOLUTE = 0x8000,
        HWHEEL = 0x01000,
        MOVE = 0x0001,
        MOVE_NOCOALESCE = 0x2000,
        LEFTDOWN = 0x0002,
        LEFTUP = 0x0004,
        RIGHTDOWN = 0x0008,
        RIGHTUP = 0x0010,
        MIDDLEDOWN = 0x0020,
        MIDDLEUP = 0x0040,
        VIRTUALDESK = 0x4000,
        WHEEL = 0x0800,
        XDOWN = 0x0080,
        XUP = 0x0100
    }

    // Master Input structure
    [StructLayout(LayoutKind.Sequential)]
    public struct lpInput {
        internal InputType type;
        internal InputUnion Data;
        internal static int Size { get { return Marshal.SizeOf(typeof(lpInput)); } }            
    }

    // Union structure
    [StructLayout(LayoutKind.Explicit)]
    internal struct InputUnion {
        [FieldOffset(0)]
        internal MOUSEINPUT mi;
        [FieldOffset(0)]
        internal KEYBDINPUT ki;
        [FieldOffset(0)]
        internal HARDWAREINPUT hi;
    }

    // Input Types
    [StructLayout(LayoutKind.Sequential)]
    internal struct MOUSEINPUT
    {
        internal int dx;
        internal int dy;
        internal int mouseData;
        internal MOUSEEVENTF dwFlags;
        internal uint time;
        internal UIntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct KEYBDINPUT
    {
        internal short wVk;
        internal short wScan;
        internal KEYEVENTF dwFlags;
        internal int time;
        internal UIntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct HARDWAREINPUT
    {
        internal int uMsg;
        internal short wParamL;
        internal short wParamH;
    }

    private class unmanaged {
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern uint SendInput (
            uint cInputs, 
            [MarshalAs(UnmanagedType.LPArray)]
            lpInput[] inputs,
            int cbSize
        );

        [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern short VkKeyScan(char ch);
    }

    internal static short VkKeyScan(char ch) {
        return unmanaged.VkKeyScan(ch);
    }

    internal static uint SendInput(uint cInputs, lpInput[] inputs, int cbSize) {
        return unmanaged.SendInput(cInputs, inputs, cbSize);
    }

    public static void SendScanCode(short scanCode) {
        lpInput[] KeyInputs = new lpInput[1];
        lpInput KeyInput = new lpInput();
        // Generic Keyboard Event
        KeyInput.type = InputType.INPUT_KEYBOARD;
        KeyInput.Data.ki.wScan = 0;
        KeyInput.Data.ki.time = 0;
        KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;


        // Push the correct key
        KeyInput.Data.ki.wVk = scanCode;
        KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN;
        KeyInputs[0] = KeyInput;
        SendInput(1, KeyInputs, lpInput.Size);

        // Release the key
        KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP;
        KeyInputs[0] = KeyInput;
        SendInput(1, KeyInputs, lpInput.Size);

        return;
    }

    public static void SendKeyboard(char ch) {
        lpInput[] KeyInputs = new lpInput[1];
        lpInput KeyInput = new lpInput();
        // Generic Keyboard Event
        KeyInput.type = InputType.INPUT_KEYBOARD;
        KeyInput.Data.ki.wScan = 0;
        KeyInput.Data.ki.time = 0;
        KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;


        // Push the correct key
        KeyInput.Data.ki.wVk = VkKeyScan(ch);
        KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN;
        KeyInputs[0] = KeyInput;
        SendInput(1, KeyInputs, lpInput.Size);

        // Release the key
        KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP;
        KeyInputs[0] = KeyInput;
        SendInput(1, KeyInputs, lpInput.Size);

        return;
    }
}
"@

if(([System.AppDomain]::CurrentDomain.GetAssemblies() | ?{$_ -match "KBEmulator"}) -eq $null) {
    Add-Type -TypeDefinition $code
}

switch($PSCmdlet.ParameterSetName){
    'key' { [KBEmulator]::SendKeyboard($Key) }
    'scancode' { [KBEmulator]::SendScanCode($ScanCode) }
}

}

[KBEmulator]::SendScanCode(0x5B)

Here's my attempt at it, should be a place to start at least. Haven't done any code in C# in a while so am unsure on how to program in it exactly but it hopefully should take in character array and press the keys in array and send as an array struct. The for loop might be an index off though not sure, and there may be a random error I didn't realize in code and I'm not even sure if it'll do all key presses at same time or one at a time, but hopefully this helps you out where to start.

public static void SendKeyboard(char ch) {
    lpInput[] KeyInputs = new lpInput[1];
    lpInput KeyInput = new lpInput();
    // Generic Keyboard Event
    KeyInput.type = InputType.INPUT_KEYBOARD;
    KeyInput.Data.ki.wScan = 0;
    KeyInput.Data.ki.time = 0;
    KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;


    // Push the correct key
    KeyInput.Data.ki.wVk = VkKeyScan(ch);
    KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN;
    KeyInputs[0] = KeyInput;
    SendInput(1, KeyInputs, lpInput.Size);

    // Release the key
    KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP;
    KeyInputs[0] = KeyInput;
    SendInput(1, KeyInputs, lpInput.Size);

    return;  
    }

 // Attempt at overloading for multiple key presses
 public static void SendKeyboard(char[] ch) {
    lpInput[] KeyInputs = new lpInput[ch.length];

    // Push the correct key
    for (int i = 0; i < ch.length; i++) {
    // Generate new memory address for KeyInput each time
    lpInput KeyInput = new lpInput();
    // Generic Keyboard Event
    KeyInput.type = InputType.INPUT_KEYBOARD;
    KeyInput.Data.ki.wScan = 0;
    KeyInput.Data.ki.time = 0;
    KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;
    KeyInput.Data.ki.wVk = VkKeyScan(ch[i]);
    KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN;
    KeyInputs[i] = KeyInput;
    }
    SendInput( ch.length, KeyInputs, ( lpInput.Size * ch.length ) );

    // Release the key
    for (int i = 0; i < ch.length; i++) {
    // Generate new memory address for KeyInput each time
    lpInput KeyInput = new lpInput();
    // Generic Keyboard Event
    KeyInput.type = InputType.INPUT_KEYBOARD;
    KeyInput.Data.ki.wScan = 0;
    KeyInput.Data.ki.time = 0;
    KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;
    KeyInput.Data.ki.wVk = VkKeyScan(ch[i]);
    KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP;
    KeyInputs[i] = KeyInput;
    }
    SendInput(ch.length, KeyInputs, ( lpInput.Size * ch.length ) );

    return;  
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM