Help with if statements and events

ساخت وبلاگ
I'm trying to make a simple ball move with the arrow keys in javascript on my webpage. I want to add an if statement testing for a key being pressed then inside that another if statement for the key code. I've want to use keydown, keyup, keypressed inside the if but those don't work. Is there anyway to get those to work within an if statement. Are there different things I can do that may work?

This is what I tried:
Code:

<!DOCTYPE html>
<html>
        <head>
                <meta charset = "UTF-8">
        </head>
        <body>
                <canvas id="myCanvas" width="200" height="100" style="border: 5px double"></canvas>
                <script>
                        var c = document.getElementById("myCanvas");
                        var ctx = c.getContext("2d");
                        var X = 100;
                        var Y = 50;
                        var draw = function(){
                                ctx.beginPath();
                                ctx.arc(X, Y, 10, 0, 2*Math.PI);
                                ctx.stroke();
                                if(keydown){
                                        X -= 3;
                                }
                        }
                        setInterval(draw, 10);
                </script>
        </body>
</html>

I'm quite new to this so I'm probably just being stupid. Any help would be appreciated! Thanks in advance!
CodingForums...
ما را در سایت CodingForums دنبال می کنید

برچسب : نویسنده : codingforums بازدید : 177 تاريخ : پنجشنبه 30 آذر 1396 ساعت: 23:15