How do I activate this?

the100thballoon

Emperor
Joined
Aug 13, 2003
Messages
1,239
How do I design my web page so that when a viewer presses "enter" ("return" to some people) it activates a certain function?


Also, is there a way to make other keyboard keys do certain things or activate certain functions on my website?
 
I would do it by putting in a <form><input type="submit"...></form> button, linked either to a CGI script or a Javascript code, or maybe having it on a php page. If you need something more dynamic yet, consider Flash...
 
IbnSina said:
I would do it by putting in a <form><input type="submit"...></form> button, linked either to a CGI script or a Javascript code, or maybe having it on a php page. If you need something more dynamic yet, consider Flash...
im confused. :confused: So it cant be done with javascript? Please talk to me as if I dont even know what HTML is.
 
when using
Code:
<form>
<input type="submit">
</form>
That creates a button (which has default text Submit, or equivalent in other languages)
This button itself is clickable, also using enter button IIRC, but as it is, it does nothing

Code:
<!doctype blablabla>
<html>
<head>
<script language="javascript">
<!--
function(){
[add what the function does here, but this requires slightly difficult code]
}
-->
</script>
</head>
<body>
(...)
<form>
<input type="submit" onSubmit="function();">
</form>
(...)
</body>
</html>

Here you see I have added some action information to the button. When clicking the button, the webpage will try to call the function called function() from the javascript.
As it is now, it will generate an error message, because a) I havent written this properly, b) there is nothing in the function that describes what the function does.

Google for javascript tutorials if you have need for simple, but limited scripting language.
Google for general tips on html if you are a newbie
Go to w3.org if you are more or less knowledgeable in html/javascript already.
 
Here's some code for a slightly more advanced version of what you are looking for:

Code:
<html>
<head>
<title>Test</title>
<script language="JavaScript">
function getKey(keyStroke) {
   var keyCode = (document.layers) ? keyStroke.which : event.keyCode;
//  var keyString = String.fromCharCode(keyCode).toLowerCase();
if(keyCode == "13") { 
alert("You Hit Enter");
} else {
alert("Wrong");
}



}
document.onkeypress=getKey;
</script>
</head>
<body>
</body>
</html>

Give it a shot to view the effects. Hitting enter triggers a function, i threw in an else statement so you can see what happens when you hit something else. I commented out keystring but you can use it if you want. It will equal whatever letter is pressed. You could use this for a million different things. I've seen people use it for site navigation, javascript games, the possibilities are vast. 13 is the keycode for enter, if you want to find out other keycodes(shift, alt, ctrl, etc...) then either google them or else set it to alert(keyCode) when you press a key and write down all of the keycodes you are interested in.
 
gonzo_for_civ said:
Here's some code for a slightly more advanced version of what you are looking for:

Code:
<html>
<head>
<title>Test</title>
<script language="JavaScript">
function getKey(keyStroke) {
   var keyCode = (document.layers) ? keyStroke.which : event.keyCode;
//  var keyString = String.fromCharCode(keyCode).toLowerCase();
if(keyCode == "13") { 
alert("You Hit Enter");
} else {
alert("Wrong");
}



}
document.onkeypress=getKey;
</script>
</head>
<body>
</body>
</html>

Give it a shot to view the effects. Hitting enter triggers a function, i threw in an else statement so you can see what happens when you hit something else. I commented out keystring but you can use it if you want. It will equal whatever letter is pressed. You could use this for a million different things. I've seen people use it for site navigation, javascript games, the possibilities are vast. 13 is the keycode for enter, if you want to find out other keycodes(shift, alt, ctrl, etc...) then either google them or else set it to alert(keyCode) when you press a key and write down all of the keycodes you are interested in.
Thanks! You are awesome!
 
Your welcome, I try ;)
 
Top Bottom