A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: how to use other keys for "key.isDown" function

Hybrid View

  1. #1
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Try the following frame script in frame 1:

    code:

    keyer = new Object();
    keyer.onKeyDown = function()
    {
    trace('this key was pressed: ' + Key.getAscii() + " (code = " + Key.getCode() + ")" );
    if (Key.getAscii() == ord('a')) {
    trace('A was pressed');
    }
    else if (Key.getCode() == Key.RIGHT) {
    trace('Right was pressed');
    }
    }

    Key.addListener(keyer);




    In the keyDown event you can look at either Key.getAscii() - which will give you printable characters, or Key.getCode() which will get you arrows and other special keys.

    Here's another version of the onKeyDown handler which uses two switch statements and is a little more elegant:

    code:

    keyer.onKeyDown = function()
    {
    trace('this key was pressed: ' + Key.getAscii() + " (code = " + Key.getCode() + ")" );

    // check for special keys first
    switch (Key.getCode()) {
    case Key.RIGHT:
    trace('right was pressed');
    break;
    case Key.LEFT:
    trace('left was pressed');
    break;
    default:
    // it wasn't a special key that we're tracking
    switch (chr(Key.getAscii())) {
    case 'A':
    case 'a':
    trace('A was pressed');
    break;
    case 'B':
    case 'b':
    trace('B was pressed');
    break;
    case 'C':
    case 'c':
    trace('C was pressed');
    break;
    }
    }
    }






    You can also make an event handler for onKeyUp as well.
    Last edited by jbum; 10-18-2004 at 10:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center