I'm trying to write a code that "blinks" the lock key leds really fast (using -comObject to sendkeys). The code run really slow on Powershell (from CMD) and miss some keypress, but seems to work great on Powershell_ise.
The code reads a file to binary and then transfer each bit to num/scroll lock. This needs to run really fast - as fast as I can.
This is the code:
$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);
}
Anyone knows why this happens?
EDIT: I added a video showing the lag of the lock key blinking on Powershell Console Vs. Powershell_ISE http://youtu.be/OnOmr50OBhs
I tried this on Powershell V3.0/4.0 on Windows 7
I used this text file name -'temp1536.txt' in %temp% folder The file is imported to binary and then light the led accordingly.
$bytes = [Byte[]] (Get-Content $env:temp\temp1536.txt -Encoding Byte -ReadCount 0) | ForEach-Object {[System.Convert]::ToString($_,2)}