views:

3728

answers:

3

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>
A: 
//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;
Rick J
Nope, doesnt work. This is the problem I was getting, after localToGlobal it is the same value. 'global' is within the application, not related to the screen from what I have read and the experience I was getting using these functions
Dan
+1  A: 

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>
Gabriel
As Rick's comment, I have tried these functions and they dont work as I expected. They dont convert to screen co-ords at all.
Dan
gotta head to lunch - but when I get back - I'll check this again - thanks for the follow up and sorry if these guys didn't help. Now I'm interested - ha - let me get a little grub and I'll look into this. Fun.
Gabriel
Back - checking into this now.
Gabriel
Cool thanks a lot for this, looking forward to a solution
Dan
This should work.
Gabriel
This should place your window to the point... :D
Gabriel
Brilliant, thats all I needed was the "stage.nativeWindow.globalToScreen" function ... great thanks.
Dan
:D - no worries happy to help.
Gabriel
Now if only we can get some others to like the answer (ha!) ...
Gabriel
A: 

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 );
}
Hooray Im Helping
Nope, evt.local is position of mouse within the button I clicked, stagex is position of mouse within the stage (same as the example in my post), I want the screen co-ords of the mouse.
Dan
Bummer, sorry I couldn't help - don't have a copy of FB3 to test on at work. Good luck.
Hooray Im Helping