32

For our WPF application, when it runs on touch screen(Surface Pro .etc), the TextBox/PasswordBox control cannot show virtual keyboard when they get focused.

Any good way to implement this feature in WPF?


Updated:

what we want to achieve finally is something like that:

If user run app on PC, we don't care about this feature, which means whether user has physical keyboard, we do nothing just like normal WPF application running on PC.

If user run on Surface Pro, when he clicks the TextBox, the built-in virtual keyboard can show up, and which should be user-friendly, such as the keyboard would never cover up the input element.


Updated 2:

So, WPF cannot easily set some property to implement this feature? In my opinion, this feature should be built-in WPF, I don't understand why I cannot find an easy way to achieve.

11
  • Do you already have a virtual keyboard? If so, which one? If not, I understand, you need to take the focus to TextBlock, show the virtual keyboard. Oct 5, 2013 at 17:59
  • How are users without a physical keyboard going to enter text into your application?
    – Eric Brown
    Oct 6, 2013 at 2:18
  • 2
    @AnatoliyNikolaev In this stage, we don't have a custom virtual keyboard and we think if WPF offers the feature, we can save a lot of work.
    – user1031200
    Oct 6, 2013 at 3:18
  • 1
    @EricBrown Hi, what we want to achieve finally is something like that: If user run app on PC, we don't care about this feature, which means whether user has physical keyboard, we do nothing just like normal WPF application running on PC. If user run on Surface Pro, when he clicks the TextBox, the built-in virtual keyboard can show up, and which should be user-friendly, such as the keyboard would never cover up the input element.
    – user1031200
    Oct 6, 2013 at 3:25
  • I'm still confused, then. There are more tablets than Surface Pro. Is the distinguishing characteristic the presence of a physical keyboard?
    – Eric Brown
    Oct 6, 2013 at 5:31

8 Answers 8

31
+50

Try this,

First check for a physical keyboard presence:

KeyboardCapabilities keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities();
return  keyboardCapabilities.KeyboardPresent != 0 ? true : false;

If you do not find a physical keyboard, use the in-built virtual keyboard of windows:

Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) + Path.DirectorySeparatorChar + "osk.exe");

Got help from here: link 1 link 2

