Call 'Enter' key to press a button

Welcome Forums General PowerShell Q&A Call 'Enter' key to press a button

This topic contains 2 replies, has 3 voices, and was last updated by

1 week ago.

  • Author
  • #182142

    Topics: 6
    Replies: 11
    Points: 97
    Rank: Member


    I've built a script for remote computer.

    the remote session start after input the IP and then pressing 'Start' button (with the mouse).

    i want to perform a click on the start button when pressing Enter key.

    searching on the network found a few options like add_keydown option but i couldn't figure how to integrate it in my script.

    i would really appreciate your help.

    the script:


    # Load required assemblies
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    # Drawing form and controls
    $Form_RemoteControl = New-Object System.Windows.Forms.Form
        $Form_RemoteControl.Text = "Remote control"
        $Form_RemoteControl.Size = New-Object System.Drawing.Size(300,150)
        $Form_RemoteControl.FormBorderStyle = "FixedDialog"
        $Form_RemoteControl.TopMost = $true
        $Form_RemoteControl.MaximizeBox = $false
        $Form_RemoteControl.MinimizeBox = $true
        $Form_RemoteControl.ControlBox = $true
        $Form_RemoteControl.backcolor = "LightSteelBlue"
        $Form_RemoteControl.StartPosition = "CenterScreen"
        $Form_RemoteControl.Font = "Segoe UI"
    # adding a label Computer
    $label_RemoteControl = New-Object System.Windows.Forms.Label
        $label_RemoteControl.Location = New-Object System.Drawing.Size(5,10)
        $label_RemoteControl.Size = New-Object System.Drawing.Size(70,20)
        $label_RemoteControl.Font = New-Object System.Drawing.Font("Segoe UI",9,[System.Drawing.FontStyle]::Regular)
        $label_RemoteControl.TextAlign = "middlecenter"
        $label_RemoteControl.Text = "Computer:"
    #add text box
    $textBox_IP = New-Object System.Windows.Forms.TextBox
        $textBox_ip.Location = New-Object System.Drawing.Point(75,10)
        $textBox_ip.Size = New-Object System.Drawing.Size(189,20)
    #add Start button
    $button_start = New-Object System.Windows.Forms.Button
        $button_start.Location = New-Object System.Drawing.Size(25,70)
        $button_start.Size = New-Object System.Drawing.Size(240,32)
        $button_start.TextAlign = "MiddleCenter"
        $button_start.font = New-Object System.Drawing.Font("Segoe UI",14,[System.Drawing.FontStyle]::Regular)
        $button_start.BackColor = "seashell"
        $button_start.Text = "Start"
    #add statement
        while ((Get-Service -ComputerName $textBox_IP.text -ServiceName servicename).Status -ne "Running") {
        # Pause before next check
        Start-Sleep -Seconds 1
    #only then..
            Start-Process -FilePath "C:\Users\user\file.exe" -verb RunAs -ArgumentList $textBox_IP.text
    # show form
    [void] $Form_remoteControl.ShowDialog()
  • #182574

    Topics: 0
    Replies: 8
    Points: 54
    Rank: Member

    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.

  • #182586

    Topics: 2
    Replies: 1000
    Points: 1,956
    Helping Hand
    Rank: Community Hero

    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.

    How to Add a PowerShell GUI Event Handler (Part 1)

    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"


    #Add Button event
    [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")

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

    } #End Function

    #Call the Function

You must be logged in to reply to this topic.