Mouse position on screen in flex

14,838

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>
Share:
14,838
Dan
Author by

Dan

First started playing with computers on Research Machines 380Z then BBCs, ZX80/81, Spectrums, Amstrad PCWs, Atari STs, Commodore Amiga and IBM PCs. Intially I started off commercially as a product and UI designer working on Amiga software (remember Wordworth, Datastore, Organiser and other Digita software) 20 years ago then taught myself C/C++ to help out debugging/fixing projects ... now write my own. At Digita I managed the Amiga product line including the production of multiple OEM versions translated into many languages for bundling with the European Amiga product. After the demise of Commodore, and thus our Amiga products, I became responsible for the blosoming PC Taxation product line including managing the UK versions of Microft Money 96/97/98, for which we became development partner, and Microsoft TaxSaver (beating the USA product to market). Follow my retweets at @coderanger

Updated on June 04, 2022

Comments

  • Dan
    Dan almost 2 years

    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>
    
  • Dan
    Dan almost 15 years
    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
    Dan almost 15 years
    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
    Dan almost 15 years
    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.
  • Hooray Im Helping
    Hooray Im Helping almost 15 years
    Bummer, sorry I couldn't help - don't have a copy of FB3 to test on at work. Good luck.
  • Mikosz
    Mikosz almost 15 years
    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.
  • Mikosz
    Mikosz almost 15 years
    Back - checking into this now.
  • Dan
    Dan almost 15 years
    Brilliant, thats all I needed was the "stage.nativeWindow.globalToScreen" function ... great thanks.
  • Mikosz
    Mikosz almost 15 years
    :D - no worries happy to help.