...

View Full Version : onkeypress help?



resin
06-23-2012, 04:11 PM
I'm trying to convert a onkeypress function to pure javascript instead of having to call it in my body tag. For example how would I convert <body onkeypress="return Hello(event)">? I tried document.onkeypress = function() {return Hello(event);}, but that didn't work. Not sure what I'm missing here?

DaveyErwin
06-23-2012, 04:24 PM
document.body.onkeypress
oops, neverMind :(

Lerura
06-23-2012, 04:25 PM
onkeypress triggers when a key is pressd and thereafter released, and do not detect certain keys, depending on the browser.
Whereas onkeyup triggers when the key is released, and detects all keys.
Try changing it to
document.onkeyup = function() {return Hello(event);}

resin
06-23-2012, 04:45 PM
Davey, I had tried that too with no luck. Lerura, I need it to be onkeypress, because I want it to trigger as soon as the key is pressed, even before the key is released. It works properly when in the body tag, just not when I try converting it like I did.

DaveyErwin
06-23-2012, 05:09 PM
Ill just "flesh out" Lerura's suggestion …



<!doctype html>
<head>
<title>keypress</title>
</head>
<body><div>
<div id="testDiv"></div>
<script>
document.onkeydown = function(e) {Hello(e);}
function Hello(e){
e = e || event;
key = e.which || e.keyCode;
alert(key)
}
</script>
</body>
</html>

Lerura
06-23-2012, 05:11 PM
Davey, I had tried that too with no luck. Lerura, I need it to be onkeypress, because I want it to trigger as soon as the key is pressed, even before the key is released. It works properly when in the body tag, just not when I try converting it like I did.

then you must use unkeydown. onkeypress doesn't trigger until you release the key again

felgall
06-23-2012, 10:03 PM
onkeydown and onkeypress trigger one after the other when the key is pressed - the difference is that onkeydown triggers for all keys whereas onkeypress only follows it for keys that actually send a character to the computer (so the alt key for ezample will trigger keydown but not keypress while the H key will trigger both).

onkeyup is the only event triggered when the key is released.

Any event handlers attached to the body tag attach to the window object - not the document.body object or the document object.


window.onkeypress = function() {return Hello(event);}

resin
06-24-2012, 12:21 PM
Unfortunately this isn't working either. Perhaps I should have mentioned this actually is being used in conjunction with onkeydown, so my complete body tag is actually <body onkeypress="return Hello(event)" onkeydown="return Hello(event)">. It's for a Alt+I function. But I did try adding both to my script like so (but still didn't work),

window.onkeypress = function() {return Hello(event);}
window.onkeydown = function() {return Hello(event);}

also tried,

document.onkeypress = function() {return Hello(event);}
document.onkeydown = function() {return Hello(event);}

and,

document.body.onkeypress = function() {return Hello(event);}
document.body.onkeydown = function() {return Hello(event);}

None of these have worked. But the function does run properly when placed within the body tag. But from what I understand it's not good practice to call events from within html tags, so I've been trying to convert them to script instead.

DaveyErwin
06-24-2012, 12:47 PM
see post #5

<!doctype html>
<head>
<title>keypress</title>
<script>
function init(){
var testDiv = document.getElementById("testDiv");
document.onkeydown = function(e) {
e = e || event;
key = e.which || e.keyCode;
testDiv.appendChild(document.createTextNode(key+" down"));
testDiv.appendChild(document.createElement("BR"));
}
document.onkeyup = function(e) {
e = e || event;
key = e.which || e.keyCode;
testDiv.appendChild(document.createTextNode(key+" up"));
testDiv.appendChild(document.createElement("BR"));
}
}
</script>
</head>
<body onload="init()">
<div id="testDiv"></div>
</body>
</html>

DaveyErwin
06-24-2012, 02:31 PM
onkeydown and onkeypress trigger one after the other when the key is pressed - the difference is that onkeydown triggers for all keys whereas onkeypress only follows it for keys that actually send a character to the computer (so the alt key for ezample will trigger keydown but not keypress while the H key will trigger both).

onkeyup is the only event triggered when the key is released.

Any event handlers attached to the body tag attach to the window object - not the document.body object or the document object.


window.onkeypress = function() {return Hello(event);}

I don't know exacly what you are saying here ?
This code does not work in ie8 and below ...

<!doctype html>
<head>
<title>keypress</title>
<script>
function init(){
var testDiv = document.getElementById("testDiv");
window.onkeydown = function(e) {
e = e || event;
key = e.which || e.keyCode;
testDiv.appendChild(document.createTextNode(key+" down"));
testDiv.appendChild(document.createElement("BR"));
}
window.onkeyup = function(e) {
e = e || event;
key = e.which || e.keyCode;
testDiv.appendChild(document.createTextNode(key+" up"));
testDiv.appendChild(document.createElement("BR"));
}
}
</script>
</head>
<body onload="init()">
<div id="testDiv"></div>
</body>
</html>
but this does work …


<!doctype html>
<head>
<title>keypress</title>
<script>
function init(){
var testDiv = document.getElementById("testDiv");
document.onkeydown = function(e) {
e = e || event;
key = e.which || e.keyCode;
testDiv.appendChild(document.createTextNode(key+" down"));
testDiv.appendChild(document.createElement("BR"));
}
document.onkeyup = function(e) {
e = e || event;
key = e.which || e.keyCode;
testDiv.appendChild(document.createTextNode(key+" up"));
testDiv.appendChild(document.createElement("BR"));
}
}
</script>
</head>
<body onload="init()">
<div id="testDiv"></div>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum