有很多解决方案,但这个代码有助于在没有第三方工具的情况下实现这一目标。遗憾的是,由于密钥不可用,我们无法使用SendKeysWin
。
这有点hacky。理想情况下,您需要查询目标监视器的分辨率,并使用所需的像素位置开始每个进程。
# stuff needed to send keystrokes
$source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyboardSend
{
public class KeyboardSend
{
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;
public static void KeyDown(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
public static void KeyUp(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
"@
Add-Type -TypeDefinition $source -ReferencedAssemblies "System.Windows.Forms"
# the arrow key combinations (0 = Left Up, 1 = Left Down, 2 = Right Up, 3 = Right Down)
$LR = @('Left','Left','Right','Right')
$UD = @('Up','Down','Up','Down')
# edit the sleep value as needed.
$sleepMS = 200
# start a process, move it in the next desirable position, x4
0..3 | % {
Start-Process powershell
# if we don't wait for the process to open, we might not have focus.
Sleep -Milliseconds $sleepMS
[KeyboardSend.KeyboardSend]::KeyDown("LWin")
[KeyboardSend.KeyboardSend]::KeyDown($LR[$_])
Sleep -Milliseconds $sleepMS
[KeyboardSend.KeyboardSend]::KeyDown($UD[$_])
[KeyboardSend.KeyboardSend]::KeyUp("LWin")
Sleep -Milliseconds $sleepMS
[KeyboardSend.KeyboardSend]::KeyDown("Escape")
}
可用密钥列表。