Related (but not a dupe!) to this question: Help with the WPF TextCompositionManager events
When using the TextCompositionManager I'm having an issue where, if an input control such as the TextBox has focus, the TextBox will "steal" the space character before I can get a chance to act on it.
For example, we have the following code:
public Window1()
{
TextCompositionManager.AddPreviewTextInputStartHandler
(this, PreviewTextInputStartHandler);
}
private void PreviewTextInputHandler(object sender, TextCompositionEventArgs e)
{
CaptureTextBlock.Text += e.Text;
e.Handled = true;
}
where Window1 looks like:
<!--standard window crap above here-->
<StackPanel>
<TextBlock Name="CaptureTextBlock" />
<TextBox Name="ThievingBastard" />
</StackPanel>
<!-- snip -->
Now, if I run this application and immediately type "I haet thieving bastards", the TextBlock will contain the text "I haet thieving bastards" and the textbox will be empty.
If, however, I focus the textbox (i.e., textbox has keyboard focus), after typing the above line the textblock will contain the text "Ihaetthievingbastards" and the textbox will contain the text " " (3 spaces).
I have two questions:
1) Can I prevent this from happening with just the facilities provided by the TextCompositionManager?
2) If not, where the hell do I plug into the text input stack so that I can fully control text input within my WPF application (negative points for even thinking about p/invoking) (you just thought about it, negative points added)?
Update
I'm using a hacky workaround where I handle the tunneling KeyDown event from the InputManager for spaces only. This method is very awkward, inefficient and basically stinks. Still looking for a better way.
For the benefit of others, my hacky code.
My particular app is waiting for a card swipe from a card reader. The following lives in the constructor of the object that watches for a card swipe (this is a side project; most comment cursing edited out):
The two methods:
When somebody presses a key (or a keystroke is sent to the system) the PreNotifyInput event fires. In this case, I determine if it is a special key (for me I have to worry about the space, but other keys apparently need special attention). If it is a special key, I "handle" the event, stopping all further processing of this keystroke. I then call my internal processing method passing in the space (or whatever special key I just intercepted).
All other keys are handled by the PreviewTextInputHandler method.
This code has a lot of stuff stripped out of it. Determining when a swipe event has happened, determining when the swipe has completed, safeguards (timeouts in case I never stop capturing a swipe), etc is removed. How you do this stuff will depend on your code requirements.