Actionscript : Mouse.hide() / Mouse.show()
April 30th, 2006 . by polyGeekHere’s a quick bit of code for toggeling between Mouse.hide() and Mouse.show() by using a keypress.
So if you need to know here’s how it works:First create an Object that you can attach a listener to.
var keyList:Object = new Object();
Now use your newly created object to assign a function to the “onKeyDown” event.
keyList.onKeyDown = function():Void {
I’m tracing out the Key.getCode() so that you can see the values if you want to change the key assignment.
trace("Key.getCode = " + Key.getCode());
Looking to see if the NumPad minus key was pressed.
if(Key.getCode() == 109) { // this is the NumPad minus key
I could have used an if/else statement here just as well but the ternary operator is just so tidy for this sort of thing. It works by taking a statement that can be resolved to true/false and then the conditional operator “?”. If the statement is true then it accesses the first statement after the “?” and if the statement is false then it accesses the second statement, the one after the “:”. So when you have an if/else that has just a single statement for each condition you can use the ternary to tighten up your code. You can also use the ternary as an if/then. In that case the second statement can just be null.
(Mouse.show() == 1) ? Mouse.hide() : Mouse.show();
}
}
All done now. Just tell Key event broadcaster to add the keyList Object to it’s list of Objects to be notifed if there is a keyEvent.
Key.addListener(keyList);



