A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] case Keyboard.ENTER works, why not case Keyboard.anyletter?

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    30

    resolved [RESOLVED] case Keyboard.ENTER works, why not case Keyboard.anyletter?

    Hi gang, first off I want to say that I completed my app development program certificate through the University of Washington this past week, and I got through a couple of sticky areas thanks only to the helpful members of Flashkit boards. I am very thankful.
    With 9 months of curriculum in my head, it seems that I've really only scratched the surface of AS.

    My current project, one I came up with after being inspired by this fellow's drum playing app (http://travisroof.com/coastdrums.html) is to let the user strum notes in a guitar A minor pentatonic scale. I've recorded the notes and have progressed to the point where I'm programming the actual note playing.

    I've decided that while playing a guitar via mouse click may sound cool I think it'd be better to wire the notes up via keyboard. And I have hit a snag that I don't understand.
    Actionscript Code:
    loA.addEventListener(MouseEvent.CLICK, playLowA);
    loC.addEventListener(MouseEvent.CLICK, playLowC);
    loD.addEventListener(MouseEvent.CLICK, playLowD);

    etc...

    Actionscript Code:
    function playLowA(evt:MouseEvent):void{
        if(channel){
            channel.stop();
            channel = blLowA.play();
        } else {
        channel = blLowA.play();
        }
    }

    function playLowC(evt:MouseEvent):void{
        if(channel){
            channel.stop();
            channel = blLowC.play();
        } else {
        channel = blLowC.play();
        }
    }

    etc...

    That's how the mouseclick function/events work. No problem. Having 12 separate notes as their own functions may seem belabored, but it satisfies Programmers Rule #1: It Works.

    Here's how I'd like to have it set up:

    Actionscript Code:
    stage.addEventListener(KeyboardEvent.KEY_DOWN, playKeyboardNote);

    function playKeyboardNote(evt:KeyboardEvent):void
    {
        switch(evt.keyCode){
            case Keyboard.ENTER:
            if(channel){
            channel.stop();
            channel = blLowA.play();
            } else {
            channel = blLowA.play();
            }
            break;
            case Keyboard.BACKSPACE:
            if(channel){
            channel.stop();
            channel = blLowC.play();
            } else {
            channel = blLowC.play();
            }
            break;
        }
    }

    But instead of ENTER and BACKSPACE I want to use my own key assignments, namely Q W E R T Y U I O P[ ]

    And the 'intellisense' capability in Flash CS4 shows that it is a possibility: when typing
    Actionscript Code:
    case Keyboard.
    dozens of key choices are available, including regular ol' letters...
    So why doesn't

    Actionscript Code:
    case Keyboard.Q:
            if(channel){
            channel.stop();
            channel = blLowA.play();
            } else {
            channel = blLowA.play();
            }
            break;

    work for me in this script?
    If Keyboard.ENTER is a valid option, why isn't Keyboard.Q?
    And yes, when testing the movie, I do choose 'Disable keyboard shortcuts.'
    And, though i don't know if it's important, I include this at the top:

    Actionscript Code:
    import flash.ui.Keyboard;



    This is gonna be one howling cool app. I've recorded the pentatonic scale in a regular bluesy amp effect, and I've got a shredding metal-like amp effect recorded ready to go, as well. I also plan to have a backing track option, so the user can play out their own little guitar solo.

    Thanks very much
    Don
    Last edited by brainSalad; 06-20-2010 at 04:50 PM. Reason: clarification of code

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    For some absolutely infathomable (to me) reason, the letter constants on Keyboard are AIR-only. Except for G. Seriously.

    But you can use the charCode or keyCode properties of the KeyboardEvent to get the letter which was pressed.

    You can take a look at my SpecificKeys class for an example. Or just use it.
    http://cosmodro.me/blog/2008/sep/2/specific-keys/

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    30
    Thanks flax. What is the .me extension on your url?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Technically, .me belongs to Montenegro. But it's one of those open country code top-level domains that sell to anyone. I just liked having "cosmodrome" for a url. I also got "gimmegimmegim.me", but haven't done anything with it and it's going to expire soon.

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    30

    Thumbs up methodology for using keys for event triggers

    I found a way to easily make individual letters/keys wired up as event listeners/triggers for actions. I didn't come up with this myself, I found it listed on a website...somewhere... i really should find out exactly where to give them credit.

    Anyway...

    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;

    stage.addEventListener(KeyboardEvent.KEY_DOWN, functionName);

    function functionName(keyEvent:KeyboardEvent)
    {
    switch(keyEvent.keyCode){
    case keyEvent.keyCode = 81:
    ** Insert your action here **
    break;
    }
    }


    Now, this keyCode = 81 translates to the letter 'Q' key.
    The top row keyCodes are:
    Q, W, E, R, T, Y, U, I, O, P

    81, 87, 69, 82, 84, 89, 85, 73, 79, 80

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    30
    here's the URL that showed me how to assign keyboard presses as event triggers:
    http://www.kirupa.com/developer/as3/...yboard_as3.htm

Tags for this Thread

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