JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 6th, 2003, 08:32 PM
Silveria Silveria is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 7 Silveria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old February 6th, 2003, 09:29 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
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

Reply With Quote
  #3  
Old February 7th, 2003, 12:39 PM
Silveria Silveria is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 7 Silveria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old February 10th, 2003, 05:46 PM
Silveria Silveria is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 7 Silveria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #5  
Old February 11th, 2003, 03:54 AM
meth meth is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 meth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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 ?

Reply With Quote
  #6  
Old February 11th, 2003, 04:50 AM
Silveria Silveria is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 7 Silveria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #7  
Old February 11th, 2003, 04:55 AM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
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.

Reply With Quote
  #8  
Old February 5th, 2004, 07:21 AM
almcnicoll's Avatar
almcnicoll almcnicoll is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Montargis, France
Posts: 6 almcnicoll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via MSN to almcnicoll
Lightbulb automatic form submission

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

Reply With Quote
  #9  
Old January 4th, 2005, 06:32 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 14
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:
Originally Posted by Silveria
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!

Reply With Quote
  #10  
Old January 5th, 2005, 09:00 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
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...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Javascript: OnKeyPress


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap