Touch keyboard hides in Tablet mode when override AutomationPeer with FrameworkElementAutomationPeer in WPF

Niranjan Kumar Gopalan 56 Reputation points
2021-07-13T10:54:25.217+00:00

Hi ,

When I create a new control which is inherited from WPF TextBox and override it's AutomationPeer with FrameworkElementAutomationPeer , the touch keyboard hides when focused or clicked inside the new control. I have attached the code below, it will be really helpful if anyone can provide a suitable solution for this issue.

 public class NewTextBox : TextBox, IDisposable
    {
        public void Dispose()
        {

        }

        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new NewAutomationPeer(this);
        }
    }

    public class NewAutomationPeer : FrameworkElementAutomationPeer
    {
        public NewAutomationPeer(NewTextBox owner) : base(owner)
        {

        }
    }

Note : Touch keyboard opens and works fine when I override the GetAutomationControlTypeCore method and return AutomationType.Edit. But I want to return the AutomationType.Custom (since my new control is custom control) and it does not work fine. Please consider this scenario while providing solution.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2021-07-19T04:03:47.73+00:00

    You could automatically open the Touch keyboard by starting taptix.exe. You can try to refer to the following code.
    The code of MainWindow.xaml is as follows:

    <local:NewTextBox Width="100" Height="30" Background="LightGreen" ></local:NewTextBox>  
    

    The code of NewTextBox is as follows:

    public class NewTextBox : TextBox  
      {  
        
        public NewTextBox()  
        {  
          this.GotFocus += TextBox_GotFocus;  
        }  
      
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)  
        {  
          RegistryKey registryKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\TabletTip\\1.7");  
      
          registryKey?.SetValue("KeyboardLayoutPreference", 0, RegistryValueKind.DWord);  
          registryKey?.SetValue("LastUsedModalityWasHandwriting", 1, RegistryValueKind.DWord);  
      
          Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");  
        }  
      }  
    

    The result is shown in the figure:

    115742-1.gif

    0 comments No comments

  2. Michal_1982_PL 1 Reputation point
    2022-07-12T11:26:34.307+00:00

    Hi @Niranjan Kumar Gopalan did you manage to show/hide the Touch Keyboard in your WPF application? I am developing similar application and looking for best solutions.

    0 comments No comments