Detect Keystrokes In Different Window with Batch - batch-file

Fair people of stack overflow! My question today is how to detect key presses, but not in the batch window itself. I have this code already:
choice /c KEY /n /t 5 /d d
if %errorlevel%==1 goto restofcode
This only works if you select the batch window itself. What I need is a way, in batch (or some other language) to make it detect if you press Ctrl+W outside of the actual batch command line itself.
There is a program called GS Auto-Clicker and it starts clicking your mouse when you press a key. I want something like this as far as detecting what key you press.

Okie doke, here's an example demonstrating GetAsyncKeyState() using a Batch + PowerShell hybrid script. Save it with a .bat extension. Hopefully the inline comments sufficiently explain how it works. See the MSDN page on GetAsyncKeyState() for full details on that function's expected arguments and return value type.
Edit: Fixed bugs. Original answer would trigger if the user pressed and released W then pressed Ctrl. It seems my PowerShell 2.0 interpreter has some buggy behavior. Setting variables to the GetAsyncKeyState() return value on each loop iteration seems to fix it. I also made the key definitions a little less neck-beardy by leveraging the System.Windows.Forms.Keys collection, and removed the possibility that the "recently pressed" bit would trigger a false positive for "currently pressed".
<# : batch portion (begins PowerShell multiline comment block)
#echo off & setlocal
set /P "=Waiting for ctrl-W... "<NUL
rem # re-launch self with PowerShell interpreter
powershell -noprofile "iex (${%~f0} | out-string)"
echo Pressed. Toodles.
goto :EOF
: end batch / begin PowerShell chimera #>
# import GetAsyncKeyState()
Add-Type user32_dll #'
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
'# -namespace System
# for Keys object constants
Add-Type -As System.Windows.Forms
function keyPressed($key) {
return [user32_dll]::GetAsyncKeyState([Windows.Forms.Keys]::$key) -band 32768
}
while ($true) {
$ctrl = keyPressed "ControlKey"
$W = keyPressed "W"
if ($ctrl -and $W) { break }
start-sleep -milliseconds 40
}
$Host.UI.RawUI.FlushInputBuffer()

Related

how to concatenate two variables when one has spaces in it in AzureDevops batch file

I have this input in my ps script. I am passing $(Build.ArtifactStagingDirectory) system variable to get the path in the input %1. The path has spaces i.e. E:\Build Agents\Agent2\_work\15\a. I need to concatenate this with other path but I am not able to do so.
set BuildDrop=%1
set Directory=%BuildDrop% + "\adapters\bin"
This is my output, which is incorrect as Directory should be something like E:\Build Agents\Agent2\_work\15\a\adapters\bin. How to solve this?
set BuildDrop="E:\Build Agents\Agent2\_work\15\a"
set Directory="E:\Build Agents\Agent2\_work\15\a" + "\adapters\bin"
My task is like this in my build pipeline
Task : Batch script
Description : Run a Windows command or batch script and optionally allow it to change the environment
Version : 1.1.10
I found the solution for this. Since my %1 had space I needed to remove apostrohphe before assigning. I did it using ~ variable. working code looks like this.
set "BuildDrop=%~1"
set "Directory=%BuildDrop%\adapters\bin"
You could try this :
set BuildDrop=%1
set Directory= %BuildDrop%\adapters\bin
echo %Directory%
Sample Output
This is the concatenation code.
%BuildDrop%\adapters\bin
In my above sample I am trying to concatenate C:\Users\svijay\Desktop & \adapters\bin using the batch script
And the output : C:\Users\svijay\Desktop\adapters\bin
UPDATE :
#echo off
set BuildDrop=%1
set Directory= %BuildDrop%\adapters\bin
set Directory= %Directory:"=%
echo %Directory%
If you have space, you could provide the "" to provide input.
In your code you could remove the double quotes.
%Directory:"=% - Removes the Double quotes from the string literal.
Sample output :
You may use this to create Azure DevOps variable containing your path
powershell: |
$directory = $(Build.ArtifactStagingDirectory)
$newDirectory = $directory + "\adapters\bin"
Write-Host "##vso[task.setvariable variable=testvar]$newDirectory "
and if you need set variable in powershell script
$BuildDrop=$args[0]
$Directory=$BuildDrop + "\adapters\bin"
Here you have an article how to use parameters in powershell.
We can use powershell task in the Azure DevOps to concatenate two variables.
Sample:
Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)"
Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)\adapters\bin"
Result:
In addition, I found a similar issue, please also check it.
Update1
Use power shell script in the Azure DevOps pipeline
$testpath1 = "E:\Build Agents\Agent2\_work\15\a"
$testpath2 = "\adapters\bin"
Write-Host "path is $($testpath1)$($testpath2)"
Result:
Use local power shell
Result:
Update2
.bat file
set testpath1=E:\Build Agents\Agent2\_work\15\a
set testpath2=\adapters\bin
set newpath=%testpath1%%testpath2%
echo %newpath%
Pipeline result:

