mdm
mdm

Reputation: 12630

Flash text field displaying incorrect characters

I've come across a strange error with a contact details form developed with Flash CS5 Prof. My keyboard layout is set up as English (UK) and pressing shift-2 gives me an ", shift-' gives me a @ in Chrome, Notepad, Word etc. In a text field on the flash form, entering shift-2 gives me @ but shift-' gives me ". I understand this is how the US keyboard is mapped out but it is confusing to my users.

How can I change the text field so that it works correctly for my keyboard layout?

Upvotes: 0

Views: 760

Answers (2)

www0z0k
www0z0k

Reputation: 4434

i didn't find a way to specify a locale in Flash, however the following code does what you want:

package {
    import flash.display.Sprite;
    public class NewClass extends Sprite {      
        public function NewClass() {
            addChild(new TextFieldReplacingChars());
        }
    }
}

import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TextEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;

class TextFieldReplacingChars extends Sprite {
    private var tf:TextField;
    private var toReplace:Object;
    private var str1:String = '';
    private var str2:String = '';
    private var pressedKeyCount: int = 0;
    private var timer:Timer;
    public function TextFieldReplacingChars() {
        tf = new TextField();
        addChild(tf);
        tf.type = 'input';
        tf.addEventListener(TextEvent.TEXT_INPUT, ontext);
        tf.addEventListener(KeyboardEvent.KEY_DOWN, onPress);
        tf.addEventListener(KeyboardEvent.KEY_UP, onRelease);
        toReplace = new Object();
        toReplace['"'] = '@';
        toReplace['@'] = '"';
        timer = new Timer(1, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, updateText);
    }

    private function onRelease(e:KeyboardEvent):void {
        pressedKeyCount -= pressedKeyCount > 0 ? 1 : 0;
    }

    private function onPress(e:KeyboardEvent):void {
        pressedKeyCount += toReplace[String.fromCharCode(e.charCode)] ? 1 : 0;
    }

    private function ontext(e:TextEvent):void { 
        if (toReplace[e.text] && pressedKeyCount > 0) {
            str1 = tf.text.substring(0, tf.caretIndex) + toReplace[e.text];
            str2 = tf.text.substring(tf.caretIndex, tf.text.length);                
            timer.start();
        }       
    }

    private function updateText(e:TimerEvent):void {
        tf.text = str1 + str2;
        tf.setSelection(str1.length, str1.length);
    }
}

Upvotes: 1

Lars Blåsjö
Lars Blåsjö

Reputation: 6127

There is a known bug when using wmode="transparent" or wmode="opaque" that, in some browsers (Firefox and possibly Crome), will give these kinds of errors, defaulting to US keyboard layout. As far as I know, there is no good solution for it, only quite cumbersome workarounds. If you google for flash wmode keyboard bug you will find quite a lot of info and workarounds.

Upvotes: 2

Related Questions