我有一个带有文本框的窗口。光标位于文本框内。如果我按下一个键,那么我会在 WndProc 中收到一条消息(针对 KeyUp 和 KeyDown)。但是如果我在 KeyUp 和 KeyDown 事件中设置 e.Handled = true ,那么我不会收到任何关键消息:
public partial class MainWindow : Window
{
public MainWindow()
{
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var textBox = new TextBox();
textBox.KeyDown += TextBox_KeyDown;
textBox.KeyUp += TextBox_KeyUp;
Content = textBox;
(PresentationSource.FromVisual(this) as HwndSource).AddHook(WndProc);
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
e.Handled = true;
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
Debug.WriteLine(msg + " " + wParam);
return IntPtr.Zero;
}
}
是否可以在 WndProc 中接收 PreviewKeyDown/PreviewKeyUp 事件?