Tablet PC Input Panel does not cause TextInput event?
I have a custom control that consumes WPF TextInput events. This works fine when using the keyboard; however, if the hand-writing recognition of the "Tablet PC Input Panel" (comes with Windows 7) is used, no TextInput event occurs when the "Insert" button is clicked.
public partial class Window1 : Window
{
public Window1()
{
}
protected override void OnTextInput(TextCompositionEventArgs e)
{
base.OnTextInput(e);
this.Title = e.Text;
}
}
class Text : Control
{
static Text()
{
KeyboardNavigation.IsTabStopProperty.OverrideMetadata(
typeof(Text), new FrameworkPropertyMetadata(true));
KeyboardNavigation.TabNavigatio开发者_Python百科nProperty.OverrideMetadata(
typeof(Text), new FrameworkPropertyMetadata(KeyboardNavigationMode.None));
FocusableProperty.OverrideMetadata(
typeof(Text), new FrameworkPropertyMetadata(true));
}
public static readonly DependencyProperty EnteredTextProperty =
DependencyProperty.Register("EnteredText", typeof(string), typeof(Text),
new FrameworkPropertyMetadata());
public string EnteredText {
get { return (string)GetValue(EnteredTextProperty); }
set { SetValue(EnteredTextProperty, value); }
}
protected override void OnTextInput(TextCompositionEventArgs e)
{
this.EnteredText = e.Text;
e.Handled = true;
}
/// <inheritdoc/>
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
Focus();
}
}
Here's the XAML:
<Window x:Class="TestProject.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
>
<local:Text xmlns:local="clr-namespace:TestProject">
<Control.Template>
<ControlTemplate TargetType="local:Text">
<Border Background="Beige">
<Viewbox>
<TextBlock Text="{TemplateBinding EnteredText}"/>
</Viewbox>
</Border>
</ControlTemplate>
</Control.Template>
</local:Text>
</Window>
When the app is started, text input appears in the title bar. This works both with keyboard and handwriting recognition. Once you click within the window to give focus to the control, only the keyboard will work for input; the hand-writing recognition is ignored.
Does anyone know what is going wrong? Why do I get the TextInput event in one case but not in the other? Is this a bug in WPF? Is there a work around?
The control is losing properties of its focus after the insertion and doesn't get them back if you call Focus() on MouseDown event (because actually it is focussed and the OnFocus event is not released again).
If you focus an other control and focus the custom control back its focussed again and the TextInput event occurs (until you insert text from the tablet pc input again).
I can't explain what is happing, I think that could be a bug of WPF but maybe that information could help for a work around...
From what I can tell, when you click on the window, the text object loses focus and therefore will not capture events.
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
Focus();
}
It seems the reason why this isn't working is because it requires the text object to be clicked to focus the text class
What I would suggest is:
- Add an event handler for mousedown of the window to focus the text item
- Focus the text object when you type in the window
- Write to both the text object and the window title when keys are captured in the window
精彩评论