5

Ctrl+などのショートカット キーOが WPF で (特定のコントロールとは関係なく) 押されたことを検出するにはどうすればよいですか?

キャプチャを試みKeyDownましたが、ダウンしているかどうかKeyEventArgsがわかりません。ControlAlt

4

2 に答える 2

11
private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyboardDevice.Modifiers == ModifierKeys.Control)
    {
        // CTRL is down.
    }
}
于 2009-05-06T23:10:19.200 に答える
1

XAMLのコマンドでこれを行う方法をついに理解しました。残念ながら、カスタム コマンド名 (ApplicationCommands.Open などの定義済みコマンドのいずれでもない) を使用する場合は、コード ビハインドで次のように定義する必要があります。

namespace MyNamespace {
    public static class CustomCommands
    {
        public static RoutedCommand MyCommand = 
            new RoutedCommand("MyCommand", typeof(CustomCommands));
    }
}

XAMLは次のようになります...

<Window x:Class="MyNamespace.DemoWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace"
    Title="..." Height="299" Width="454">
    <Window.InputBindings>
        <KeyBinding Gesture="Control+O" Command="local:CustomCommands.MyCommand"/>
    </Window.InputBindings>
    <Window.CommandBindings>
        <CommandBinding Command="local:CustomCommands.MyCommand" Executed="MyCommand_Executed"/>
    </Window.CommandBindings>
</Window>

もちろん、ハンドラーが必要です。

private void MyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
    // Handle the command. Optionally set e.Handled
}
于 2009-05-07T17:10:19.430 に答える