WPF-键盘和鼠标同时单击

时间:2020-07-05 16:38:45

标签: powershell

在PowerShell和WPF中是否可以通过键盘单击和鼠标单击触发事件? 例如,我的WPF中有以下按钮:

<button
    title={'foo'}
    onClick={(e) => myFunction(e.currentTarget.title)}
>
   click me
</button>

我想触发两个事件。使用标准的鼠标单击事件:

function myFunction(e) {
  alert(e); // e is the value that you have pass 
}

当我按住CTRL并在同一按钮上单击鼠标时,会发生一些事情。 我喜欢这样的键盘点击

<Button Name="button_test" Content="Test" Grid.Column="0" Grid.Row="4" />

但是我不知道如何结合这两个事件。甚至有可能吗?

谢谢

斯蒂芬

1 个答案:

答案 0 :(得分:0)

抱歉,迟到了。我用一个额外的变量“解决了”它,但是我不确定这是否是正确的方法,因此任何建议都将不胜感激。

$title = "test"

Add-Type -AssemblyName PresentationFramework

# GUI
[xml]$xaml = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window_GuiManagement" Title="$title" WindowStartupLocation = "CenterScreen" 
        Width = "Auto" Height = "350.000" Visibility="Visible" WindowStyle="ToolWindow" ResizeMode="NoResize" SizeToContent="WidthAndHeight" >
    <Grid>
        <DockPanel Margin="5">
            <StackPanel DockPanel.Dock="Bottom" >
                <Button Name="button_1" Content="Button 1" />
                <Button Name="button_2" Content="Button 2"  />
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>
"@

$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)

# Declare objects
$button_1 = $window.FindName("button_1")
$button_2 = $window.FindName("button_2")


$global:ctrl = $false

$button_1.Add_Click({ Write-Host "Button 1 clicked" })

$button_2.Add_Click({
        if($global:ctrl){
            Write-Host "do more" $ctrl
        } else {
            Write-Host "do standard" $ctrl
        }
})

$window.add_KeyDown{
    param
    (
      [Parameter(Mandatory)][Object]$sender,
      [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$e
    )
    #Write-Host $e.Key
    if($e.Key -eq "LeftCtrl")
    {
        return ($global:ctrl = $true)
    }
}

$window.add_KeyUp{
    param
    (
      [Parameter(Mandatory)][Object]$sender,
      [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$e
    )
    #Write-Host $e.Key
    if($e.Key -eq "LeftCtrl")
    {
        return ($global:ctrl = $false)
    }
}

$Window.ShowDialog() | Out-Null