Call 'Enter' key to press a button

Can be closed. no solution found

A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.

Set the focus using the .Focus() method of the button object.

This is al generic GUI form design specifics, but not a PowerShell specific thing. WPF/Win, etc GUI (model, SDI, MDI) all have the same kinds of requirements. With all fully documented in the MS docs and other GUI design sites. YOutue is also your friend. https://www.youtube.com/results?search_query=powershell+gui+desing

How to Add a PowerShell GUI Event Handler (Part 1) https://social.technet.microsoft.com/wiki/contents/articles/25911.how-to-add-a-powershell-gui-event-handler-part-1.aspx

This method puts the event handler inside the parentheses of the $Button.Add_Click call. This method works, but from coding point of view it is not the preferred way, as it make it difficult to find, as the number of controls on the form increases and the complexity grows. Relevant parts are in bold.


Function Generate-Form {

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Build Form

$Form = New-Object System.Windows.Forms.Form
$Form.Text = “My Form”
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = “CenterScreen”
$Form.Topmost = $True

Add Button

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = “Show Dialog Box”

$Form.Controls.Add($Button)

#Add Button event
$Button.Add_Click(
{
[System.Windows.Forms.MessageBox]::Show(“Hello World.” , “My Dialog Box”)
}
)

#Show the Form
$form.ShowDialog()| Out-Null

} #End Function

#Call the Function
Generate-Form