Powershell 代码在 powershell_ise 上运行良好但在 powershell 上中断

Powershell code works good on powershell_ise but breaks on powershell

我正在尝试编写一个代码,使 "blinks" 锁键指示灯非常快(使用 -comObject 发送密钥)。代码 运行 在 Powershell(来自 CMD)上真的很慢并且错过了一些按键,但似乎在 Powershell_ise.

上工作得很好

代码将文件读取为二进制文件,然后将每个位传输到 num/scroll 锁。这需要 运行 非常快 - 尽可能快。

这是代码:

$wsh = New-Object -ComObject "WScript.Shell"
$bytes = [Byte[]] (Get-Content  $env:temp\temp1536.txt -Encoding Byte -ReadCount 0) | ForEach-Object {[System.Convert]::ToString($_,2)}
##($i=0; $i -le $byte.length; $i++){

 foreach ($byte in $bytes) {
 #$byte;
   while($byte.length -ne 1 ){
    if($byte[1] -eq '1'){
        #echo "1";
        $wsh.SendKeys('{SCROLLLOCK}');
        [System.Threading.Thread]::Sleep(40);   
        $wsh.SendKeys('{SCROLLLOCK}');
    } Else {
        #echo "0";
        $wsh.SendKeys('{NUMLOCK}');
        [System.Threading.Thread]::Sleep(40);
        $wsh.SendKeys('{NUMLOCK}');
    }
    $byte=$byte.Substring(1);
    [System.Threading.Thread]::Sleep(50);


   }
   #echo " ";
   #echo "1";

   $wsh.SendKeys('{CAPSLOCK}');
   [System.Threading.Thread]::Sleep(55);

   $wsh.SendKeys('{CAPSLOCK}');
   [System.Threading.Thread]::Sleep(20);

 }

有人知道为什么会这样吗?

编辑: 我添加了一个视频,显示锁定键在 Powershell 控制台上闪烁的滞后。 Powershell_ISE http://youtu.be/OnOmr50OBhs

我在 Windows 7

的 Powershell V3.0/4.0 上试过这个

我在 %temp% 文件夹中使用了这个文本文件名 -'temp1536.txt' 该文件被导入为二进制文件,然后相应地点亮 LED。

$bytes = [Byte[]] (Get-Content  $env:temp\temp1536.txt -Encoding Byte -ReadCount 0) | ForEach-Object {[System.Convert]::ToString($_,2)}

我无法让您的代码正常工作(它永远不会进入 if($byte[1] -eq '1')),但这是使用 keybd_event\GetKeyState Win32 API that works fine in my ISE and console. Adapted from code posted here.

的版本
$Keyboard = @'
using System.Runtime.InteropServices;

namespace My
{
    public class Keyboard
    {
        private const byte VK_SCROLL = 0x91;
        private const byte VK_NUMLOCK = 0x90;
        private const byte VK_CAPITAL = 0x14;
        private const uint KEYEVENTF_KEYUP = 0x2;

        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        [DllImport("user32.dll", EntryPoint = "GetKeyState", SetLastError = true)]
        static extern short GetKeyState(uint nVirtKey);

        // SCROLLLOCK
        public static void SetScrollLockKey(bool newState)
        {
            bool scrollLockSet = GetKeyState(VK_SCROLL) != 0;
            if (scrollLockSet != newState)
            {
                keybd_event(VK_SCROLL, 0, 0, 0);
                keybd_event(VK_SCROLL, 0, KEYEVENTF_KEYUP, 0);
            }
        }
        public static bool GetScrollLockState()
        {
            return GetKeyState(VK_SCROLL) != 0;
        }

        // NUMLOCK
        public static void SetNumLockKey(bool newState)
        {
            bool scrollLockSet = GetKeyState(VK_NUMLOCK) != 0;
            if (scrollLockSet != newState)
            {
                keybd_event(VK_NUMLOCK, 0, 0, 0);
                keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0);
            }
        }

        public static bool GetNumLockState()
        {
            return GetKeyState(VK_NUMLOCK) != 0;
        }

        // CAPSLOCK
        public static void SetCapsLockKey(bool newState)
        {
            bool scrollLockSet = GetKeyState(VK_NUMLOCK) != 0;
            if (scrollLockSet != newState)
            {
                keybd_event(VK_CAPITAL, 0, 0, 0);
                keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
            }
        }

        public static bool GetCapsLockState()
        {
            return GetKeyState(VK_CAPITAL) != 0;
        }
    }
}
'@

Add-Type -TypeDefinition $Keyboard -ErrorAction Stop

$Bytes = Get-Content -Path '.\led.txt' -Encoding Byte

foreach ($byte in $Bytes) {
    if($byte)
    {
        [My.Keyboard]::SetScrollLockKey($true)
        [System.Threading.Thread]::Sleep(40)
        [My.Keyboard]::SetScrollLockKey($false)
    }
    else
    {
        [My.Keyboard]::SetNumLockKey($true)
        [System.Threading.Thread]::Sleep(40)
        [My.Keyboard]::SetNumLockKey($false)
    }

    [My.Keyboard]::SetCapsLockKey($true)
    [System.Threading.Thread]::Sleep(55)

    [My.Keyboard]::SetNumLockKey($false)
    [System.Threading.Thread]::Sleep(20)
}

这似乎只是某些 PC 上的问题。 无法真正理解为什么。但我让它在某些计算机上运行良好。