mercredi 25 janvier 2017

as3 How to dispatch a specific keyboard event

I was looking for this answer and had no luck. One place I looked actually had a very discouraging answer: "You cannot force mouse or keyboard events - they HAVE TO come from mouse or keyboard."


I tried 'brute force' and came up with this solution. Maybe I'm going about it wrongly or stupidly; is there a better way?

I had a keyboard event that launched a class and wanted to put a sprite on the stage that would initiate this same action -- clicking on the sprite would launch the keyboard event (Escape key).

In the eventListener function, I traced the event e itself:

    private function keys(e:KeyboardEvent):void {
        trace("EscapeKey: ",e);   
        if (e.keyCode == 27) {

The output was

EscapeKey: [KeyboardEvent type="keyDown" bubbles=true cancelable=false eventPhase=2 charCode=27 keyCode=27 keyLocation=0 ctrlKey=false altKey=false shiftKey=false]    

I then had the mouseClick listener create and dispatch a new keyboardEvent using the values I got from the above trace:

    private function pauseClick(e:MouseEvent):void {
        var a:KeyboardEvent = new KeyboardEvent("keyDown", true, false, 27, 27, 0, false, false, false);

Hopefully, this post will come in handy to others looking for these types of mouse/keyboard event redundancies.

Aucun commentaire:

Enregistrer un commentaire