"Windows 10 App development" input simulation

Source: Internet
Author: User

---restore content starts---

Input injection literal translation is: Import injection. The popular translation method is: analog input. This injection behavior can simulate several input behaviors:

1, keyboard keys.

2, mouse.

3, touch control.

4, writing pen input.

5, shortcut keys. This is limited to, for example, pressing the Win key and the back key.

The API that supports input behavior injection is under the Windows.UI.Input.Preview.Injection namespace, with the word "Preview" in the namespace, and it is estimated that this function is still in the trial run phase.

This so-called injection function, in short, can be simulated in the UWP app keyboard input, mouse action, etc., of course, according to the UWP security scheme, it is not possible as in the past Win 32 to let you everywhere, in the UWP, you can only inject the current process of UI behavior, meaning you understand, do not say more, It is your application that can only do your thing, do not disturb others.

The old week personally felt that the injection API was useless, but since the SDK has, let's get to know it.

Because this heap of APIs is restricted, so many friends try to call, and the result has been an exception. Well, sell a Xiaoguanzi first, then talk about it later.

Or the same as the usual, first tell the guys how to use. In fact, it is very simple, the core class is Inputinjector, this class you can not call the constructor, to use it, you want to call the static method TryCreate, the call will return a Inputinjector instance.

You can then call the following methods to send the input behavior as needed.

1, injecttouchinput, analog finger touch behavior, parameters are a set of Injectedinputtouchinfo instances, each Injectedinputtouchinfo instance represents a touch, the main parameters have touch point coordinates, Angle (such as two-finger rotation, the direction is counterclockwise), the force.

2, Injectmouseinput, Analog mouse. The method parameter consists of a set of Injectedinputmouseinfo objects, a single Injectedinputmouseinfo object that describes mouse coordinates, left-click or bounce, and so on.

3, Injectpeninput, analog pen input (such as electromagnetic pen), the data is described by a Injectedinputpeninfo object, such as the position of the tip of the pen, and so on.

4, Injectkeyboardinput, analog keyboard input, data from a set of Injectedinputkeyboardinfo object description, logarithmic content can be guessed, of course, with the key to press the virtual key code.

All right, let's get an example of the analog keyboard input in the old week.

        <StackPanelMargin= " the">            <TextBoxName= "txt" />            <StackPanelMargin= "2,15"Orientation= "Horizontal">                <ButtonContent= "A"Style="{StaticResource Btnst}"Click= "OnClick"/>                <ButtonContent= "B"Style="{StaticResource Btnst}"Click= "OnClick" />                <ButtonContent= "C"Style="{StaticResource Btnst}"Click= "OnClick" />                <ButtonContent= "D"Style="{StaticResource Btnst}"Click= "OnClick" />                <ButtonContent= "E"Style="{StaticResource Btnst}"Click= "OnClick" />                <ButtonContent= "F"Style="{StaticResource Btnst}"Click= "OnClick" />                <ButtonContent= "G"Style="{StaticResource Btnst}"Click= "OnClick" />            </StackPanel>        </StackPanel>

Interface no mystery, a TextBox is of course to receive input, the other string of buttons is of course the analog keyboard button.

These buttons all share the same Click event handling method--onclick.

Next is the write-processing code.

First, declare the variable at the page class level.

     NULL ;      ...      = Inputinjector.trycreate ();

The Click event is then processed.

        Private voidOnClick (Objectsender, RoutedEventArgs e) {Button btn=(Button) sender; stringContent = btn. Content as string; Charc = content[0]; Injectedinputkeyboardinfo KeyInfo=NewInjectedinputkeyboardinfo (); KeyInfo. Virtualkey= (ushort) C; Injectedinputkeyboardinfo[] Infos={KeyInfo}; //Let the text box get keyboard focus, or you can't lose it.txt.            Focus (Focusstate.keyboard); Injector.        Injectkeyboardinput (infos); }

This code is also very simple, a row of buttons, their content properties are the letter a B C D E F G, when the button is clicked, take out the character, and the char type, why use it, this is to be converted to integer value, because the key code is actually the ASCII code character.

After the key code is assigned to the Virtualkey property of the Injectedinputkeyboardinfo object, it specifies which key to press.

One thing to note here is that you must add this line before you send the input behavior.

Txt. Focus (Focusstate.keyboard);

You want to, to simulate the keyboard to input characters into the input box, so you have to make sure that the input box has been given keyboard focus.

After the example is complete, you will be sure to run happily, the result ...

You want to cry? Don't cry, you are not Lin sister, cry what.

At this time you look at the SDK documentation, a beat brain, is, so Trojan-like behavior how easy to invoke it, be sure to configure the manifest file. This high-level behavior requires that the namespace be introduced in the manifest file:

Http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities

Then you read the document, Cicada, to add this statement:

  < Capabilities >    <  name= "internetclient"/>    <Name  = "inputinjection"/>  </capabilities> 

Yes, there's nothing wrong with this idea, and if your app is going to be submitted to the App Store, declare it.

However, when you run again, you want to cry again.

This TMD strange, how still have no access to it, because you understand, too sensitive behavior, you are the application store to apply for cooperation in order to use.

It's not going to work. Actually not, the following old weeks to tell you the answer.

You should change the Name of Capability to inputinjectionbrokered. That's how it's changed.

  < Capabilities >    <  Name= "inputinjectionbrokered"/>  </ Capabilities >

The secret of the old week to tell the people, the old week spent five cents paid off the Microsoft development team to get the secret.

Well, after this change, you will be smiling face life, look at the effect.

Surprise you.

After the surprise, the old week told you a bad news, so after processing is not submitted to the store .

Well, the old weeks to share here, it is time to eat, this evening big Cook Cuong to invite me and a few buddies to dinner, he personally cook. So this is the end of the blog post.

Sample code Download: Http://files.cnblogs.com/files/tcjiaan/inputInjectionSample.zip

"Windows 10 App development" input simulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.