且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firefox onkeydown检测按键

更新时间:2023-11-29 20:35:28

没有建议的解决方案,因为我需要添加我自己的参数除了必要的事件参数为Firefox获取keyCode。无论如何,帮助,谢谢你们。我为未来的读者提供了最终的解决方案:

最后,我只需要将event参数添加到调用者:

 < input type =textonkeydown =TextOnKeyDown('myPrefix',event); /&GT; 

和处理程序:

  function TextOnKeyDown(prefix,evt){
var key =(evt.which)? evt.which:evt.keyCode;
...
}

不知何故,不知道为什么。


From an input text I need to call a function to capture the onkeydown event and I need to pass a parameter to that event, for example:

<input type="text" onkeydown="TextOnKeyDown('myPrefix');" />...

Then in that function I need to know what key was pressed. I use

function TextOnKeyDown(prefix) {
    var key = event.keyCode; 
    ...
}

This works like a charm in IE, but not in Firefox. I've read that for firefox you have to pass an argument to the handler and then use it to get the key, something similar to:

<input type="text" onkeydown="detectKey(event)"/>
...
function detectKey(e){
     var key = (document.all) ? e.keyCode : e.which; 
     ...    
} 

But I cannot get to pass my parameter and the needed event parameter for firefox. Any suggestions?

None of the proposed solutions worked as what I needed was to add my own parameter apart from the necessary "event" parameter for Firefox to get the keyCode. Anyway the helped, thanks guys. I put the final solution for future readers:

In the end I just had to add the "event" parameter to the caller:

<input type="text" onkeydown="TextOnKeyDown('myPrefix', event);" />

and the handler:

function TextOnKeyDown(prefix, evt) {    
   var key = (evt.which) ? evt.which : evt.keyCode;
...
}

Somehow it didn't work before, I don't know why.