|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Javascript: OnKeyPress
Hello, hello.
I was wondering if somebody could tell me about the javascript onKeyPress event, I've been searching around for it but I haven't really found anything that explains what I need to know. What I am wondering is simply: if I have a submit-button, with the value "Purchase!" for instance, is it possible to have that button to submit whenever I press "P" in my keyboard? Hoping for an answer, cheers! |
|
#2
|
||||
|
||||
|
From what I've read, you'd do something like
<html> <head> function printpage() { if (window.event.keyCode[something like that - you'll have to read up on that yourself] == '[p's ascii code]') window.print() } </head> <body onkeypress="printpage()"> foo </body> </html> You might just be better of using a print button, with the window.print() method. Checking for a character everytime a key is pressed isn't v. efficient. Or you can do: window.onload(window.print()) Bit of doco here on onkeypress |
|
#3
|
|||
|
|||
|
I got the onKeyPress function to work, with a little experimentation. But since I don't really know javascript I have another problem .. If the user presses "P" I want the form to submit automaticlly ..
Ex: Code:
if (key_pressed == P) {
submit form!
}
How do I do that? |
|
#4
|
|||
|
|||
|
I've worked out so that it works, except for one little thing. When the forms submits using a key on the keyboard I can't use any of the variables with PHP, just like they were not set.
This is my code, all for testing: Code:
<html>
<head>
<title>Untitled Document</title>
<script type="text/JavaScript">
<!--
function doKey($key) {
if ($key == 112) {
alert("You pressed key p" + $key);
document.forms["keyvalue"].submit();
} else {
alert("You just pressed a key");
}
}
-->
</script>
</head>
<body onKeyPress="doKey(window.event.keyCode)">
<?php
if (isset($test_submit)) {
echo "Hello world!";
}
?>
<br><br>
<form id="keyvalue" method="post" action="">
<input type="submit" name="test_submit" value="submit">
</form>
</body>
</html>
Any thoughts why it doesn't work? I'd be very thankful for any help ![]() EDIT: Just some info', when I press the submit-button with the mouse .. the variables gets set. Odd, I say. |
|
#5
|
|||
|
|||
|
Why on earth do you want to break convention just to submit a form? Users are familiar with submitting via buttons ro the enter key; why alienate them ?
|
|
#6
|
|||
|
|||
|
It's not about alienating anything, I just want to try it. I am going to use for a project of my own, a game in PHP. And I figured that it would be alot easier to be able to use the keyboard instead of having to use the mouse to click all the time.
Besides, in games aren't we pretty adjusted to using buttons? ![]() |
|
#7
|
||||
|
||||
|
Yeah - that's what I'm thinking too meth.
What's wrong with using most browsers builtin "ctrl + P" function (to print)? It's a standard control function across the Windows platform. Last edited by stumpy : February 11th, 2003 at 04:57 AM. |
|
#8
|
||||
|
||||
|
Your problem here is at the browser level, not PHP. When you click the submit button, the name/value pair "test_submit","submit" is sent along with any other name/values in the form (eg "username","silveria")
But if you submit the form with javascript, any submit (and, I would guess, reset) buttons are not sent. Why? Well, it's really quite logical. You see, you can have multiple submit buttons on your form (apologies if this is no news to you!) - I think they can even have the same name and different values - the idea being that you can then have two buttons after your contact details, one saying "Donate $500" and the other saying "Donate all your savings", and your PHP script can then pick up which button was clicked with the autovariables. If you want a standard has_been_submitted variable and none of the other fields in your form is always reliably set (not 0, not false, not blank string) then put in a hidden field: <input type="hidden" name="form_has_been_submitted" value="indeed it has" /> NB This also saves you hassle later on if you change your <input type="submit" />s to <button type="submit"></button>s - why? because Netscape and IE disagree about which part of the button element gets passed as the value of the name/value pair. Stick with hidden fields - it may seem ugly but it's safer! If I'm feeling particularly lazy, I sometimes write a standard hidden field into a separate PHP file and then include_once() it - that way I don't end up putting in a typo in the name field and it looks tidier too. Hope that helps, Al |
|
#9
|
|||
|
|||
|
Hi. I'm not sure wheather this is what you're looking for. If you want to submit the form when you press the key P you should do:
<script> function print() { if (event.keyCode == 80) { document.[form name].submit } } </script> <body onKeyDown="print();"> . . . . </body> Hope this works. You should check the key code...but the rest works like a glub. Remember that event.keyCode only works on IE, there is another sentence for Netscape (which I don't remember now) ![]() Quote:
|
|
#10
|
||||
|
||||
|
Wow, a two year old thread...
![]() How come the accesskey attribute wasn't mentioned? <input type="submit" value="whatever" accesskey="p" /> The downfall to this is one would have to press ALT+P instead of simple P... However this way is accessible, and rumoured to become mandatory in future XHTML drafts... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Javascript: OnKeyPress |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|