[Javascript] Keystroke event

Nick Fitzsimons nick at nickfitz.co.uk
Fri Mar 24 05:42:25 CST 2006


> Hi
>
> Does anyone know a good reference or have a nice examples to catch the
> keystroke ctrl+s in the browser
> I wrote this simple function but everytime the browser try to do a save.
>
<snip>
>
> function keyDown(e) {
>     var keycode = e.which
>     if(keycode==17){
>         document.onkeydown = trigger_ctrlS;
>         return true
>     }
> }
> function trigger_ctrlS(e){
>     var keycode = e.which
>     if(keycode==83){
>         alert("ctrl s: ")
>         document.onkeydown = keyDown
>         return true
>     }
> }
> document.onkeydown = keyDown


The usual way of cancelling the default action for an event is to return
false from the event handler; try changing your "return true;" in the
above to "return false;" and see if that helps.

If that doesn't work, as you say it's for Firefox, try visiting the
Firefox/Mozilla developers site:
<http://developer.mozilla.org/en/docs/DOM:event#Introduction>
explains the "preventDefault" method, which might do what you want.

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/




More information about the Javascript mailing list