6
  • 1
    So WPF cannot easily achieve this by setting some property or very few other settings naturally? Actually we also post another question related osk.exe stuff(stackoverflow.com/questions/19088686/…), in general we don't think it's a good solution since we have to control a lot of stuff to meet user friendly design.
    – user1031200
    Oct 8, 2013 at 8:34
  • This is really the only way to get the behaivor you want. If you want the current Input element to be in visual state, you could resize the CurrentWindow in order to not overlay the on-screen keyboard. (In fact creating a custom textbox, with the described feature is really easy and works smothlessly. Be sure to check if the OS is W8 before you open the keyboard.
    – Florian
    Oct 9, 2013 at 12:09
  • 10
    This namespace is only present in the WinRT framework. Is there an equivalent for plain old .Net WPF? Jan 21, 2015 at 11:38
  • How would you set the position of the keyboard?
    – lahjaton_j
    Apr 19, 2016 at 8:55
  • 1
    i think "TabTip.exe" is usually better than "osk.exe"
    – 00jt
    Dec 31, 2018 at 4:39
7

I've published a sample on how to trigger the touch keyboard in WPF applications when a user clicks into a Textbox, its here:

http://code.msdn.microsoft.com/Enabling-Windows-8-Touch-7fb4e6de

It has been something I've been working on for many months, i'm glad to finally contribute this example to our community. Please let me know if there are any questions, suggestions, problems, etc in the sample Q&A pane

1
  • 3
    Works pretty well but it disables scrolling controls like ScrollViewer. I ended up launching tabtip.exe on GotFocus using EventManager.RegisterClassHandler().
    – LMeyer
    Jan 24, 2014 at 10:34
5

I created a library to automate everything concerning TabTip integration in WPF app.

You can get it on nuget, and after that all you need is a simple config in your apps startup logic:

TabTipAutomation.BindTo<TextBox>();

You can bind TabTip automation logic to any UIElement. Virtual Keyboard will open when any element of specified type will get focus, and it will close when element will lose focus. Not only that, but TabTipAutomation will move UIElement (or Window) into view, so that TabTip will not block focused element.

For more info refer to the project site.

2
  • Use namespace using System.Windows.Controls; Feb 24, 2021 at 14:59
  • 1
    I had to add these too to make it work; System.Reactive.Linq System.Reactive.Core System.Reactive.Interfaces System.Reactive.Windows.Threading Feb 24, 2021 at 15:08
3

This solution is very simple: http://code.msdn.microsoft.com/windowsapps/Enabling-Windows-8-Touch-7fb4e6de

The steps are detailed in the link above, here is the short version:

  • Add a UIAutomationClient reference
  • Use IFrameworkInputPane from managed code (DLL at link or convert inputpanelconfiguration.idl to DLL, see steps below)
  • Create new class InkInputHelper to disable inking support (code below)
  • Call InkInputHelper.DisableWPFTabletSupport(); from MainWindow constructor or similar
  • Add using System.Windows.Interop;
  • Add to MainWindow_Loaded or similar:

        System.Windows.Automation.AutomationElement asForm =
        System.Windows.Automation.AutomationElement.FromHandle(new WindowInteropHelper(this).Handle);
        InputPanelConfigurationLib.InputPanelConfiguration inputPanelConfig = new InputPanelConfigurationLib.InputPanelConfiguration();
        inputPanelConfig.EnableFocusTracking();
    

To convert inputpanelconfiguration.idl to DLL

On Windows 8.1: c:\Program Files (x86)\Windows Kits\8.1\Include\um\inputpanelconfiguration.idl

To build a DLL from the IDL use the following steps:

  • Start a command prompt
  • Use the MIDL compiler tool to build a Type Library TLB file
    • Example: midl /tbld {filename}
  • Use the TLBIMP tool to convert the above generated Type Library (TLB file) into a DLL that .NET can use by running the following command
    • Example: TLBIMP.exe InputpanelConfiguration.tlb /publickey:{pathToKey} /delaysign

InkInputHelper class:

using System;
using System.Reflection;
using System.Windows.Input;

namespace ModernWPF.Win8TouchKeyboard.Desktop
{
public static class InkInputHelper
{
    public static void DisableWPFTabletSupport()
    {
        // Get a collection of the tablet devices for this window.  
        TabletDeviceCollection devices = System.Windows.Input.Tablet.TabletDevices;

        if (devices.Count > 0)
        {
            // Get the Type of InputManager.
            Type inputManagerType = typeof(System.Windows.Input.InputManager);

            // Call the StylusLogic method on the InputManager.Current instance.
            object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
                        BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                        null, InputManager.Current, null);

            if (stylusLogic != null)
            {
                //  Get the type of the stylusLogic returned from the call to StylusLogic.
                Type stylusLogicType = stylusLogic.GetType();

                // Loop until there are no more devices to remove.
                while (devices.Count > 0)
                {
                    // Remove the first tablet device in the devices collection.
                    stylusLogicType.InvokeMember("OnTabletRemoved",
                            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                            null, stylusLogic, new object[] { (uint)0 });
                }
            }
        }
    }
}
}

This should work. Again, much better info and a downloadable sample in the link. I only copy pasted the basics for archiving purposes.

2

When targeting .Net 4.6.2+ you don't need to do anything:

https://riptutorial.com/wpf/example/23104/showing-touch-keyboard-on-windows-8-and-windows-10

10
  • 1
    @00jt interesting. Do you have the latest Windows 10 version? (1809)
    – itsho
    Dec 31, 2018 at 16:16
  • 1
    @00jt 10.0.17134 is Version 1803 (April 2018 Update). I guess you have Tablet mode enabled. if it still doesn't work - please try the Windows 10 Settings approach section on the original link.
    – itsho
    Jan 1, 2019 at 9:34
  • 1
    @00jt I've tested on Windows 10 1709 and compiled with 4.6.2. it works ONLY if you use the touch screen, and NOT if you use the mouse to click the field. also tested on PasswordBox - see gist I've just created
    – itsho
    Jan 2, 2019 at 10:48
  • 1
    @00jt that's really weird :-( I've tested again on few other PC and tablets, and it behaves correctly for both TextBox and PasswordBox. Out of curiosity: did you try the example gist I provided above?
    – itsho
    Jan 3, 2019 at 12:56
  • 2
    @00jt I really don't know what to say, but maybe you can utilize one of Microsoft's examples in GitHub
    – itsho
    Jan 5, 2019 at 16:28
1

