Search code examples
actionscript-3flasheventskeyboard-eventsgeorgian

Catching uppercase letters in AS3


Okay, I know how to catch usual lowercase letters. I'm using KeyboardEvent.KEY_DOWN and compare the code to the ASCII table to find out which key was pressed. Now, I also want to be able to find out if there is, for example SHIFT+A pressed, and I have no idea how to implement this. I mean, in my program SHIFT and A are absolutely different keys which have nothing to do with each other, and they both will call KeyboardEvents when pressed. In Georgian alphabet some letters are typed by combination of SHIFT and English letters, for example W on Georgian keyboard means წ, when SIFT+W means ჭ. Absolutely different letters, as you can see. And I want to be able to catch both, coz I'm currently developing Georgian-language game. Any help will be appreciated. Thanks in advance :-)


Solution

  • Take a look at the KeyEvent class. It provides you with the infos on if the shift key was pressed or not. Look at this example:

    stage.addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);
    
    function keyDownHandler (e:KeyboardEvent):void {
        trace("Key code: "+e.keyCode);
        trace("Ctrl status: "+e.ctrlKey);
        trace("Key location: "+e.keyLocation);
        trace("Shift key: "+e.shiftKey);
    }
    

    This is an example output:

    (pressing the "a" key)
    Key code: 65
    Ctrl status: false
    Key location: 0
    Shift key: false
    
    (pressing the SHIFT first for shift+a)
    Key code: 16
    Ctrl status: false
    Key location: 1
    Shift key: true
    
    (pressing the "a" key while still holding shift)
    Key code: 65
    Ctrl status: false
    Key location: 0
    Shift key: true
    

    As you can see the combination of KeyboardEvent.keyCode and KeyboardEvent.shiftKey hold the information which you are looking for.