-
Get Key Pressed using Event.CHANGE??
Hi guys,
I'm trying to change this as2 code to as3...
Actionscript Code:
myText.onChanged = function() { trace("CHANGED!"); trace("THIS WAS PRESSED: " + String.fromCharCode(Key.getAscii())) }
I've tried the following - but it is no good if the user pastes or cuts text using the mouse:
Actionscript Code:
myText.addEventListener(KeyboardEvent.KEY_UP, setText);
I've also tried TextEvent.TEXT_INPUT -- both KeyboardEvent and TextEvent let me get the users input -- but the problem is the textfield is updated after the listener function is run.
This is no good - as the function is making a change to the users input.
The only way i found to detect if the textfield has been changed (which also works if the user pastes, or cuts with the mouse) is the following:
Actionscript Code:
myText.addEventListener(Event.CHANGE, setText); function setText (e:Event):void { trace("CHANGED") trace("BUT THIS IS THE FULL TEXT " + e.target.text) }
But i need to get the single key the user pressed on the keyboard - like in my AS2 example. I'm kind of looking for a e.keyCode that works with an EVENT, not just a Keyboard or Text event.
Thanks for any help!
-
Senior Member
Give this a try, setText traces the keyboard character and both listeners go to same function.
Code:
import flash.events.KeyboardEvent;
myText.addEventListener(Event.CHANGE, setText);
function setText (e):void {
if(e.type == "keyUp"){
var character:String = String.fromCharCode(e.charCode);
trace(character);
}
}
myText.addEventListener(KeyboardEvent.KEY_UP,setText);
-
That sort of works - but not as well as the AS2 version i had
Your example calls the function twice - and the real problem is still that the textfield is updated before the function ran.
In the AS2 version.. if the user clicked the "." key --- the function checked to see if there already was a dot in the textfield - and if there was, it removed it.
With your AS3 version -- the user clicks the "." and for a second it appears in the textfield - the function runs, and it is removed. So you can see the dot for a second.
Although this works -- it just isnt quite as nice as the AS2 version.
If anyone has any other ideas - that would be great.
if not -- Thanks for the code Steven, it works better than mine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|