Flex Examples
Detecting when a mouse leaves a Flex application
The following example shows how you can detect when the mouse leaves the bounds of a Flex application by listening for the stage object’s mouseLeave event.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/22/detecting-when-a-mouse-leaves-a-flex-application/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white"
applicationComplete="init();">
<mx:Style>
Alert {
modalTransparencyColor: black;
modalTransparency: 0.6;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
private var _isMouseInSWF:Boolean = false;
private var alert:Alert;
[Bindable]
private function get isMouseInSWF():Boolean {
return _isMouseInSWF;
}
private function set isMouseInSWF(value:Boolean):void {
_isMouseInSWF = value;
}
private function init():void {
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
stage.addEventListener(Event.MOUSE_LEAVE, stage_mouseLeave);
}
private function stage_mouseLeave(evt:Event):void {
isMouseInSWF = false;
if (alert) {
PopUpManager.removePopUp(alert);
}
alert = Alert.show("GONE");
}
private function stage_mouseMove(evt:MouseEvent):void {
if (!isMouseInSWF) {
if (alert) {
PopUpManager.removePopUp(alert);
}
// alert = Alert.show("BACK");
isMouseInSWF = true;
}
}
]]>
</mx:Script>
<mx:Label text="isMouseInSWF: {isMouseInSWF}" />
</mx:Application>
View source is enabled in the following example.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
14 Responses to Detecting when a mouse leaves a Flex application
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Perfect! I talked to a few people about this, letting them know it’s possible. I think this is perfect for better user interactivity, and has the right application for business/financial displays that would blur sensitive data when the user is not focused in the application, or trying to make a screenshot, etc.
Peter,
Why use MOUSE_OVER/isMouseInSWF combo instead of just a MOUSE_OVER event?
Craig
ps. Thanks for the great examples.
How to use this to module ?
“stage” This is valid do all application ok?
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
How to set to module current ?
I’m using
addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
addEventListener(Event.MOUSE_LEAVE, stage_mouseLeave);
But Event.MOUSE_LEAVE not functional to deactivate
Marcio,
What use sdk version of flex?
I had completed to test on sdk version 3.6 in flex4
I’ve noticed that when you interact with the browser outside the flash movie, that funkiness happens inside the movie.
e.g. Highlight the view source line
Great example, but it does have limitations.
When adding a slider, if you hold the slider thumb down then keep the mouse button pressed and move off the stage, it fires when you release the mouse button.
However, if the swf is in a table, and you do the same, the event never fires!
Kristian,
Can you please file a bug at http://bugs.adobe.com/flex/ and include a simple test case, if possible. That may be a bug with the Flex Slider control or possibly Flash Player.
Peter
PS: If you can also post the bug number here, a few of us can vote/subscribe to the issue also.
Hi Peter.
I’ve logged the bug under the Flash Player, as it also happens with Flash CS3, so it’s not limited to Flex.
Bug number is FP-733.
Cheers,
Kristian.
A similar error to the one Kristian documented happens if you have ‘wmode’ set to ‘transparent’, if you leave the stage with the mouse pressed and release outside the event won’t fire, then when you enter the stage again the slider thumb is stuck to the mouse, and when you release you get an error.
wmode set to transparent has 20+ errors associated with it, avoid it at all costs :) (it’s sometimes unavoidable)
Likewise, it seems that this is limited if you alt+tab to a different window while your mouse is done. It seems as though since the browser is no longer the active window when the mouse up occurs it can’t detect it and therefore no MOUSE_LEAVE event is dispatched.
Hello Everyone,
I have ran into issues with using this. For example, the mouse leave event is dispatched when a context menu is displayed via right click. So, if you are in the center of the stage and right click, the mouse leaves the stage. So, I modified this example by adding a “mouse border” around the stage and I check to see if the mouse is within the border before it leaves the stage.
I hope that this helps….
var mouseOutOnX:Boolean = ( mouseX (stage.width – 10));
var mouseOutOnY:Boolean = ( mouseY (stage.height – 10));
if ( ( mouseOutOnX || mouseOutOnY) )
{
…….
It should be
vrathore.blogspot.com
Peter – I cut and paste your code to try in AIR to trigger a state change – it successfully changes from initial state to isMouseInSWF state but does not change back on exit. Any ideas?
thanks, Max