Winforms 使用Powershell和Windows窗体。有没有一种不用文本框就能获得按键输入的方法

Winforms 使用Powershell和Windows窗体。有没有一种不用文本框就能获得按键输入的方法,winforms,powershell,Winforms,Powershell,我正在使用Powershell和Windows窗体。有没有一种方法可以在不使用文本框的情况下获得按键输入 对于一个文本框,我知道你可以做如下操作 $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$newid=$objTextBox.Text;$objForm.Close()}}) #VARIABLE CAPTURE $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") Power

我正在使用Powershell和Windows窗体。有没有一种方法可以在不使用文本框的情况下获得按键输入

对于一个文本框,我知道你可以做如下操作

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$newid=$objTextBox.Text;$objForm.Close()}})  #VARIABLE CAPTURE
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 

Powershell提供读取主机,该主机将从Powershell提示符读取


$newid=Read Host

Powershell提供了Read Host,它将从Powershell提示符中读取


$newid=Read Host

可能您试图创建一个完全没有控件的表单,或者在焦点控件收到按下的键之前键入键返回特定值

在这种情况下,我将限定接收输入的变量的范围,使其至少具有如下范围:
$script:FormResult

作为演示,它是一个没有其他控件的小表单,只有一个标签,显示用户可以通过按键选择的一些操作

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select option"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
# with this, the form will receive key events before the event is passed to the control that has focus.
$objForm.KeyPreview = $true

$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,20) 
$Label.AutoSize = $true
$label.Text = @"
Select an action to perform.

1. Perform action 1
2. Perform action 2
3. Perform action 3

Press Escape to cancel
Press Enter to perform all actions
"@

$objForm.Controls.Add($Label)

$objForm.Add_KeyDown({
    $script:FormResult = $null
    switch ($_.KeyCode) {
        {'Enter','Return' -contains $_} { $script:FormResult = 'AllActions' ; break }
        {'D1', 'NumPad1' -contains $_}  { $script:FormResult = 'Action1'; break }
        {'D2', 'NumPad2' -contains $_}  { $script:FormResult = 'Action2'; break }
        {'D3', 'NumPad3' -contains $_}  { $script:FormResult = 'Action3'; break }
        'Escape'                        { $script:FormResult = 'Cancel' }
    }
    if ($script:FormResult) { $objForm.Close() }
})

[void] $objForm.ShowDialog()

# clean up the form from memory when done
$objForm.Dispose()
在此之后,您可以使用变量
$script:FormResult


希望这会有所帮助

也许您正在尝试创建一个完全没有控件的表单,或者在焦点控件收到按下的键之前键入一个键返回一个特定值

在这种情况下,我将限定接收输入的变量的范围,使其至少具有如下范围:
$script:FormResult

作为演示,它是一个没有其他控件的小表单,只有一个标签,显示用户可以通过按键选择的一些操作

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select option"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
# with this, the form will receive key events before the event is passed to the control that has focus.
$objForm.KeyPreview = $true

$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,20) 
$Label.AutoSize = $true
$label.Text = @"
Select an action to perform.

1. Perform action 1
2. Perform action 2
3. Perform action 3

Press Escape to cancel
Press Enter to perform all actions
"@

$objForm.Controls.Add($Label)

$objForm.Add_KeyDown({
    $script:FormResult = $null
    switch ($_.KeyCode) {
        {'Enter','Return' -contains $_} { $script:FormResult = 'AllActions' ; break }
        {'D1', 'NumPad1' -contains $_}  { $script:FormResult = 'Action1'; break }
        {'D2', 'NumPad2' -contains $_}  { $script:FormResult = 'Action2'; break }
        {'D3', 'NumPad3' -contains $_}  { $script:FormResult = 'Action3'; break }
        'Escape'                        { $script:FormResult = 'Cancel' }
    }
    if ($script:FormResult) { $objForm.Close() }
})

[void] $objForm.ShowDialog()

# clean up the form from memory when done
$objForm.Dispose()
在此之后,您可以使用变量
$script:FormResult


希望这有帮助

你想做什么?我们没有收到你的来信。。给出的答案解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易找到它。你想做什么?我们没有收到你的来信。。给出的答案解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易找到它…但这与Windows窗体无关…但这与Windows窗体无关