pharo-graphics / Toplo

A widget framework on top of Bloc
MIT License
17 stars 8 forks source link

Simulate a user typing a string #163

Open tinchodias opened 6 days ago

tinchodias commented 6 days ago

I failed on my first attempt to do it:

tf := ToTextField new.
s := tf inSpace.
s show.

"now, manually click on the text field"

"now, try to simulate typing:"

"aString := 'hello'."
aString := 'h'.

events := Array streamContents: [ :stream |

    aString do: [ :each |
        | key|
        key := KeyboardKey fromCharacter: each.
        stream
            nextPut:
                ((BlKeyDownEvent target: tf)
                    key: key;
                    timestamp: s time now;
                    yourself);
            nextPut:
                ((BlKeyUpEvent target: tf)
                    key: key;
                    timestamp: s time now;
                    yourself) ] ].

events do: [ :event | s dispatchEvent: event ].
tinchodias commented 5 days ago

This does work:

tf := ToTextField new.
s := tf inSpace.
s show.

tf requestFocus.
BlSpace
    simulateEvent: (BlTextInputEvent new
        text: '1234';
        yourself)
    on: tf.

(inspired on BlDevScripterTypeTextStep>>#playOn:)

I keep this question open:

should we add a method to BlSpace as there is for click (BlSpace class>>#simulateClickOn:)?