Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

windows 8 - Show/Hide Keyboard programmatically on windows8

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...