9

I am trying to show/hide keyboard on a Windows Metro app programmatically. I initially thought I could do it using a collapsed textbox and setting focus on it. But it seems like that has been disallowed in this link. The link also talks about the AutomationPeer and TextAutomationPeer to accomplish this. Is there a resource on how to use these ?

Thanks in advance PK

pkumar0
  • 2,069
  • 3
  • 14
  • 21
  • This Link (http://social.msdn.microsoft.com/Forums/ha-Latn-NG/winappswithcsharp/thread/3c227262-1d2c-4382-9c50-5b71d2b5d823) indicates that you can set/remove the focus to a `Text Control` to show/hide the keyboard. What exactly you are trying to do? Can you provide more details? If you are using Javascript, can you try putting a `div` over your textbox to make it "invisible" and react to input change event – louis.luo Apr 12 '12 at 19:41
  • Both links talk about the inability to show the keyboard: """This means that applications cannot programmatically invoke the touch keyboard via manipulation of focus. Big culprits here in the past have been webpages—many of them set focus by default into an input field but have many other experiences available on their page for the user to enjoy. However, we feel that requiring the user to tap the input field is an acceptable compromise.""" – pkumar0 Apr 12 '12 at 20:44
  • To enable user-driven invocation, we track the coordinates of the last touch event and compare them to the location of the bounding rectangle of the element that currently has focus. If the point is contained within the bounding rectangle, the touch keyboard is invoked. – pkumar0 Apr 12 '12 at 20:48
  • What we are trying to do is intercept keyboard input and send it to a server, also the server can instruct the client to show keyboard. – pkumar0 Apr 12 '12 at 20:49
  • possible duplicate of [how to hide on EditText soft keyboard windows 8 Metro Application?](http://stackoverflow.com/questions/10714431/how-to-hide-on-edittext-soft-keyboard-windows-8-metro-application) – N_A Jul 24 '12 at 20:55

5 Answers5

3

From here:

UI Automation is the mechanism through which developers communicate whether or not a particular UI element can receive text input. You must ensure that the appropriate accessibility properties are set in your apps so that the touch keyboard will know to appear when focus lands on a specific UI element. For Windows-provided controls, this will be done automatically because proper accessibility properties are set by default, but for custom controls and experiences you must do additional work to set the accessibility properties correctly; remember that the touch keyboard reacts to these properties.

If you use C# or C++, use an AutomationPeer object, and specifically a TextAutomationPeer. A Windows 8 Release Preview sample will demonstrate how to do this in C#. Remember that the control must also be editable and able to receive text to get the keyboard to invoke, in addition to having the appropriate accessibility settings. Indicating that something can receive text when it cannot will mislead accessibility tools and the users who rely on them.

To enable user-driven invocation, we track the coordinates of the last touch event and compare them to the location of the bounding rectangle of the element that currently has focus. If the point is contained within the bounding rectangle, the touch keyboard is invoked.

So you cannot programmatically show the keyboard. The appropriate way to hide/show the keyboard is by setting your control to accept input using an AutomationPeer object.

From here, if you set the input control to read-only then it will not trigger the keyboard, so possibly you could use that to control when the keyboard opens.

Edit:

There are a few things to check when implementing the text automation peer:

  1. Make sure you either test with a real touch device or test using the simulator with the Basic Touch Mode tool. If you don't do this the automation peer won't activate since it is only activated by a stylus or a touch input (not mouse).

  2. Make sure your custom control implements OnCreateAutomationPeer something like this:

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

  3. Make sure your Automation Peer implements FrameworkElementAutomationPeer, ITextProvider and IValueProvider

More details found in the example here.

Community
  • 1
  • 1
N_A
  • 19,799
  • 4
  • 52
  • 98
  • 1
    I am not sure but I think even the automationpeer method does not work. I tried it but could not make it work. – pkumar0 Sep 30 '12 at 05:02
  • I tried running an metrostyle app with textbox in my windows 8 PC. When tapped on textbox the touch keyboard din't show up. Why is that so ? or will it be shown if its run in a tablet ? – Chaitanya Feb 18 '13 at 11:03
  • +1 Nice answer. For reference, see also [Custom automation peers](https://msdn.microsoft.com/en-us/library/windows/apps/jj219211.aspx#oncreateautomationpeer). – HappyNomad Aug 26 '15 at 00:08
1

Just put a TextBox and hide it. Set the IsReadOnly = true and set the tab index of the TextBox to 0, so keyboard will focus on that TextBox but it realize TextBox is readonly and it will not poped up. :)

Sevenate
  • 6,221
  • 3
  • 49
  • 75
SurenSaluka
  • 1,534
  • 3
  • 18
  • 36
0

There is an example with the custom AutomationPeer, which seems to be helpful for the question.

Good guide for creating the programmatic keyboard logic.

Holp it could be help

Community
  • 1
  • 1
Yang C
  • 536
  • 5
  • 16
0

If you add a textbox and then select properties> under Miscellaneos there is a property named PreventKeyboardDisplayOnProgrammaticFocus, check that and set the focus of your text box like this:

 HiddenSearchBox.Focus(FocusState.Programmatic);
Juan
  • 1,352
  • 13
  • 20
-2

Here is little PowerShell script I use to achieve that. You can do the same in C# by getting the service and starting/stopping it depending on your needs.

$serv = get-ciminstance win32_service -filter "name = 'TabletInputService'"
# if started stop it
if( $serv.State.equals("Running") ){
    Stop-Service TabletInputService
}
# if not set to disabled, disable it
# else set to auto and start
if( !$serv.StartMode.equals("Disabled") ){
    Set-Service TabletInputService -StartupType Disabled
    "TabletInputService Disabled"
}
else {
    Set-Service TabletInputService -StartupType Auto
    Start-Service TabletInputService
    "TabletInputService Enabled and Started"
}
Mayank
  • 8,777
  • 4
  • 35
  • 60
  • How much time does it take? I am not working on the product anymore but this seems little slow. – pkumar0 Jul 12 '13 at 18:37