Forum Moderators: open
Could anyone give me some practical pointers to handle the 'shiftkey' event for Gecko based browser/Netscape4? (Gecko is more critical at the moment)
The code I have been testing with is as below:
Function:
function fillform(txt) {
if (event.shiftKey)
{event.returnValue = false;
alert('shift on');
MM_changeProp('fieldone','','value',txt,'INPUT/TEXT')}
else {alert('click only');
MM_changeProp('fieldtwo','','value',txt,'INPUT/TEXT')}
}
HTML:
<a href="#" onClick="fillform('mytext')">click</a> Many thanks in advance,
RJW
function chkSearch(e,form) {
if (window.Event) { var whichCode = e.which; } //NN
else if (e.type == "keypress") { var whichCode = e.keyCode; } // IE
else { return; }
if (whichCode == 13) { // only if enter is pressed
form.submit();
}
}
RJW
Such an event object is not passed by IE. The arg will be undefined. IE uses a global
event property, carrying the event for the current thread. "Normalisation" is acheived with a quick "short-circuit" evaluation, which in JS logic will produce the defined object. The same thing is done for the (which¦keyCode) property of this event object.
I think Moz browsers have a
keyCode property too, but it acts differently, so we give the event the opportunity to reveal its which instead first. Try this:
(but replace broken ¦¦ with uncorrupted ones)
<html><head><title>TEST</title>
<script type="text/javascript">
document.onkeypress = function(e)
{
e = e¦¦event;
var code = e.which¦¦e.keyCode;
alert(code)
}
</script>
</head>
<body>
<h3>Press a key</h3>
</body>
</html>
PS Standards browsers have a global
[b]E[/b]vent object. This is the constructor for the event objects that are passed into handlers.
IE, FF & Opera all respond to [eventObj].shiftKey.
Some browsers will decide not to fire events for certain keys depending on whether the event is onkeypress, onkeydown or onkeyup. With onkeypress, I have just noticed that Opera fires an event for the shift key itself. This could be useful, but it could also mess up a script that accepts the behaviour of the other browsers. The shift key, in normal circumstances, should be rejected - else we'll get 2 events for the price of one.
document.onkeypress = function(e)
{
e = e¦¦event;
var code = e.which¦¦e.keyCode;
/* reject initial shift use */
if(code==16) return;
var shift = e.shiftKey;
alert('code: '+code+'\n'+'shift: '+shift)
}
[blue]event[/blue]. This means that IE recognises it too (but for a different reason). The
event keyword can be placed anywhere in the passed arguments. DEMO:
<html><head><title>TEST</title>
<script type="text/javascript">
function test(arg0, e)
{
alert('arg0:'+arg0+'\n'+'e: '+e.type);
}
</script>
</head>
<body>
<h3 onclick="test('a',[blue]event[/blue])">Click Me</h3>
</body>
</html>
Regards,
RJW
function fillform(txt,e) {
e = e¦¦event;
var code = e.which¦¦e.keyCode;
/* reject initial shift use */
if(code==16) return;
var shift = e.shiftKey;
//alert('code: '+code+'\n'+'shift: '+shift+'\n'+'txt: '+txt)
if (shift) {MM_changeProp('fieldone','','value',txt,'INPUT/TEXT');}
else {MM_changeProp('fieldtwo','','value',txt,'INPUT/TEXT');}
} Many thanks for all your help, much appreciated! : )
Regards,
RJW