I saw this done in a TechEd session, you need to first disable Inking support (DisableWPFTabletSupport) then you can create an InputPanelConfiguration (AutomationElement.FromHandle(new WindowsInteropHelper(this).Handle) and call EnableFocusTracking.

DisableWPFTabletSupport: http://msdn.microsoft.com/en-us/library/ee230087.aspx

EnableFocusTracking: http://msdn.microsoft.com/en-us/library/windows/desktop/jj126268(v=vs.85).aspx

1
  • Do you have a link to the TechEd session? Unfortunately for me EnableFocusTracking is only supported on Win8 or higher..
    – Wouter
    Oct 31, 2013 at 8:40
0
 public static class InkInputHelper
    {
        public static void DisableWPFTabletSupport()
        {
            // Get a collection of the tablet devices for this window.  
            TabletDeviceCollection devices = System.Windows.Input.Tablet.TabletDevices;

            if (devices.Count > 0)
            {
                // Get the Type of InputManager.
                Type inputManagerType = typeof(System.Windows.Input.InputManager);

                // Call the StylusLogic method on the InputManager.Current instance.
                object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
                            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                            null, InputManager.Current, null);

                if (stylusLogic != null)
                {
                    //  Get the type of the stylusLogic returned from the call to StylusLogic.
                    Type stylusLogicType = stylusLogic.GetType();

                    // Loop until there are no more devices to remove.
                    while (devices.Count > 0)
                    {
                        // Remove the first tablet device in the devices collection.
                        stylusLogicType.InvokeMember("OnTabletRemoved",
                                BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                                null, stylusLogic, new object[] { (uint)0 });
                    }
                }
            }
        }
    }

use that class to determine if there is a physical keyboard, or a similar way that might better suit your needs.

i used this class to open and close the keyboard wherever i wanted.

 class KeyboardManager
    {

        public static void LaunchOnScreenKeyboard()
        {
            var processes = Process.GetProcessesByName("osk").ToArray();
            if (processes.Any())
                return;
            string keyboardManagerPath = "KeyboardExecuter.exe";
           Process.Start(keyboardManagerPath);
        }

        public static void KillOnScreenKeyboard()
        {
            var processes = Process.GetProcessesByName("osk").ToArray();
            foreach (var proc in processes)
            {
                proc.Kill();
            }
        }
        public static void killTabTip()
        {
            var processes = Process.GetProcessesByName("TabTip").ToArray();
            foreach (var proc in processes)
            {
                proc.Kill();
            }
        }

        public static void LaunchTabTip()
        {
            Process.Start("TabTip.exe");
        }
    }

keep in mind the following: i added a copy of both osk.exe AND tabtip.exe. adding this in my program solved an issue where either tabtip or osk wouldnt work on 32/64 bits.

osk is the keyboard, tabtip is the docked version of it. keyboardexecuter is a program i made which is used as a fallback method.

note* i can currently not test this on a touch screen device. you have to try that on your own.

for this to all work correctly i used this code in my mainwindow:

public int selectedTableNum;
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;

            // Disables inking in the WPF application and enables us to track touch events to properly trigger the touch keyboard
            InkInputHelper.DisableWPFTabletSupport();
            //remove navigationbar
            Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
            {
                var navWindow = Window.GetWindow(this) as NavigationWindow;
                if (navWindow != null) navWindow.ShowsNavigationUI = false;
            }));


            KeyboardManager.LaunchTabTip();

        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //Windows 8 API to enable touch keyboard to monitor for focus tracking in this WPF application
            InputPanelConfiguration cp = new InputPanelConfiguration();
            IInputPanelConfiguration icp = cp as IInputPanelConfiguration;
            if (icp != null)
                icp.EnableFocusTracking();
            mainFrame.Content = new LoginPage();
        }
        //public bool HasTouchInput()
        //{
        //    foreach (TabletDevice tabletDevice in Tablet.TabletDevices)
        //    {
        //        //Only detect if it is a touch Screen not how many touches (i.e. Single touch or Multi-touch)
        //        if (tabletDevice.Type == TabletDeviceType.Touch)
        //            return true;
        //    }

        //    return false;
        //}

i included the comments because it might be usefull to someone if there is an error.

the inputpanel configuration:

[Guid("41C81592-514C-48BD-A22E-E6AF638521A6")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInputPanelConfiguration
{
    /// <summary>
    /// Enables a client process to opt-in to the focus tracking mechanism for Windows Store apps that controls the invoking and dismissing semantics of the touch keyboard.
    /// </summary>
    /// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
    int EnableFocusTracking();
}

[ComImport, Guid("2853ADD3-F096-4C63-A78F-7FA3EA837FB7")]
class InputPanelConfiguration
{
}

i hope this might help future visitors of this question.

1
  • the return"S_OK" is not used anymore, il try to find it should it be needed May 11, 2016 at 8:58
-2

The easiest option when not requiring SecureString output is to use TextBox and use something like Wingdings as font.

1
  • 1
    It's not clear how this (workaround) answers the question. Please elaborate and, ideally, provide an example. Mar 21, 2017 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.