簡體   English   中英

Powershell代碼在powershell_ise上運行良好,但在powershell上中斷

[英]Powershell code works good on powershell_ise but breaks on powershell

我正在嘗試編寫一個非常快的“閃爍”鎖定鍵指示燈的代碼(使用-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 Console Vs上的鎖定鍵閃爍的滯后時間。 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的版本,在我的ISE和控制台中運行良好。 改編自此處發布的代碼。

$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中這似乎是一個問題。 真的不明白為什么。 但是我知道它可以在某些計算機上正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM