windows phone 8.1 swype keyboard event capture

2019-04-28 08:23发布

Update [16-Jul-2014]: The question is technically incorrect. Read the answer to get more details.


I was trying to capture text before reaching to my text box. and I discovered the following facts:

  • KeyDown, KeyUp event will tell you what virtualKey was pressed not the character !!

  • CoreWindow.CharacterReceived will capture the character but this
    event is not specific to TextBox and it will tell you the character
    after it reached to the textBox.

Now my question is:

Can any one tell me how can I capture the event of the Swype Keyboard on windows phone 8.1? enter image description here

Notice that :

1- I tried to capture it in TextBox.Paste but it fails :(

2- The event textBox.textChanged() is not what I am looking for bec that event fire after the keyboard is done and this event (textChanged) will fire after keyDown, keyUp, CharacterReceived regardless of how the text was input.

1条回答
何必那么认真
2楼-- · 2019-04-28 09:00

OK! I have been in this issue for days and extensively tested it to understand and came up with the following conclusions:

1- the input from keyboard is handled by the event of the page named (CharacterRecieved). the event is fired and captured by the Page then sent to the TextBox and will result in the firing of TextChanged event.

2- If you come to winRT and windows phone with the winForms mentality you will surely have the confusion i had, and will take some time to figure it out [unless you read this answer which will make it shorter].

3- Don't expect [ as I falsely expected ] that the event of the character entered will fire in the TextBox. It will fire on the page (CoreWindow) then changes the text value of the textBox, so you have to catch the event on the page level, not on the control level.

4- Difference between entering one letter by keyboard and entering one word using swype keyboard:

**

  • in Case of One letter entered by the keyboard on the phone, the following sequence will mostl likely happen:

**

suppose the textBox.Text = "99";

Now I will press the number 7:

1- KeyDown event will fire: here you may capture the virtualKey but you won't be able to know the character, so pressing 'a' letter you can't know is it 'A' capital or 'a' small. still textBox.Text = "99";
2- CharacterRecieved Fire; textBox.Text = "997";
3- KeyUp event fire; textBox.Text = "997";
4- textChanged fire.; textBox.Text = "997";

  • while in case of swype-keyboard:

suppose textBox.Text = "99"; and I want to enter the text "hello";

swype will add white space before the word so it will be " hello"

and the event sequence is as follow:

1- a loop for each character in the string " hello" will fire the event CharacterRecieved and the textBox.Text value will be textBox.Text= "99 " in the first iteration; then from the second iteration the textBox.Text = "99 hello"; in each iteration you can capture the key code [char] which is in this case (32, 110, 101, 108,108, 111). notice that by now the textBox.Text value is changed however not yet shown on the screen !!

2- The textChanged event will fire twice (weird !! i think one for the white space and the second for the word "hello"), Also, by now the textBox.Text = "99 hello" but still not yet shown on the screen till the end of the two iterations over the textChanged event.

With this we come to notice the difference between swype and normal keyboard key events which is that in swype there is no keyDown keyUp events at all !!

Now if you know the scenarios with each method of keyboard input ( swype/non-swype) you can plan your validation and application behaviour as you wish. provided that you know it is totally different than the legacy windows system input.

I hope this will help someone there and save him/her many hours of confusion and agony :)

查看更多
登录 后发表回答