Jump to content

Key Presses


Darkness
 Share

Recommended Posts

Is there a way to make it so, whenever a user presses a certain key (such as the down key), it submits a button?For example, I have this code:

<input type='submit' value='Submit'>

Whenever somebody presses the "down" key, I want it to submit. Is there a way to do this?Thanks!

Link to comment
Share on other sites

This is a javascript thing.First you need to capture the keystrokes and check for the down arrow. This link (just one of many) explains most of that: http://www.geekpedia.com/tutorial138_Get-k...JavaScript.htmlThen you need to submit the form. That would work something like this:document.getElementById("my_form").submit()But first, ask yourself REAL HARD if you really want to mess with the commonly-accepted user interface for submitting forms. Users are very resistant to change.

Link to comment
Share on other sites

If you use the submit button's value in your server side processing (and I imagine you do since you want to use a key that's not normally associated with submittion), you'll need to add a hidden form element with the same name and value before submittion.If you really want to do an "action" with the key instead of simply submitting the form (say, move something down), I suggest you try to do it in JavaScript (with or without AJAX) instead of submitting the form directly and refreshing/changing the page in the process.If you really want to submit a form (say, a search query), detect the enter key. As said already, users are very resistant to change, and if you try to force such a change, you'll likely just annoy your users.

Link to comment
Share on other sites

Well, it's not technically to submit a form. It's hard to explain.Is there a way to submit a link instead of a field? Like, when the user pressed "down", it would be as if they clicked on the link (it would bring them to http://blah.com);

<a href='http://blah.com'>Blah</a>

Image submit fields are inefficient, so I've decided to use a link instead of a submit field.Thanks.

Link to comment
Share on other sites

The easiest way is to alter the window.location property upon the proper key press, like so:

window.location = 'http://blah.com';

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...