OK next AS obstacle to overcome! How can I have an input textfield and when you type something and then press “ENTER” on the keyboard something happens? Just need to know how to make an enter listener!
Thanks
OK next AS obstacle to overcome! How can I have an input textfield and when you type something and then press “ENTER” on the keyboard something happens? Just need to know how to make an enter listener!
Thanks
Create an input textfield on the stage and give it the INSTANCE name of “textBox” (no quotes).
Add these actions to the frame…
_root.onEnterFrame = function() {
if (Key.isDown(Key.ENTER)) {
trace(textBox.text);
}
};
It checks if the enter key is pressed and if it is pressed it traces the text inside the textbox.
Just a quick example.
Thanks lost, just what I needed! However, I don’t want the user to be able to press “enter” at any time but when text has been entered into the input field!
Key.addListener(myTextField)
myTextField.onKeyDown = function(){
if (Key.isDown(Key.ENTER) && eval(Selection.getFocus()) == this){
trace("enter Pressed in textField "+ this);
}
}
Don’t fully understand your code! What does this part do:
eval(Selection.getFocus()) == this)
How would I execute a function which I defined earlier, for example
test = function(){…}
Thanks for the help senocular! My actionscript library is growing!
Selection is a Flash defined object that lets you control/get information about whats selected in Flash. When you use the getFocus method in Selection, it tells you what is currently focused in Flash, or what is selected, or where the cursor is (in an input field). It tells you this as a string though. So if your cursor was in the myInput textfield you made on the stage, Selection.getFocus() would return something like “_level0.myInput” - as a string. What this does for us uis gives us a way to check if the cursor is in this textfield or not so we can only run the function needed when the Enter key is pressed while in this testfield and not anywhere else. However, as a string we cant compare it directly to the object itself because thats not a string but the actual textfield object. What eval does is converts a string into an object so
eval("_level0.myInput") == _level0.myInput
(assuming the textfield is in the _root of the main level) - eitherway using eval on the getFocus will give you an object reference of whats focused so
eval(Selection.getFocus()) == this
will let you know if this is focused or not - or if the textfield has been clicked in and the cursor is there ready for typing.
Sorry senocular but I’m having some trouble despite your explanation! First of all let me try to clarify what I want to do:
=)
Key.addListener(inputtextfield)
inputtextfield.onKeyDown = function(){
if (Key.isDown(Key.ENTER) && eval(Selection.getFocus()) == this){
if (this.text == test) function1();
else function2();
}
}
Yes, it works! Just a thing though! How do I prevent the possibility of pressing enter all the time? You can only evaluate once! Sorry for the simple questions!
what do you mean by all the time?
Well at the moment you can change the input and then press enter to execute the functions again! I just want you to input and then press enter and then execute the functions once! =) P.S. You can change the input but once you press enter that’s final!
I don’t know if this is an efficient way of doing it but i added the following code within the if statement i.e.
if(…){
function1();
Key.removelistener(textfield);
}
*Originally posted by senocular *
**
Key.addListener(myTextField)
myTextField.onKeyDown = function(){
if (Key.isDown(Key.ENTER) && eval(Selection.getFocus()) == this){
trace("enter Pressed in textField "+ this);
}
}
**
LOL… I know the onKeyDown method in case you were implying I didn’t hehe. I just figured the method I used would be easier for cookie to understand
*Originally posted by lostinbeta *
**LOL… I know the onKeyDown method in case you were implying I didn’thehe. I just figured the method I used would be easier for cookie to understand
**
give me some credit here, I knew you knew
Yeah…uhh… I know you knew I knew, but at first I wasn’t too sure if I knew you knew I knew. Ya know?
After typing this… the word knew makes no sense to me :sleep:
I know what you mean I knew one time where I knew that if I used a word like knew a lot I wouldnt know what knew knew because I knew knew only as a slur of sounds if you knew or know what I mean… or knew… ya know?
exactly!
:: Copyright KIRUPA 2024 //--