Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text input detect enter key press?

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
0
0
US
I have created the following math game

The game is working great, but there is one thing I don't like about it. If the player enters their answer and presses enter, it inserts a return into the input field. I would rather that fire the same code as the button to check the answer.

Does anyone know if that is possible and if so can you provide sample Action Script code?

Many thanks.
Mark
 
In my continued research I have found the following:

To capture the Enter (or Return) key, use either a button handler, such as:

on (keyPress "<Enter>") {
// Respond to Enter key press (e.g., submit a form)
}
or a keyDown handler, such as:

onClipEvent (keyDown) {
if (Key.getCode( ) == Key.ENTER) {
// Respond to Enter key press (e.g., submit a form)
}
}

The problem is that these actions are for buttons or movie clips. I need to apply an action to a input text box. Does anyone know if there is a way to do that?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Looks like I figured part of the problem out. I am able to get my input box to fire my actions with the following code:

Code:
listenForEnterObject = new Object();
listenForEnterObject.onKeyDown=function(){
    if(Key.isDown(Key.ENTER)){
       _root.gotoAndStop("report");
    }
}
Key.addListener(listenForEnterObject);

The problem that I have now is that once I get to the report frame the key press listener seems to still be functioning. When enter is pressed on the report frame, it is starting the entire flash game over from the beginning. Is there a way for me to block that once I reach my report frame? I have tried adding the same code in and simply change the destination, but it is ignored.

I have a button on the report frame, actions for that from have the following code for the button:

Code:
btnBack.onRelease = function(){
	if (numTries <8) {
		_root.gotoAndStop("Solve");
	} else {
		numRight = 0;
		numTries = 0;
		_root.gotoAndStop("Selection");
	}
}

This works great. I want enter to do the same thing as the button when on that frame.

I tried the following:

Code:
listenForEnterObject = new Object();
listenForEnterObject.onKeyDown=function(){
    if(Key.isDown(Key.ENTER)){
	if (numTries <8) {
		_root.gotoAndStop("Solve");
	} else {
		numRight = 0;
		numTries = 0;
		_root.gotoAndStop("Selection");
	}
    }
}
Key.addListener(listenForEnterObject);
But as stated above, pressing enter just starts everything all over again.

I am wondering if there might be a way to change the original function so it can check which is the current frame and based on that direct the user to the proper frame. Is there a way in action script to return the current frame name?

Any help is greatly appreciated.
 
Problem solved. The above actually did the trick. When I published to my web server I did not have the problem with the enter key on the report page.

Wish I understood why it was different when I tested locally on the machine I developed the game in but I am not complaining.

I hope the above solution helps others out.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Boy do I feel dense. Just realized that the enter key is the play button on the local flash player when you press Ctrl+Enter from within Flash...mystery solved.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top