riaxe snippets
Using mouse position from Javascript into Flex
MouseEvents can be captured from Javascript and can be used for some amazing things in Flex. I have used ExternalInterface and FlexBridge to capture mouse positions from a html page to rotate a 3D Flex Cube as shown below:

The demo of the same can be seen here
The following javascript is used to capture the mouse move positions:
document.onmousemove = getMouseXY;
var flexApp;
// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0
// Main function to retrieve mouse x-y pos.s
function initCallback() {
flexApp = FABridge.mousecapture.root()
alert("initCallback");
}
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft
tempY = event.clientY + document.body.scrollTop
} else { // grab the x-y pos.s if browser is NS
tempX = e.pageX
tempY = e.pageY
}
// catch possible negative values in NS4
if (tempX < 0){tempX = 0}
if (tempY < 0){tempY = 0}
// show the position values in the form named Show
// in the text fields named MouseX and MouseY
document.Show.MouseX.value = tempX
document.Show.MouseY.value = tempY
return true
}
function getMousex(){
return tempX
}
function getMousey(){
return tempY
}
The mouse positions can be recieved in Flex by calling getMousex() and getMousey() methods using External Interface.
Related posts:
| Print article | This entry was posted by Susrut Mishra on January 29, 2009 at 12:59 pm, and is filed under actionscript, flash, flex, Javascript. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |



about 2 years ago
I do like this, neat and useful for *something* – not sure what but i do like things like this.
assuming you could also control multiple swfs at the same time.. or even from one flex app to js then to another flex app – infact you could even do one window to another I guess
about 1 year ago
Hello all.
I want to access flex file through Javascript. Through JS i want click and Drag a Button and after the on specific position click on that Button.
Thanks,
Ajay
about 1 year ago
Yes I guess we cannot do stuffs like click a button in flex using JS but ya we can certainly find out some kind of workaround using some kinda hack techniques using DIVs in the HTML and dispatching events in flex connecting with the JS.