6

与此问题相关(但不是骗子!): Help with the WPF TextCompositionManager events

在使用TextCompositionManager时,我遇到了一个问题,如果 TextBox 等输入控件具有焦点,则 TextBox 会在我有机会对其采取行动之前“窃取”空格字符。

例如,我们有以下代码:

public Window1()
{
  TextCompositionManager.AddPreviewTextInputStartHandler
    (this, PreviewTextInputStartHandler);
}

private void PreviewTextInputHandler(object sender, TextCompositionEventArgs e)
{
  CaptureTextBlock.Text += e.Text;
  e.Handled = true;
}

其中 Window1 看起来像:

<!--standard window crap above here-->
<StackPanel>
  <TextBlock Name="CaptureTextBlock" />
  <TextBox Name="ThievingBastard" />
</StackPanel>
<!-- snip -->

现在,如果我运行这个应用程序并立即输入“我喜欢小偷”,TextBlock 将包含文本“我喜欢小偷”,并且文本框将为空。

但是,如果我关注文本框(即文本框具有键盘焦点),则在键入上述行后,文本块将包含文本“Ihaetthievingbastards”,文本框将包含文本“”(3 个空格)。


我有两个问题:
1) 我可以仅使用 TextCompositionManager 提供的工具来防止这种情况发生吗?
2)如果不是,我到底应该在哪里插入文本输入堆栈,以便我可以完全控制我的 WPF 应用程序中的文本输入(甚至考虑 p/invoking 的负面点)(你只是想了一下,增加了负面点) ?


更新

我正在使用一种 hacky 解决方法,我只为空间处理来自 InputManager 的隧道 KeyDown 事件。这种方法非常笨拙,效率低下,而且基本上很臭。还在寻找更好的方法。

4

1 回答 1

8

为了他人的利益,我的hacky代码。

我的特定应用程序正在等待读卡器刷卡。以下内容存在于监视刷卡的对象的构造函数中(这是一个附带项目;大多数评论都被删除了):

// this is where we handle the space and other keys wpf f*s up.
System.Windows.Input.InputManager.Current.PreNotifyInput += 
    new NotifyInputEventHandler(PreNotifyInput);
// This is where we handle all the rest of the keys
TextCompositionManager.AddPreviewTextInputStartHandler(
    Application.Current.MainWindow, 
    PreviewTextInputHandler);

两种方法:

/// <summary>
/// Handles the PreNotifyInput event of the input manager.
/// </summary>
/// <remarks>Because some controls steal away space (and other) characters, 
/// we need to intercept the space and record it when capturing.</remarks>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The 
/// <see cref="System.Windows.Input.NotifyInputEventArgs"/> 
/// instance containing the event data.</param>
private void PreNotifyInput(object sender, NotifyInputEventArgs e)
{
    // I'm only interested in key down events
    if (e.StagingItem.Input.RoutedEvent != Keyboard.KeyDownEvent)
        return;
    var args = e.StagingItem.Input as KeyEventArgs;
    // I only care about the space key being pressed
    // you might have to check for other characters
    if (args == null || args.Key != Key.Space)
        return;
    // stop event processing here
    args.Handled = true;
    // this is my internal method for handling a keystroke
    HanleKeystroke(" ");
}


/// <summary>
/// This method passes the event to the HandleKeystroke event and turns
/// off tunneling depending on whether or not Capturing is true.
/// Also calls StopCapturing when appropriate.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The 
/// <see cref="System.Windows.Input.TextCompositionEventArgs"/> 
/// instance containing the event data.</param>
private void PreviewTextInputHandler(object sender, 
    TextCompositionEventArgs e)
{
    HanleKeystroke(e.Text);
}

当有人按下一个键(或一个键击被发送到系统)时,PreNotifyInput 事件就会触发。在这种情况下,我确定它是否是特殊键(对我来说我必须担心空间,但其他键显然需要特别注意)。如果它是一个特殊键,我会“处理”该事件,停止对该击键的所有进一步处理。然后我调用传入空间的内部处理方法(或我刚刚截获的任何特殊键)。

所有其他键都由 PreviewTextInputHandler 方法处理。

这段代码去掉了很多东西。确定滑动事件何时发生、确定滑动何时完成、安全措施(超时以防我从不停止捕获滑动)等被删除。你如何做这些事情将取决于你的代码要求。

于 2009-10-02T13:37:34.643 回答