Macromedia Flex Macromedia Flex
Get Mouse Position
  Home

Jul 08, 2005 - Get Mouse Position
Get the X and Y positions of the mouse on mousedown and mousemove

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" horizontalAlign="left" 
  mouseMove="onMouseMove(event)"
  mouseDown="onMouseDown(event)">
<mx:Script><![CDATA[
 //tests mousedown to see if it is over Web Access button
 private var giMouseXLast:Number = 0;
 private var giMouseYLast:Number = 0;
 private function onMouseDown(oEvent:Object):Void
 {
  var iMouseX:Number = oEvent.target.mouseX;
  var iMouseY:Number = oEvent.target.mouseY;
  if (iMouseX > 200 && iMouseX < 400 && iMouseY > 100 && iMouseY <200)  {   //mousedown is over magic spot
   alert("You hit the magic spot");
  }
  lblLastMouseDownCoord.text = "(" + iMouseX + "," + iMouseY + ")"
 }
 
 //sets global variables when mouse moves
 private function onMouseMove(oEvent:Object):Void
 {
  giMouseXLast = oEvent.target.mouseX;
  giMouseYLast = oEvent.target.mouseY;

 } 
]]></mx:Script>
 <mx:HBox>
  <mx:Label id="lblLastMouseDown" text="Last Mouse Down"  color="#FFFFFF" fontSize="18" />
  <mx:Label id="lblLastMouseDownCoord" text="" color="#FFFFFF" fontSize="18" />
 </mx:HBox>
 
 <mx:HBox>
  <mx:Label id="lblCurMousePos" text="Current Mouse Position"  color="#FFFFFF" fontSize="18" />
  <mx:Label id="lblCurMousePosCoord"
    text='{"(" + giMouseXLast + "," + giMouseYLast + ")"}'
    color="#FFFFFF" fontSize="18" />
 </mx:HBox> 
 
</mx:Application>

File Details
Created On Jul, 08, 2005 by Tracy Spratt
Last Modified On Jul, 08, 2005 by Tracy Spratt
Group: Tips and Articles
Flex Versions: All
Category: General
Type: Complete Lesson
Difficulty: Beginner
Keywords: MouseX MouseY
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80