答案1
有很多解决方案,但是此代码有助于实现这一目标,而无需第三方工具。遗憾的是,我们不能使用发送键因为Win
密钥不可用。
这有点儿不妥。理想情况下,您需要查询目标显示器的分辨率,并以所需的像素位置启动每个进程。
# 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")
}