如何防止输入控件从TextCompositionManager中窃取空格字符?

4 wpf textinput textcompositionmanager

这个问题的相关(但不是愚蠢!): 帮助WPF TextCompositionManager事件

当使用TextCompositionManager时,我遇到了一个问题,即如果TextBox等输入控件具有焦点,TextBox将"窃取"空格字符,然后才能有机会对其进行操作.

例如,我们有以下代码:

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

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

Window1的样子:

<!--standard window crap above here-->
<StackPanel>
  <TextBlock Name="CaptureTextBlock" />
  <TextBox Name="ThievingBastard" />
</StackPanel>
<!-- snip -->
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行此应用程序并立即键入"我讨厌窃贼的混蛋",TextBlock将包含文本"我讨厌窃贼的混蛋",文本框将为空.

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


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


更新

我正在使用一个hacky解决方法,我只从InputManager处理隧道KeyDown事件.这种方法非常笨拙,效率低下,基本上很臭.仍在寻找更好的方法.

小智 6

为了别人的利益,我的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);
Run Code Online (Sandbox Code Playgroud)

这两种方法:

/// <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);
}
Run Code Online (Sandbox Code Playgroud)

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

所有其他键由PreviewTextInputHandler方法处理.

这段代码有很多东西被剥夺了.确定滑动事件何时发生,确定滑动何时完成,保护措施(如果我从未停止捕获滑动,则超时)等被删除.你如何做这些东西将取决于你的代码要求.