PDA

View Full Version : converting one keypress to two keypresses of a different nature


canadianjameson
06-10-2005, 10:39 PM
I have a friend who has a form which he does not want the user to hit "enter" to submit, but rather press the submit button (he has his reasons, i suppose). I have a script here that converts any enter keystroke into a "tab". what i need help with is the following.

the submit button has this code:
<input type="image" alt="Sign In" name="SignIn" border="0" src="/common/images/english/btn_signin.gif" tabindex="4" />.
The password field is tab-index 2 (i don't know if he would be willing to make it 3).

could someone help me modify this code to catch the Enter keypress if the cursor is in the password field, and convert that keypress to a click on the submit button? i figure either we could use the "tab + tab + Spacebar" approach, or another if its cross-browser.


<script language="javascript" type="text/javascript">
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
if (!evt) return true;
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13 && node && node.type=="text") {
evt.keyCode = 9; //tab
return true;
}
}
document.onkeydown = checkCR;

</script>

Dolphin123
06-11-2005, 09:44 PM
Noooooo Don't hit enter

No offense but what is this person afraid of happening, the whole computer crashing. :D Maybe a traumatic experience in the past?

I don't have much experience in javascript, but why not creat a pop-up message when the person hits the enter key, that says something like "To submit form please press submit button on screen" ((using the mouse) button,) well actually by pressing it. The mouse button)

Maybe use:

onkeypress = script [CT]
The onkeypress event occurs when a key is pressed and released over an element. This attribute may be used with most elements.

http://www.w3.org/TR/REC-html40/interact/scripts.html#adef-onkeypress
(all about forms)

Then assign the enter key to do the action you want it to, or turn a div on or off.

If someone has jscript disabled the whole thing might not work.

canadianjameson
06-11-2005, 09:50 PM
well dolphin, because with the same amount of code i could just convert the enter keypress into a click on the button.

and that accomplishes my goal without aggravating the user.

Thanks for the suggestiong though.

anyone?

Dolphin123
06-11-2005, 09:52 PM
See my edited post above. ;)

Wouldn't that work?

Anyway good luck! :thumbsup:

Dolphin123
06-11-2005, 09:59 PM
Try this:

Javascript: Triggering a Function on Enter Key (http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21274640.html)

Disable "Enter" Key in Form script (http://www.dynamicdrive.com/dynamicindex16/disableenter.htm)

<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function handleEnter (field, event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

</script>

:thumbsup:

brothercake
06-11-2005, 10:29 PM
You musn't do this mate, really - if you do, then what if somebody can't use a mouse at all? How do they submit the form?

Dolphin123
06-11-2005, 11:09 PM
How would someone navigate through the website then without a mouse?
Sincere question

Maybe change the enter button assigment then adding a 'F1' key submit function and put a message on the website that pressing 'F1' will submit the form?

'F1' meaning F1 through F12

Or do some people already have special assignments for function keys?
Sincere question

canadianjameson
06-12-2005, 04:46 PM
i won't brother, that being said do you see any reason why i shouldn't catch the enter keypress, if in that particular form field, and have it submit the form via a mouseclick?

I appreciate the help you've offered dolphin, its given me some interesting ideas

canadianjameson
06-12-2005, 05:31 PM
okay i've made a few changes to the script, lemme know what you think. I need this to be 100% cross browser so i put two examples... please tell me which one is most compatible.

also, i need to modify them to only work if the focus is in this field:
<input type="PASSWORD" name="Q1" value="" size="18" maxlength="32" tabindex="2" onfocus="EnterOn=true;" onblur="EnterOn=false;" />

ex1

<script language="javascript" type="text/javascript">
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
if (!evt) return true;
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13 && node && node.type=="text") {
document.rbunxcgi.SignIn.click();
return(false); }
}
document.onkeydown = checkCR;

</script>

ex2

<script language="javascript" type="text/javascript">
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
if (!evt) return true;
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13 && node && node.type=="text") {
evt.keyCode = 9; //tab
evt.keyCode = 9; //tab
evt.keyCode = ?????; //Whats the keycode for spacebar?
return(false);
}
}
document.onkeydown = checkCR;

</script>

any thoughts?

looka
06-13-2005, 07:03 AM
why do yopu bother document?

simply attach your function to form.

html:
blahblah

script:
document.sth.onkeypress=document.sth.onkeyup=checkCR;

function:
chechCR(e){
//im not sure - perhaps u should add something here.
if(!e) e=window.event;
if(e.keyCode==9) {
//u can submit form here:
//document.sth.submit();

//or ask what to do
if(window.prompt('do i have to submit the form?')) document.sth.submit();
}
}

brothercake
06-19-2005, 11:21 AM
How would someone navigate through the website then without a mouse?
Using the tab key to move around, and the enter key to activate links and buttons.

Some browsers have more advanced toolsets - like Opera has "spatial navigation" whereby you can hold down the Shift key and then move around the page 2-dimensionally using the arrow keys.

canadianjameson
06-19-2005, 09:29 PM
i see :D

i have sorta abandonned the idea as per your suggestion anyways, so thats cool