11using System . Collections . Generic ;
2+ using System . Globalization ;
23using System . Threading . Tasks ;
34
45namespace PuppeteerSharp . Input
@@ -41,7 +42,8 @@ public Task DownAsync(string key, DownOptions options = null)
4142
4243 var text = options ? . Text == null ? description . Text : options . Text ;
4344
44- return _client . SendAsync ( "Input.dispatchKeyEvent" , new Dictionary < string , object > ( ) {
45+ return _client . SendAsync ( "Input.dispatchKeyEvent" , new Dictionary < string , object >
46+ {
4547 { "type" , text != null ? "keyDown" : "rawKeyDown" } ,
4648 { "modifiers" , Modifiers } ,
4749 { "windowsVirtualKeyCode" , description . KeyCode } ,
@@ -67,7 +69,8 @@ public Task UpAsync(string key)
6769 Modifiers &= ~ ModifierBit ( key ) ;
6870 _pressedKeys . Remove ( description . Code ) ;
6971
70- return _client . SendAsync ( "Input.dispatchKeyEvent" , new Dictionary < string , object > ( ) {
72+ return _client . SendAsync ( "Input.dispatchKeyEvent" , new Dictionary < string , object >
73+ {
7174 { "type" , "keyUp" } ,
7275 { "modifiers" , Modifiers } ,
7376 { "key" , description . Key } ,
@@ -83,15 +86,7 @@ public Task UpAsync(string key)
8386 /// <param name="charText">Character to send into the page</param>
8487 /// <returns>Task</returns>
8588 public Task SendCharacterAsync ( string charText )
86- {
87- return _client . SendAsync ( "Input.dispatchKeyEvent" , new Dictionary < string , object > ( ) {
88- { "type" , "char" } ,
89- { "modifiers" , Modifiers } ,
90- { "text" , charText } ,
91- { "key" , charText } ,
92- { "unmodifiedText" , charText }
93- } ) ;
94- }
89+ => _client . SendAsync ( "Input.insertText" , new Dictionary < string , object > { [ "text" ] = charText } ) ;
9590
9691 /// <summary>
9792 /// Sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>, and <c>keyup</c> event for each character in the text.
@@ -109,8 +104,11 @@ public async Task TypeAsync(string text, TypeOptions options = null)
109104 {
110105 delay = ( int ) options . Delay ;
111106 }
112- foreach ( var letter in text )
107+
108+ var textParts = StringInfo . GetTextElementEnumerator ( text ) ;
109+ while ( textParts . MoveNext ( ) )
113110 {
111+ var letter = textParts . Current ;
114112 if ( KeyDefinitions . ContainsKey ( letter . ToString ( ) ) )
115113 {
116114 await PressAsync ( letter . ToString ( ) , new PressOptions { Delay = delay } ) . ConfigureAwait ( false ) ;
0 commit comments