开发者

Strategy for supporting international keyboards in Flash/Flex

I have an Flash/Flex application that is showing weird behavior when used in conjunction with international keyboards. For example, I have extende开发者_运维百科d TextInput in this app to allow for special behavior when the user presses the hyphen ('-') key. When this happens I want the text of the input to be padded with zeros in addition to the typed text out to six digits. So if the user types in "5-" they should see in the field "000005-".

On a French keyboard, the user is required to have CapsLock enabled to type numbers, and the 6 and hyphen characters share a key. So when the French user types in just "6" with CapsLock enabled, what they see in the field is "000006-". It appears however that both the 6 and - characters are being sent to the field. The 6 causes the numeral to appear, and the - causes the special behavior described above.

I've heard that listing for a TextEvent instead of a KeyEvent might help in this case. I have not tried that yet, but I intend to shortly. However, there are several other issues related to differences between international keyboards. So I am wondering if there are any general strategies for ensuring correct support for the various international keyboards.

Any thoughts or ideas? FYI, this app currently uses Flex SDK 3.5 and Flash Player 10.


FYI, the correct answer was to listen for the TextEvent rather than a KeyboardEvent. I suppose that makes sense since by the time the actual text is available it has presumably been translated correctly from the keyCode and charCode values.


Have you ever tried keyboardEvent.charCode with String.fromCharcode?

private function onKeyDown(e:KeyboardEvent):void {
    var c:String = String.fromCharCode(e.charCode);
    switch (c) {
    case '-': ....
    }
}

Manipulating texts in event handler of TextEvent.TEXT_INPUT will be ok, but I don't recommend this approach because the time when TextEvent.TEXT_INPUT dispatched has a little difference in FP10 and FP10.1 (for 10.1 a new API for IME deal with this event)

For more details, I did a limited implementation :

Handling TextField-related Events - wonderfl build flash online


try this. It works very well for me. With this Tool and this Table you localize it for your targeted keyboard-layout

var c:uint;
var symbol:Number = KeyToSymbol(kEvent.keyCode, kEvent.shiftKey, kEvent.altKey);
if (symbol > -1)
  c = SymbolDE(symbol);
else
  c = kEvent.charCode;



function KeyToSymbol(code:Number=-1, shift:Boolean=false, alt:Boolean=false)
{
    var symbol:Number = -1;

    if (shift) {
        switch(code) {
            case 49: symbol = 0; break;
            case 50: symbol = 1; break;
            case 51: symbol = 2; break;
            case 52: symbol = 3; break;
            case 53: symbol = 4; break;
            case 54: symbol = 5; break;
            case 55: symbol = 6; break;
            case 56: symbol = 8; break;
            case 57: symbol = 10; break;
            case 48: symbol = 12; break;
            case 219: symbol = 14; break;
            case 221: symbol = 17; break;
            case 187: symbol = 19; break;
            case 191: symbol = 22; break;
            case 189: symbol = 25; break;
            case 190: symbol = 27; break;
            case 188: symbol = 29; break;
            case 222: symbol = 31; break;
            case 192: symbol = 33; break;
            case 186: symbol = 35; break;
            case 226: symbol = 38; break;
            case 220: symbol = 41; break;
        }
    }else if (alt) {
        switch(code) {
            case 55: symbol = 7; break;
            case 56: symbol = 9; break;
            case 57: symbol = 11; break;
            case 48: symbol = 13; break;
            case 219: symbol = 16; break;
            case 187: symbol = 21; break;
            case 81: symbol = 36; break;
            case 226: symbol = 39; break;
            case 69: symbol = 42; break;
        }
    }else {
        switch(code) {
            case 219: symbol = 14; break;
            case 187: symbol = 20; break;
            case 191: symbol = 23; break;
            case 189: symbol = 24; break;
            case 190: symbol = 26; break;
            case 188: symbol = 28; break;
            case 222: symbol = 30; break;
            case 192: symbol = 32; break;
            case 186: symbol = 34; break;
            case 226: symbol = 37; break;
            case 220: symbol = 40; break;
        }
    }
    return symbol;
}


function SymbolDE (symbol:Number = -1) {
    switch(symbol) {
        case 0: return 33; break;   // ! 
        case 1: return 34; break;   // " 
        case 2: return 167; break;  // § 
        case 3: return 36; break;   // $ 
        case 4: return 37; break;   // % 
        case 5: return 38; break;   // & 
        case 6: return 47; break;   // /  
        case 7: return 123; break;  // {  
        case 8: return 40; break;   // (  
        case 9: return 91; break;   // [ 
        case 10: return 41; break;  // )  
        case 11: return 93; break;  // ] 
        case 12: return 61; break;  // = 
        case 13: return 125; break;     // } 
        case 14: return 63; break;  // ?
        case 15: return 223; break; // ß
        case 16: return 92; break;  /* \ */
        case 17: return 96; break;  // ´
        case 19: return 42; break;  // *
        case 20: return 43; break;  // +
        case 21: return 126; break; // ~
        case 22: return 39; break;  // '
        case 23: return 35; break;  // #
        case 24: return 45; break;  // -
        case 25: return 95; break;  // _
        case 26: return 46; break;  // .
        case 27: return 58; break;  // :
        case 28: return 44; break;  // ,
        case 29: return 59; break;  // ;
        case 30: return 228; break; // ä
        case 31: return 196; break; // Ä
        case 32: return 246; break; // ö
        case 33: return 214; break; // Ö
        case 34: return 252; break; // ü
        case 35: return 220; break; // Ü
        case 36: return 64; break;  // @
        case 37: return 60; break;  // <
        case 38: return 62; break;  // >
        case 39: return 124; break; // |
        case 40: return 94; break;  // ^
        case 41: return 186; break; // °
        case 42: return 128; break; // €
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