1.21 gigawatts
1.21 gigawatts

Reputation: 17756

How do I check if the user is typing in a text field?

How would you check if the user is typing into a text input (Flex TextArea, TextInput, RichEditableText)?

In my AIR app I want to listen for keyboard events on the application but I only want to respond if they are not typing into a text field and if no other component handles it.

For example, I want to listen to the A key and run a command. But only if they are not typing into a text field.

FYI I added a NativeFlexMenu and added keyboard modifier to it for A and it is capturing the key event even when the user is typing in a text field.

Upvotes: 0

Views: 434

Answers (1)

1.21 gigawatts
1.21 gigawatts

Reputation: 17756

This appears to work when added to the application. The one problem with this is that if a text field has focus it will continue to have focus even if you click on the application. I might try to add an click event handler to change focus.

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication keyUp="application1_keyUpHandler(event)"
                       keyDown="application1_keyDownHandler(event)">
</s:WindowApplication>

protected function application1_keyDownHandler(event:KeyboardEvent):void {
    var keyCode:int = event.keyCode;
    var applicable:Boolean;
    var focusedObject:Object;
    var target:Object = event.target;

    if (keyCode==Keyboard.A || 
        keyCode==Keyboard.B || 
        keyCode==Keyboard.C) {

        if (!event.controlKey && !event.commandKey) {
            applicable = true;
        }
    }

    if (!applicable) return;

    focusedObject = focusManager.getFocus();

    if (focusedObject is Application || event.target is Stage) {
        isApplication = true;
    }

    //var t:int = getTimer();
    if (target is RichEditableText ||
        focusedObject is IEditableText ||
        focusedObject is SkinnableTextBase ||
        focusedObject is TextField ||
        focusedObject is FTETextField ||
        focusedObject is FlexHTMLLoader ||
        focusedObject is ComboBox) {
        applicable = false;
    }
    //trace("time:" + (getTimer() - t)); takes 0 ms

    if (applicable) {
        if (keyCode==Keyboard.A) {
            // do something
        }
        else if (keyCode==Keyboard.B) {
            // do something
        }
        else if (keyCode==Keyboard.C) {
            // do something
        }
    }
}

Upvotes: 1

Related Questions