ショートカットに問題がありましたら、ヘルプ/ヒントをいただければ幸いです! 目的:私のアプリでは、修飾キーの有無にかかわらず、ショートカットキーを処理できる必要があります。 例えば、私はキー 'a'と 'CTR + a'を処理する必要があります。しかし、これらのキーを制御するコントロールがない場合にのみ、それらを処理したいと思います。例えば、TextBoxクラスでは、 'Ctrl + C'などのコマンドを含むほとんどのキーが使用されるため、TextBoxがそれらを処理するときにこれらのイベントをインターセプトしたくありません。ショートカットとキーボードイベントを正しく処理する方法は?
KeyUpにイベントをWindowに添付するだけでなく、コマンドを使用しようとしましたが、TextBoxがキーを使用する前にKeyBoxがそれらを表示する前にキーを傍受します。任意の子コントロールで処理されないキーを取得するためにウィンドウを取得するにはどうすればよいですか?私のためにはうまくいかなかった以下のコードを見てください。また、私は多くの異なるコントロールを持っているので、私はむしろ "適切な"ソリューションを持っています:私はむしろウィンドウ内のコントロールの各インスタンスにハンドラーをアタッチしません。
そして
<Window x:Class="KeyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="Help"
CanExecute="HelpCanExecute"
Executed="HelpExecuted" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="Help" Key="H" />
</Window.InputBindings>
<Grid>
<WrapPanel>
<TextBox Name="myLOG" Width="300" Height="200" Background="LightBlue" />
<TextBox Name="myINPUT" Width="300" Height="200" />
<Button Content="JUST FOR FUN" />
</WrapPanel>
</Grid>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace KeyTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
myLOG.Text += "HELP CAN EXECUTE\n";
e.CanExecute = true;
e.Handled = true;
}
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
myLOG.Text += "HELP EXECUTED!!!\n";
e.Handled = true;
}
public void myKeyUpHandler(Object sender, KeyEventArgs args)
{
myLOG.Text += "KEY UP EVENT! " + args.Key + "\n";
}
public MainWindow()
{
InitializeComponent();
this.KeyUp += new KeyEventHandler(myKeyUpHandler);
}
}
}
焦点は "h" を押すと、テキストボックスにある、私はテキストのみに行くに 'h' をしたいにもかかわらず、コマンドをトリガするためにボックス。また、テキストボックスの中で、アルファベットの数字キーを押すと、たとえわかっていても、TextUpがそのイベントを処理していたはずですが、KeyUpイベントが発生します。
ありがとうございました!
+1これは本当にWPF初心者の参考になる参考になる質問です。 –