A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: keyboard event problem

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Posts
    3

    keyboard event problem

    function mkey (event:KeyboardEvent)
    {
    }

    function mkey (event:MouseEvent)
    {
    }

    this.addEventListener(KeyboardEvent.KEY_DOWN, mkey);
    mbutton.addEventListener(MouseEvent.CLICK, mbut);
    I created a simple keyboard event that makes an object move. The problem I'm having is that when I try this out in the player the flash doesn't recieve any Keyboard events until I click an object that I made use a mouse event.

    basically I have to click on the mbutton object for the keyboard events to be recognised.

    Should I set a focus or what?

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Your function names are the same. The last function will override the previous one.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Apr 2008
    Posts
    3
    Oops, I didn't mean to write the functions that way when I posted. Assuming that the function names are different. Would flash look for the mouse listener before the keyboard listener?



    function mkey (event:KeyboardEvent)
    {
    }

    function mbut (event:MouseEvent)
    {
    }

    this.addEventListener(KeyboardEvent.KEY_DOWN, mkey);
    mbutton.addEventListener(MouseEvent.CLICK, mbut);

  4. #4
    FK Newb Beater
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    676
    Setting focus to the object is the ONLY way it can receive keyboard events. If you click it, you are setting it's focus. If you want to capture keyboard events without having to set focus, add the listener to the stage object.
    code:
    this.stage.addEventListener(KeboardEvent.KEY_DOWN, whatever);

    Just because you changed the code, doesn't mean it's yours

    Most Recent Work:
    Commercial tanning beds website

  5. #5
    Junior Member
    Join Date
    Apr 2008
    Posts
    3
    Thank you, that is exactly what I was looking for. I assumed that 'this' would have implied that it was the stage.

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