Not yet rated

Problem

Retrieving position relative to application window is easy, but to get position relative to screen we need to use slightly different approach.

Solution

By using globalToScreen we can retrieve mouse position relative to screen.

Detailed explanation

First, we have to add event listener for mouse move to application. We will insert this in creationComplete event:

application.addEventListener(MouseEvent.MOUSE_MOVE, reflectMouse);

Also, we are going to add Lable element where we will display current mouse position:

<mx:Label x="10" y="10" text="Label" width="165" id="myMouse"/>

Now, create function that will track mouse position:

private function reflectMouse(event:MouseEvent):void
{
  var myMousePos:Point = application.stage.nativeWindow.globalToScreen(new Point(application.mouseX, application.mouseY));
  myMouse.text = "X:" + myMousePos.x + " / Y: " + myMousePos.y;
}

That's it!

Happy coding!


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes