1

What I would like to do is to assign a button to a shortcut key, for example $TurnOnButton as key "Q", and $TurnOffButton as key "W" on my keyboard.

So basically, in my example below. When the script is running and the form is present, I would be able to push button "Q" on my keyboard to run calculator, and press button "W" to terminate it.

Is this possible with PowerShell?

Code example:

Add-Type -AssemblyName System.Windows.Forms

function Return-TurnOff
{
    $x = Stop-Process -ProcessName calc
    $x
}

function Return-TurnOn
{
    $x = Start-Process calc
    $x
}

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Title of the form"
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.minimumSize = New-Object System.Drawing.Size(300,200) 
$form.maximumSize = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen"

$TurnOffButton = New-Object System.Windows.Forms.Button
$TurnOffButton.Location = New-Object System.Drawing.Point(10,125)
$TurnOffButton.Size = New-Object System.Drawing.Size(55,25)
$TurnOffButton.Text = "Turn Off"
$TurnOffButton.Add_Click({Return-TurnOff})
$form.AcceptButton = $TurnOffButton
$form.Controls.Add($TurnOffButton)

$TurnOnButton = New-Object System.Windows.Forms.Button
$TurnOnButton.Location = New-Object System.Drawing.Point(10,65)
$TurnOnButton.Size = New-Object System.Drawing.Size(55,25)
$TurnOnButton.Text = "Turn On"
$TurnOnButton.Add_Click({Return-TurnOn})
$form.AcceptButton = $TurnOnButton
$form.Controls.Add($TurnOnButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(215,125)
$CancelButton.Size = New-Object System.Drawing.Size(55,25)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,15) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Label for textbox:"
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,35) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

1 Answer 1

2

If you don't mind having the label on the button contain the letter that corresponds to the button AND pressing the Alt key, then it's very easy, just put an '&' before the letter in the button text you want to be the accelerator:

$TurnOnButton.Text = "Turn O&n"
$TurnOffButton.Text = "Turn O&ff"

would make Alt-N perform Turn On and Alt-F perform Turn Off.

A more complicated solution is to register for keyboard presses, but it will let you handle any keystroke whether or not Alt is pressed. $form|gm -MemberType event key* will show you the events whose name starts with "Key". You can then google for how to handle events from Powershell with WinForms.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks user2460798! I honestly didn't even think of adding an Alt-shortcut to the buttons. I will explore that other option later as it is more what I am looking for however, but I will stick to your Alt-solution for now. Thanks a lot. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.