需要在批处理文件中模拟Win键

时间:2018-06-27 17:39:48

标签: batch-file cmd sendkeys

我一直在努力寻找答案,但是我的研究并未提出很多对我的情况有所帮助的东西。我需要在创建的批处理文件中模拟Windows密钥。这是为了帮助我最大化窗口,以及在不使用鼠标的情况下轻松地在监视器之间移动窗口。这批工作站将自动运行,无法访问鼠标或物理键盘。

这是我正在使用的代码。

    @if (@CodeSection == @Batch) @then

    @echo off

    rem Use %SendKeys% to send keys to the keyboard buffer
    set SendKeys=CScript //nologo //E:JScript "%~F0"

    rem Start the other program in the same Window
    Start "Progam"

    rem the script only works if the application in question is the active window. Set a timer

    timeout /t 10

    rem use the tab key to move the cursor to the login and password inputs.

    %SendKeys% "This would be the username{TAB}"
    %SendKeys% "this would be the password{ENTER}"

    rem I then have a timer to let the login happen

    timeout /t 10

    rem this where I am now trying to maximize and move the active window around.


    goto :EOF


    @end
    // JScript section

    var WshShell = WScript.CreateObject("WScript.Shell");
    WshShell.SendKeys(WScript.Arguments(0));

2 个答案:

答案 0 :(得分:2)

好问题!是的,我前段时间不得不为此战斗。我想在启动时自动启动从Microsoft Store安装的应用程序,由于这个原因,最简单的方法是将其固定到任务栏,然后模拟命中 + 1 启动它。我认为现在是这种情况,但是没有办法SendKeys() Windows元密钥。但是感谢LandOfTheLostPass on reddit,我发现了SendKeys()的另一种选择:发送键盘扫描代码。

我修改了reddit中的代码,使我可以模拟按下多个键,然后释放它们。如果我没记错的话,reddit上的代码只会模拟按下,然后一次释放一个键。我只修改了多键组合的扫描代码功能。现在重新检查代码,看来我可能已经破坏了SendChars()函数。但是我还是没用过。

<# : win+1.bat -- Batch portion
@echo off & setlocal

powershell -window minimized -noprofile "iex (${%~f0} | out-string)"

goto :EOF

: end batch / begin PowerShell chimera #>

# https://www.reddit.com/r/PowerShell/comments/3qk9mc/keyboard_keypress_script/
Add-Type @"

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 SendScanCodeCombo(short[] scanCodes) {
        lpInput[] KeyInputs = new lpInput[1];
        lpInput KeyInput = new lpInput();
        Enum[] actions = { KEYEVENTF.KEYDOWN, KEYEVENTF.KEYUP };

        // Generic Keyboard Event
        KeyInput.type = InputType.INPUT_KEYBOARD;
        KeyInput.Data.ki.wScan = 0;
        KeyInput.Data.ki.time = 0;
        KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;

        // Press and release the correct key combination
        foreach (KEYEVENTF action in actions) {
            foreach (short scanCode in scanCodes) {
                KeyInput.Data.ki.wVk = scanCode;
                KeyInput.Data.ki.dwFlags = action;
                KeyInputs[0] = KeyInput;
                SendInput(1, KeyInputs, lpInput.Size);
            }
        }
        return;
    }

    public static void SendChars(char[] keys) {
        lpInput[] KeyInputs = new lpInput[1];
        lpInput KeyInput = new lpInput();
        Enum[] actions = { KEYEVENTF.KEYDOWN, KEYEVENTF.KEYUP };

        // Generic Keyboard Event
        KeyInput.type = InputType.INPUT_KEYBOARD;
        KeyInput.Data.ki.wScan = 0;
        KeyInput.Data.ki.time = 0;
        KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;

        foreach (KEYEVENTF action in actions) {
            foreach (char ch in keys) {

                // Press the 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;
    }
}
"@ # end Add-Type

# Send LWin+1
[KBEmulator]::SendScanCodeCombo(@(0x5B, 0x31))

请参见Virtual-Key Codes on Microsoft Docs查找其他按键的扫描代码。


顺便说一句,Windows API提供了other ways,可以在不模拟键盘快捷键的情况下最大化和移动窗口。 (请不要太严厉地判断。在学习如何编写批处理+ PowerShell混合脚本或在PowerShell中包含.NET代码之前,我曾写过该hack。)

答案 1 :(得分:0)

您可以使用http://nircmd.nirsoft.net/

nircmd sendkeypress key