Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onkeyup and onkeydown not catching anything

Can someone tell me whats going on? My console isn't throwing any exceptions.

    function onKeyDown(){
                    var e = event.keyCode;

                    //if (e==87 /*w*/){
                        up = true;
                        throw('up'); 
                    //}

                    if (e==65 /*a*/){
                        left = true;
                    }

                    if (e==83 /*s*/){
                        down = true;
                    }

                    if (e==68 /*d*/){
                        right = true;
                    }
                }

                function onKeyUp(){
                    var e = event.keyCode;

                    //if (e==87 /*w*/){
                        up = false;
                        throw('up'); 
                    //}

                    if (e==65 /*a*/){
                        left = false;
                    }

                    if (e==83 /*s*/){
                        down = false;
                    }

                    if (e==68 /*d*/){
                        right = false;
                    }
                }

Okay, so that was the javascript, and this is the html:

    <body>
            <input id="ss" type="button" value="start/stop" />
            <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">
                <canvas id="canvas" width="480" height="320"  onkeyup="onKeyUp();" onkeydown="onKeydown();">
                    Canvas not displaying.
                </canvas>
            </div>
        </body>



Edit: current code:

window.onload = function(){ init(); };

function init(){
                initSettings();
                initImages();

                document.getElementById('canvas').onkeydown = function(event) {
                    var e = event.keyCode;

                    //if (e==87 /*w*/){
                        up = true;
                        throw('up'); 
                    //}

                    if (e==65 /*a*/){
                        left = true;
                    }

                    if (e==83 /*s*/){
                        down = true;
                    }

                    if (e==68 /*d*/){
                        right = true;
                    }
                }
like image 671
CyanPrime Avatar asked Dec 04 '22 23:12

CyanPrime


1 Answers

You should be doing something like this:

document.onkeydown = function(event) {
    ...
}
document.onkeyup = function(event) {
    ...
}

And get rid of:

onkeyup="onKeyUp();" onkeydown="onKeydown();"

The quick test page I made which works:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">
document.onkeydown = function(event) {
 event = event || window.event;

 var e = event.keyCode;

 //if (e==87 /*w*/){
  up = true;
  throw('up'); 
 //}

 if (e==65 /*a*/){
  left = true;
 }

 if (e==83 /*s*/){
  down = true;
 }

 if (e==68 /*d*/){
  right = true;
 }
}

document.onkeyup=function(event) {
 event = event || window.event;

 var e = event.keyCode;

 //if (e==87 /*w*/){
  up = false;
  throw('up'); 
 //}

 if (e==65 /*a*/){
  left = false;
 }

 if (e==83 /*s*/){
  down = false;
 }

 if (e==68 /*d*/){
  right = false;
 }
}
</script>
</head>

<body>
    <input id="ss" type="button" value="start/stop" />
    <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">
        <canvas id="canvas" width="480" height="320">
            Canvas not displaying.
        </canvas>
    </div>
</body>

</html>
like image 142
thirtydot Avatar answered Dec 29 '22 23:12

thirtydot