How to find out the character pressed key in langu

2019-07-01 11:31发布

问题:

I can't use charCode, or keyCode in KeyboardEvent to find out the character pressed, because even if I change the keyboard layout, charCode and keyCode are not change (if press the same key).

So, how to find the presssed character, following to the current keyboard layout?

I already found this question, but what the document said:

The charCode property is the numeric value of that key in the current character set (the default character set is UTF-8, which supports ASCII).

is not correct.

Edit: I am using Flex 4.

回答1:

I found the solution.

We can listen for TextEvent. This event not only dispatches to text components, but also all focused components that implement UIComponent.

For example:

package champjss
{
    import flash.events.TextEvent;
    import mx.core.UIComponent;

    public class EditorComponent extends UIComponent
    {
        public function EditorComponent()
        {
            super();

            addEventListener(TextEvent.TEXT_INPUT, handleTextEvent);
            setFocus();
        }

        protected function handleTextEvent(event:TextEvent):void
        {
            // This text the text that user types,
            // following to current keyboard layout ;)

            trace(event.text);
        }
    }
}


回答2:

keyCode values are locale dependent. I believe various layouts will share the same keyCode for the same keys. It's not the keyboard that determines the keycodes, it is the locale setting on the computer. I hope this helps.



回答3:

Are you looking for String.fromCharCode()?