Search code examples
actionscript-3mouseeventflashdevelopkeycode

keycode for MouseEvent?


So to my understanding, there are keycodes to represent the keystrokes, for example:

public function left(e:KeyboardEvent):void
{
    if (e.keycode == 65)
    {
       leftKey = true;
    }
}

I want to do something similar with this logic and apply it for Mouse Events. I've searched on Google but haven't found much results for Flashdevelop AS3. Would there be a keycode to represent Mouse Events? For example:

stage.addEventListener(MouseEvent.MOUSE_DOWN, down);
stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
stage.addEventListener(MouseEvent.CLICK, click);

public function down(e:MouseEvent):void
{
    if (e.keycode == ?)
}
public function move(e:MouseEvent):void
{
    if (e.keycode == ?)
}
public function click(e:MouseEvent):void
{
    if (e.keycode == ?)
}

Thanks in advance!


Solution

  • When you fire a mouse event that has a listener attached to it, then the function associated with that listener will get called. Period. You don't need to then, again, check to see if the mouse event happened. That's the whole beauty of event listeners. Just to make sure you get it, I'll post an example.

    stage.addEventListener(MouseEvent.CLICK,mClick);
    

    Great. We added a listener to the stage. Now, any time you click anywhere on the stage, this listener will cause that mClick function to get called. Now we write the mClick function.

    private function mClick(me:MouseEvent):void{
        trace("me.target.x",me.target.x);
        trace("me.target.y",me.target.y);
    }
    

    me is just a variable we chose to represent the event that triggered this function. Event is a class. A MouseEvent is a subclass of Event. So we are saying that me is an Event of sublcass MouseEvent and that this is the expected input for this function. If you were to try to call this function elsewhere in your code it would throw an error saying that this function expected some sort of input.

    me.target is the thing that caused the event to get triggered. In this case it is the mouse, so me.target.x will be the x position of the mouse at the time that the mouse was clicked.

    That is all there is to it. You have just confused yourself a little bit by trying to apply a specific solution to a different problem, namely, how to register key presses on a keyboard. They are handled slightly fundamentally differently. With a keyboard, we check listen for if the keyboard had a key pressed and then in the event handler, we determine which key was pressed. With mouse events we would have a different listener for mouse move, mouse click, left mouse click and middle mouse click. Since each of those specific events has their own listener, we don't have to evaluate which button was pressed in the handler:

    public function left(e:KeyboardEvent):void
    {
        if (e.keycode == 65)
        {
           leftKey = true;
        }
    }
    

    The e is the thing that caused the event. In this case, the Keyboard key. Each key has a keycode. But instead of writing a different listener for every single key on the keyboard, we can just write one listener and one function and then evaluate which key was pressed inside the function. As stated before, with the mouse, the left mouse button gets its own event listener, so no need (or ability) to check that in the event listener. There are properties like ROLL_OVER and MOUSE_UP and MOUSE_DOWN. Look at the documentation for a full list.