Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tablet PC Input Panel does not cause TextInput event?

Tags:

c#

textinput

wpf

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.TabNavigationProperty.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?

like image 247
Daniel Avatar asked Nov 18 '10 21:11

Daniel


2 Answers

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...

like image 187
zee Avatar answered Nov 15 '22 00:11

zee


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:

  1. Add an event handler for mousedown of the window to focus the text item
  2. Focus the text object when you type in the window
  3. Write to both the text object and the window title when keys are captured in the window
like image 30
Qwerty01 Avatar answered Nov 15 '22 01:11

Qwerty01