# On Keypress ENTER

**URL:** <https://forum.kirupa.com/t/on-keypress-enter/14703>\
**Category:** flash\
**Created:** [March 1, 2003, 5:56pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703 "2003-03-01T17:56:15Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![cookie](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@cookie](https://forum.kirupa.com/u/cookie)\
**Post date:** [March 1, 2003, 5:56pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/1 "2003-03-01T17:56:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 7:12pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/2 "2003-03-01T19:12:51Z")

</div>

Create an input textfield on the stage and give it the INSTANCE name of “textBox” (no quotes).

Add these actions to the frame…

```php
_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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 7:24pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/3 "2003-03-01T19:24:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 7:51pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/4 "2003-03-01T19:51:17Z")

</div>

```auto

Key.addListener(myTextField)
myTextField.onKeyDown = function(){
	if (Key.isDown(Key.ENTER) && eval(Selection.getFocus()) == this){
		trace("enter Pressed in textField "+ this);
	}
}

```

😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 8:04pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/5 "2003-03-01T20:04:20Z")

</div>

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! 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 8:13pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/6 "2003-03-01T20:13:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 8:42pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/7 "2003-03-01T20:42:11Z")

</div>

Sorry senocular but I’m having some trouble despite your explanation! First of all let me try to clarify what I want to do:

1. I want to enter text into an “input textfield”
2. When you press “enter” it checks the inputted text with a variable called “test”  
If the input is equal to test then execute function1 and if not true execute function2
3. If any other key is pressed nothing happens and the input field is only checked once!

=)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 10:22pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/8 "2003-03-01T22:22:38Z")

</div>

```auto

Key.addListener(inputtextfield)
inputtextfield.onKeyDown = function(){
	if (Key.isDown(Key.ENTER) && eval(Selection.getFocus()) == this){
		if (this.text == test) function1();
		else function2();
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 10:37pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/9 "2003-03-01T22:37:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 10:49pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/10 "2003-03-01T22:49:23Z")

</div>

what do you mean by all the time?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 10:52pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/11 "2003-03-01T22:52:42Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 11:07pm UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/12 "2003-03-01T23:07:24Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 2, 2003, 12:06am UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/13 "2003-03-02T00:06:23Z")

</div>

> \*Originally posted by senocular \*  
> \*\*

```auto

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 :evil: hehe. I just figured the method I used would be easier for cookie to understand 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 2, 2003, 12:20am UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/14 "2003-03-02T00:20:59Z")

</div>

> \*Originally posted by lostinbeta \*  
> \*\*LOL… I know the onKeyDown method in case you were implying I didn’t :evil: hehe. I just figured the method I used would be easier for cookie to understand 🙂 \*\*

give me some credit here, I knew you knew 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 2, 2003, 12:23am UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/15 "2003-03-02T00:23:30Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 2, 2003, 12:38am UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/16 "2003-03-02T00:38:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 2, 2003, 12:40am UTC](https://forum.kirupa.com/t/on-keypress-enter/14703/17 "2003-03-02T00:40:24Z")

</div>

exactly!
