Forms 使用Click事件按下键
我在写一份表格时遇到了一些困难。我有一个按钮,用是/否框提示用户,我想添加一些功能,如果用户在单击按钮时按住shift键,则可以绕过提示。以下是我在点击事件Forms 使用Click事件按下键,forms,winforms,powershell,events,Forms,Winforms,Powershell,Events,我在写一份表格时遇到了一些困难。我有一个按钮,用是/否框提示用户,我想添加一些功能,如果用户在单击按钮时按住shift键,则可以绕过提示。以下是我在点击事件scriptblock中尝试过的内容,但似乎没有任何效果: if($_.KeyCode -eq 'Shift'){ #Stuff } if($_.Shift){ #Stuff } 你知道我能做些什么来让它工作吗?单击事件没有键码。以下内容适用于PowerShell v2-在更
scriptblock
中尝试过的内容,但似乎没有任何效果:
if($_.KeyCode -eq 'Shift'){
#Stuff
}
if($_.Shift){
#Stuff
}
你知道我能做些什么来让它工作吗?单击事件没有键码。以下内容适用于PowerShell v2-在更高版本中可能有更简单的方法
function Get-KeyState([uint16]$keyCode)
{
$signature = '[DllImport("user32.dll")]public static extern short GetKeyState(int nVirtKey);'
$type = Add-Type -MemberDefinition $signature -Name User32 -Namespace GetKeyState -PassThru
return [bool]($type::GetKeyState($keyCode) -band 0x80)
}
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$button = New-Object System.Windows.Forms.Button
$button.Text = 'hi'
$Form.Controls.Add($button)
$button.add_Click(
{
$VK_SHIFT = 0x10
$ShiftIsDown = (Get-KeyState($VK_SHIFT))
if ($ShiftIsDown){
[System.Windows.Forms.MessageBox]::Show("Hi, you clicked the button with shift." ,"My Dialog Box")
}
else{
[System.Windows.Forms.MessageBox]::Show("Hi, you clicked the button without shift." ,"My Dialog Box")
}
}
)
$Form.ShowDialog()
PS v3有更好的解决方案吗?