Dan
Dan

Reputation: 1571

Mouse position on screen in flex

I am trying to obtain the actual mouse co-ordinates on the screen so I can create a Native Window at that position but I dont seem to be able to find the right way to do this correctly.

I have tried various things, the closest thing I have at the moment is:

this.contentMouseX and this.contentMouseY

This gives me the coords on the current stage which is fine, then I add to that the:

NativeApplication.nativeApplication.activeWindow.x and activeWindow.y

Which is close, but this doesnt take into account the application title bar.

There must be an easier and more straightforward way of doing this I am sure, can anyone give advice cos I fail to find it on google?

I have tried localToGlobal which doesnt work, it seems that 'global' means within the application and not global to the screen which is of no use to me. Here is an example that shows the failure...

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.core.Application;

            private function click(evt:MouseEvent):void
            {
                var pt:Point = new Point( this.contentMouseX, this.contentMouseY );
                var global:Point = Application.application.localToGlobal( pt );

                trace( "local_x: " + pt.x + " x " + pt.y );
                trace( "global_x: " + global.x + " x " + global.y );
            }
        ]]>
    </mx:Script>

    <mx:HBox horizontalAlign="center" width="100%">
        <mx:Button id="butt" label="Click" click="click(event)" />
    </mx:HBox>
</mx:WindowedApplication>

Upvotes: 2

Views: 14872

Answers (3)

Gabriel
Gabriel

Reputation: 1273

After a false start... Sorry about that...

You can use stage.nativeWindow.globalToScreen - this will do what you need.

In terms of position of the window (for completeness) - you can also look into stage.nativeWindow.x and stage.nativeWindow.y --- this will give you the position for the application on the desktop - from there - you can position relative to that point.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.core.Application;

private function click(evt:MouseEvent):void
{
  var pt:Point = new Point( this.butt.x, this.butt.y );
  var global:Point = Application.application.localToGlobal( pt );

  trace( "local_x: " + pt.x + " x " + pt.y );
  trace( "global_x: " + global.x + " x " + global.y );

  var p:Point = stage.nativeWindow.globalToScreen(new Point(this.butt.x, this.butt.y));

  trace(p.x + " x " + p.y);
  var na:NativeWindow = new NativeWindow(new NativeWindowInitOptions());
  na.visible = true;
  na.width = 100;
  na.height = 100;
  na.x = p.x;
  na.y = p.y;
  na.activate();

}
]]>
</mx:Script>


<mx:Button x="10" y="10" id="butt" label="Click" click="click(event)" />

</mx:WindowedApplication>

Upvotes: 6

Hooray Im Helping
Hooray Im Helping

Reputation: 5264

Does this work:

private function click(evt:MouseEvent):void
{
  var pt:Point = new Point( evt.localX, evt.localY );
  var global:Point = new Point( evt.stageX, evt.stageY);

  trace( "local_x: " + pt.x + " x " + pt.y );
  trace( "global_x: " + global.x + " x " + global.y );
}

Upvotes: 0

Rick J
Rick J

Reputation: 2703

//get the application level mouse coordinate
var local_x : int = this.mouseX; 

//convert this x value into a global coordinate
var pt : Point = new Point(local_x, 0);
var global_x : int = this.localToGlobal(pt).mouseX;

Upvotes: 0

Related Questions