Keyboard Event Handling
A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Keyboard Event Handling

  1. #1
    Junior Member
    Join Date
    Apr 2000
    Posts
    13

    Cool

    Keyboard event handling is what i'm after, primarily for controlling sprites in gamming. I'm not talking about incrementing a variable by 1 with a repeated keystroke. I'm looking to trap the key when pressed, so that when the trap is made for a particular event, the trap automatically causes a stop on that event, so recursive traps can never take place. The return from the trap routine automatically does an ON unless an explicit OFF has been performed inside the trap routine.

    Say instead of having to continously tap on the 'Right Arrow Key' to have an object move to the Right, why not hold down on the 'Key' and have the object gradually slide to the right while having the key held down and have the object stop its movement upon the 'Key' being depressed. I have not yet to see this in any Flash Game . . .

    Kind of a critical fundamental to have in a game


    ------------------
    Life is a waste of Time, Time is a waste of Life, So why not get wasted all of the Time and enjoy the Time of your Life!

  2. #2
    Junior Member
    Join Date
    Apr 2000
    Posts
    21

    Thumbs up


    i started a reply to this, but i realized it would be better in a tutorial, so i wrote a quick tutorial and sent it over to flashkit. basically there's a way to detect on keyrelease, but it's not completely accurate, there's a slight delay. i'll post again if the tutorial goes up, keep your eye out for it.


    ------------------
    kennethAustin
    kenneth.austin@knowledgenet.com

  3. #3
    Senior Member
    Join Date
    Feb 2000
    Posts
    195

    Unhappy

    I believe you are talking about capturing both Key-Down and Key-Up events? Alas, Flash simply cannot do that . Flash fires the Key-Press event as soon as the key is depressed. Holding a key down will result in repeated Key-Press events (at the repeat-rate of your keyboard, as defined by your OS).

    Hardly ideal for gaming, I agree. Perhaps in Flash 5.


    <EMBED src="/cgi-bin/ubb/Members/sigs/00000205.swf" quality=high WIDTH=500 HEIGHT=50 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"> </EMBED>

  4. #4
    Member
    Join Date
    Feb 2000
    Posts
    62

    Wink


    I think you can simulate this by doing the following:

    1) Setup an "onkey" event which sets variable "key1"=true

    2) In actionscript have the following code:

    If (key1=true)
    Set Variable: "x" = x+1
    End If
    Set Variable: "key1" = false

    If this action-script is called in a loop then when the key is pressed x will increase - when you release the key, it stops.


    JR

  5. #5
    Member
    Join Date
    Feb 2000
    Posts
    62

    Post


    It worked smoothly enough for me - maybe you did it wrong.

    JR

  6. #6
    Junior Member
    Join Date
    Apr 2000
    Posts
    21

    Post



    could you post an example? is it smooth right away or is there a slight pause before it starts looping? also the key repeat timing is dependent on the user's os, so yours might be set faster.

    ------------------
    kennethAustin
    kenneth.austin@knowledgenet.com

  7. #7
    Member
    Join Date
    Feb 2000
    Posts
    62

    Wink


    Sure, no problem. Here's an example with acceleration/deceleration. Just press and hold space to move the object - let go and it will slow down.
    http://www.edesign.uk.com/flash/keypress.html

    I've uploaded the FLA too
    http://www.edesign.uk.com/flash/keypress.fla

    JR

  8. #8
    Member
    Join Date
    Feb 2000
    Posts
    62

    Wink


    Works well for me and as far as I'm aware my settings are standard for a Windows 98 PC. In control panel my "repeat delay" is set to short and "repeat rate" is set in the middle (neither fast nor slow).

    I'd be interested if it works well for other people....maybe Trigg can comment?

    Thanks

    JR

  9. #9
    Junior Member
    Join Date
    Apr 2000
    Posts
    21

    Lightbulb

    true, it will cause the action to be repeated on key press, but it's not smooth, try it. the key repeat process works so that when you press a key, it performs the action, then there is a slight delay until it loops repeatedly.

    also with this you can't perform an action on key release, because it would be looping that action constantly

    again, i'll post if the tutorial goes up

  10. #10
    Member
    Join Date
    Feb 2000
    Posts
    62

    Cool


    I agree, getting the little things right are important........I hope our combined responses have helped Trigg.

    JR

  11. #11
    Junior Member
    Join Date
    Apr 2000
    Posts
    21

    Smile


    sorry if i was being unclear, your way works perfectly for the acceleration example, in fact i think people should use this in the asteroids games that are out there.

    what i was referring to is the slight delay when you press the key, which you can't see in your example because the object doesn't have any inertia yet. this delay can cause an annoyance that is cropping up in alot of games now, the jump before you start moving. you can see it in the example below.
    http://www.goodnet.com/~kaustin/key_release.html

    if you press 2 you can see your example, which for me jumps back and forth once before staying, and if you press 1 it just goes, but there's a delay when you release.

    this is all kind of nit picking, but eliminating annoyances like these can make or break some games, you know?

    ------------------
    kennethAustin
    kenneth.austin@knowledgenet.com

  12. #12
    Junior Member
    Join Date
    Apr 2000
    Posts
    21

    Post


    me too

    i'm probably going to end up using both methods in a sort of "oddworld"-like game to move the guy around.

    flash 5 should have on key release, making things a hell of a lot easier

    ------------------
    kennethAustin
    kenneth.austin@knowledgenet.com

  13. #13

    Lightbulb

    You mean the sudden "teleportation"after you pressed the key to move the guy in the mc?

    Well, easy: make an extra frame before the game really starts with only yhe character in it. Put a code in it where you say the X_position of the guy must be 200 (or any other) and also for the Y_position, so the character moves to that place... make sure these X and Y positions are... sorry, kinda vague... but try it out or mail me, I'll send ya a fla-example if I still find it on my pc.

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

Poll by Flashkit.com