바로 가기에 문제가있어 도움이나 팁을 많이 주시면 감사하겠습니다. 목표 : 앱에서 수정자를 사용하거나 사용하지 않는 단축키를 처리 할 수 있어야합니다. 예를 들어, 'CTR + a'뿐만 아니라 'a'키도 처리해야합니다. 하지만 컨트롤을 이러한 키를 처리하는 경우에만 처리 할 싶어요. 예를 들어, TextBox 클래스는 'Ctrl + C'등과 같은 일부 명령을 포함하여 대부분의 키를 사용하므로 TextBox에서 처리 할 때 이러한 이벤트를 가로 채고 싶지 않습니다.바로 가기 및 키보드 이벤트를 올바르게 처리하는 방법은 무엇입니까?
Command를 사용하여 KeyUp에 이벤트를 연결하려고 시도했지만 TextBox에서 키를 사용하기 전에 키를 가로 채기 명령을했지만 TextBox에서 키를 사용한 경우에도 KeyDown이 Window 수준으로 버블되었습니다! 자식 컨트롤에 의해 처리되지 않는 키를 얻기 위해 내 윈도우를 얻으려면 어떻게해야합니까? 아래 코드는 저에게 맞지 않습니다. 또한 여러 컨트롤이 있기 때문에 차라리 "적절한"솔루션이 있습니다. 즉, 창에서 컨트롤의 각 인스턴스에 핸들러를 연결하지 않습니다.
그리고
<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"를 누르면 텍스트 상자에 내가 텍스트 만 이동에 '시간'을 원하는 경우에도 명령을 트리거에 대한 상자. 또한, 내 텍스트 상자, 모든 숫자 키를 누르면 KeyUp 이벤트를 발생, 비록 내가 이해할 수있는, 텍스트 상자를 처리 했어야 그 사실!
도움 주셔서 감사합니다.
+1 이것은 정말 WPF 초보자를위한 참조가 될 수있는 유용한 질문입니다. –