Ctrl+などのショートカット キーOが WPF で (特定のコントロールとは関係なく) 押されたことを検出するにはどうすればよいですか?
キャプチャを試みKeyDownましたが、ダウンしているかどうかKeyEventArgsがわかりません。ControlAlt
Ctrl+などのショートカット キーOが WPF で (特定のコントロールとは関係なく) 押されたことを検出するにはどうすればよいですか?
キャプチャを試みKeyDownましたが、ダウンしているかどうかKeyEventArgsがわかりません。ControlAlt
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
// CTRL is down.
}
}
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
}