write robocopy output to console and log file

I have an interactive PowerShell using windows forms, meaning a PowerShell script displays some controls on a form asking for input and finally runs robocopy, when the user clicks the button.
This is a simple script displaying the problem.
You can copy the code, save it to a Power Sell script and run. It needs a:\tmp\tmp folder to successfully run, see the CopyFolder function.
function createdMainForm {
$mainForm = New-Object System.Windows.Forms.Form
$btn = New-Object System.Windows.Forms.Button
$btn.Text = "RoboCopy"
#register click event
$btn.add_click({Add_click})
$mainForm.Controls.Add($btn)
$mainForm.ShowDialog()
}
#click event
function Add_click() {
CopyFolder
}
function CopyFolder() {
$sourseFolder = "C:\tmp\tmp"
$targetFolder = "C:\tmp\tmp2"
$Logfile = "c:\tmp\a.log"
robocopy $sourseFolder $targetFolder /tee /log:$Logfile
}
function ReadMode() {
Write-Host Mode [1 for GUI mode]
$mode = Read-Host
if ($mode -eq 1) {
createdMainForm
} else {
CopyFolder
}
}
ReadMode
I want to capture the robocopy progress in a log file as well as in the console.
However only the log file capture the output, while the console "hangs" until end.
I found that is works smoothly when PowerShell does not display form, but simply runs the command.
The script works in two modes. enter 1 for "form mode" anything else for console mode, where the log actually written to console and file.
How can I use the form, and display the progress in the console?
Use the proper robocopy parameters:
/log:<LogFile> Writes the status output to the log file (overwrites the existing log file).
…
/tee Writes the status output to the console window, as well as to the log file.
robocopy $s $t /tee /log:C:\path\to\your.log
As for updating the output of your embedded control, you probably need to refresh your form on a timer:
$timer = New-Object Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({$form.Refresh()})
Start the timer when calling robocopy and stop it after the command completes. Details will vary depending on your actual code (which you chose to omit).
Edit: I think I finally understood what you're trying to achieve and what problem you have getting there.
You're running robocopy from a scriptblock that is associated with a form element. Because of that STDOUT output doesn't go to the console. You need to take the robocopy output (the success output stream) and write it to the host console yourself:
function CopyFolder() {
...
robocopy $sourseFolder $targetFolder /tee /log:$Logfile | Write-Host
}

Batch icon message on task bar

Is it possible to make a Batch, or .vbs if needed, is it possible to make batch have a little icon down there with the flag, battery and volume.
I want it to be a "Shutting down in xx minutes/hours", and mayyybbe clicking it cancels' it.
Thanks in advance :)
Well, I know how to accomplish part of what you want -- making a systray balloon tip by borrowing from PowerShell. But I don't know how to make it listen for dismissal of the balloon. Maybe someone else can offer another answer building upon mine?
Anyway, I use this for a conversion script I made to convert flac to mp3 in batches. Feel free to hack it for your own evil purposes.
#echo off
setlocal
for %%I in (*.flac) do (
rem // This initiates the systray balloon.
call :systray converting from "%%~nxI" to "%%~nI.mp3"
)
goto :EOF
rem // Here's the :systray function
:systray <message>
setlocal enabledelayedexpansion
set "args=%*"
set "args=!args:'=''!"
set "code="[void] [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');^
$o=New-Object Windows.Forms.NotifyIcon;$o.Icon='%systemroot%\System32\PerfCenterCpl.ico';^
$o.BalloonTipIcon='Info';$o.BalloonTipText='!args!';$o.BalloonTipTitle='%~nx0';$o.Visible=1;^
$o.ShowBalloonTip(10000);Start-Sleep -M 12000;$o.Dispose();Exit""
start /b "" "powershell" %code%
endlocal & goto :EOF
The n values in $o.ShowBalloonTip(n1) and Start-Sleep -Mn2 are in milliseconds. Salt to taste.
Update: I found a bit about registering an event for $o.BalloonTipClicked as well as a lovely example in the wild. Basically, replace this:
$o.ShowBalloonTip(10000);Start-Sleep -M 12000;$o.Dispose();Exit
... with this:
$o.ShowBalloonTip(10000);register-objectevent $o BalloonTipClicked clicked;^
if (wait-event clicked -t 12) {$true} else {$false}; $o.Dispose(); Exit
You also need to execute powershell in a single threaded apartment for the event to work.
start /b "" "powershell" -STA %code%
Now, you need to figure out how to make that relevant back in the context of your batch process. For one thing, you'd probably no longer be able to use start /b to make the balloon tip non-blocking, and you'd probably use a for /F loop to capture the output of the powershell command.
Adding to your worries, I propose that "Shutting down in xx minutes" is not entirely user-friendly. What if "Shutting down in 30 minutes" appeared 29 minutes ago, but the user just now saw it? "Shutting down at 9:51 AM" might be better.
So with all this in mind, since what you want is event driven and since the batch language doesn't handle date-time math all that easily, I suggest doing the whole damn thing in PowerShell. Save this with a .ps1 extension. Right-click and run with PowerShell. Or if you want to execute it from a cmd console, do powershell ".\scriptname.ps1".
set-executionpolicy remotesigned
if ([threading.thread]::CurrentThread.GetApartmentState() -eq "MTA") {
& powershell.exe -window minimized -sta $MyInvocation.MyCommand.Definition
exit
}
[void] [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
$minutes = 30
$launch_time = (Get-Date).AddMinutes($minutes).ToShortTimeString()
$o = New-Object Windows.Forms.NotifyIcon
$o.Icon = "$env:SystemRoot\System32\PerfCenterCpl.ico"
$o.BalloonTipIcon = "Info"
$o.BalloonTipText = "Shutting down at $launch_time"
$o.BalloonTipTitle = "Shutdown pending..."
$o.Visible = 1
function show-balloon { $o.ShowBalloonTip($minutes * 60 * 1000) }
show-balloon
$o_hover = [Windows.Forms.MouseEventHandler]{ show-balloon }
$o.add_MouseMove($o_hover)
register-objectevent $o BalloonTipClicked clicked
if (wait-event clicked -t ($minutes * 60)) {
remove-event clicked
$o.BalloonTipText = "Have a nice day!"
$o.BalloonTipTitle = "Shutdown aborted"
$o.ShowBalloonTip(10000)
if (wait-event clicked -t 10) { remove-event clicked }
} else {
# Initiate shutdown sequence on my mark. Authorization rojo alpha 3. Mark.
stop-computer
}
unregister-event clicked
$o.Dispose()
Bill_Stewart, if you're reading this, I know you're pleased. As it happens, PowerShell is indeed the correct tool for the job this time.

Programmatically send key strokes to a window program in Groovy or bat script

Backstory: I need to programmatically find the differences between two files. I want to use WinMerge to generate a report (Tools -> Generate Report) that I can parse to get the differences between two files. I need this done using either a Groovy script or a bat script.
I was hoping that WinMerge would offer command line options to generate the report and then I could just use a Groovy Process object to execute WinMergeU.exe with the arguments. No such luck according to the command options I've found for WinMerge.
Next, I was hoping to be able to start WinMerge and send keystrokes to step through the menus to generate the report(Alt+T, R, Diff.html, [Enter]). I don't see a way to do that from a Groovy Process and I haven't found a way to do this in a bat script. I'm looking for something similar to WshShell.Sendkeys in VB. Is this a wild-goose chase?
UPDATE/Answer with PowerShell in a bat file:
I was intrigued by Knuckle-Dragger's comment about using a PowerShell script in a bat file.
$folder = "C:\DevTools\WinMerge\WinMergeU.exe"
ii $folder
Start-Sleep -m 1000
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[Microsoft.VisualBasic.Interaction]::AppActivate("WinMerge")
Start-Sleep -m 100
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
Start-Sleep -m 100
[System.Windows.Forms.SendKeys]::SendWait("%F")
[System.Windows.Forms.SendKeys]::SendWait("o")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("%T")
[System.Windows.Forms.SendKeys]::SendWait("r")
Start-Sleep -m 1000
[Microsoft.VisualBasic.Interaction]::AppActivate("Save As")
Start-Sleep -m 1000
[System.Windows.Forms.SendKeys]::SendWait("Diff.txt")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
To encapsulate this in a command window, save it to a file PowerShellScript.ps1:
start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta *PowerShellScript.ps1*
Here is a powershell example to activate winmerge and send some keys.
EDIT: Reduced copy pasta with some .NET variables. $SK = Sendkeys $AA = AppActivate $LRA = Reflect .NET.
$startapp = "C:\DevTools\WinMerge\WinMergeU.exe"
ii $startapp
$SK = "[System.Windows.Forms.SendKeys]::SendWait"
$AA = "[Microsoft.VisualBasic.Interaction]::AppActivate"
$LRA = "[void][System.Reflection.Assembly]::LoadWithPartialName"
Start-Sleep 1
$LRA+'("Microsoft.VisualBasic")'
$AA+'("WinMerge")'
Start-Sleep -m 100
$LRA+'("System.Windows.Forms")'
Start-Sleep -m 100
$SK+'("%F")'
$SK+'("o")'
$SK+'("{ENTER}")'
$SK+'("%T")'
$SK+'("r")'
Start-Sleep 1
$AA+'("Save As")'
Start-Sleep 1
$SK+'("Diff.txt")'
$SK+'("{ENTER}")'
To encapsulate this in a command window, save it to a file PowerShellScript.ps1: Note, changed the command syntax a bit, should work if you use the & {.\dot\source\path}
start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta -Command "& {.\PowerShellScript.ps1}"
This is something that I threw together without testing, and I don't have WinMerge so testing isn't really an option.
RunMe.bat
start "" "C:\Path\WinMerge.exe" "file1" "file2"
GenerateDiffFile.vbs
GenerateDiffFile.vbs
Set s = CreateObject("WScript.Shell")
wscript.sleep(1000) ' Sleep for 1 second to allow time for WinMerge to finish loading and merge the files
s.SendKeys("%tr")
wscript.sleep(250) ' Give time for dialog box to appear
s.SendKeys("Diff.html{Enter}")
This is completely untested, but I think it is very close to what you need... You can see that the batch-file runs WinMerge, passing the two files to merge on the command line. Then it launches the VBS script, which pauses long enough to allow the launched WinMerge to be able to accept keyboard input. I used this page from Microsoft to know what keys to send.
I've been sending keys to programs for years. It works very reliably, as long as the program you want to send keys to keeps focus.
All I do is this:
echo y | "Batchfile needing the keystroke"

Hide empty console window in a all GUI Powershell script?

I have made a very simple Powershell script with WinForms GUI.
Everything works as intended but, when I run the .ps1 script with PowerShell a black empty console window appears at first and then the GUI shows.
Anyway to make the console window dissapear?
Best regards
I wrote a small article on this subject (sorry in french) one year ago.
Here is the common solution using a small VBS script to start PowerShell hidding his window (the trick is in the last ,0).
Set Args = Wscript.Arguments
'MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0
I also embeded PowerShell in an executable with no console called slxPShell2.EXE.
I found the above didn't work for me. I used this:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False
Set objShell = Nothing
Hope that helps.
This solution Minimizes Powershell window after it starts. Powershell window opens, then disapears, without using any outside code. Put at beginning of your script.
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
This is how I got this working:
Have the Winforms GUI script in one ScriptOne.ps1 file
Create another LaunchScriptOne.ps1 file with the content:
powershell.exe -WindowStyle Hidden -File "C:\path\to\ScriptOne.ps1".
The solution was provided in another thread on the same topic: Hide or Minimize the powershell prompt after Winform Launch
I hope someone will find a way to put this into one single script as well. The answers above in this thread did not help me, but maybe I did something wrong, idk.
I'm nube so no rep so can't comment inline... though wrt #Ipse's solution which I'm a fan of, I also make sure to close the hidden window when the script is done... not sure if PS gets around to this sort of auto-garbage collection, but suspect it's good best practice.
eg. at end of your script I'd suggest doing:
stop-process -Id $PID
(which should terminate that hidden window v. just leave it lurking around and tying up those resources).

Resources