Search code examples
c#keydownkeyup

KeyUp And KeyDown arent registering


I don't get anything registered when i'm key down and key up heres my code:

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch(e.KeyCode)
        {
            case Keys.A:
                MessageBox.Show("Hi");
                Walking = true;
                break;
        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        Walking = false;
    }

i don't get anything :(

Heres the rest of my code if that helps

public partial class Form1 : Form {

    //Graphic Vars
    Graphics G;
    Bitmap BB;
    Graphics BBG;
    Rectangle sRect;
    Rectangle dRect;
    Bitmap bmpTile;
    bool AllowGrid = false;
    int allowgrid = 0;

    //Color
    Pen rainbowpen = Pens.Black;
    int rainbowpenstate = 0;

    //wh
    int w;
    int h;

    //Other Vars
    bool isRunning = true;

    //Map Vars
    int[,,] Map = new int[151, 151, 11];
    int MapX = 20;
    int MapY = 20;

    //Mouse Vars
    int mouseX;
    int mouseY;
    int mMapX;
    int mMapY;

    //Character (toons)
    Bitmap bmpChar;
    int xPos =0;
    int yPos =0;
    int MoveSpeed =8;
    short MoveDir =0;
    short LastDir =2;
    bool Walking = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load
    (object sender, EventArgs e)
    {

        this.KeyDown += Form1_KeyDown;
        this.KeyUp += Form1_KeyUp;

        this.Show();
        this.Focus();

        //wh
        w = this.Width;
        h = this.Height;

        //Initialize Graphics
        G = this.CreateGraphics();
        BB = new Bitmap(w, h);

        Textures t = new Textures();

        bmpTile = new Bitmap(Gametextures.Image);

        //Map[21, 21, 0] = 1;

        //Prevent from changing sizes
        this.FormBorderStyle = FormBorderStyle.FixedSingle;

        //Check if played before
        CheckGameFiles();

        //Game Loop
        StartGameLoop();
    }

    public void StartGameLoop()
    {
        while (isRunning)
        {
            Application.DoEvents();

            //User Input (1)
            //AI (2)
            //Update Object Data (3)
            //Check Triggers (4)
            //Draw Graphics (5)
            DrawGraphics();
            //Music (6)

        }
    }

    public void DrawGraphics()
    {
        //Fill Back Buffer
        //Draw Tiles
        for(int X = 0; X < 30; X++)
        {
            for(int Y = 0; Y < 30; Y++)
            {

                //sRect = new Rectangle(X * 32, Y * 32, 32, 32);

                /*Fill Background*/
                //G.FillRectangle(Brushes.Black, sRect);
                //Grid

                GetSourceRect(MapX + X, MapY + Y, 32, 32);
                dRect = new Rectangle(X * 32, Y * 32, 32, 32);
                G.DrawImage(bmpTile, dRect, sRect, GraphicsUnit.Pixel);

                if (AllowGrid)
                {
                    G.DrawRectangle(Pens.Red, sRect);
                }

            }
        }

        //Draw Final Layers 

        //Chars/Menus
         G.DrawRectangle(rainbowpen, mouseX * 32, mouseY * 32, 32, 32);

        //Copy BackBuffer To Graphics Object
        G = Graphics.FromImage(BB);

        //BB to screen BB = BackBuffer
        BBG = this.CreateGraphics();
        BBG.DrawImage(BB, 0, 0, this.Width, this.Height);

        //Fix OverDraw
        G.Clear(Color.Black);

    }

    public void HideStart()
    {
        mainscreen1.Hide();
        button1.Hide();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (!File.Exists("GameFiles/player.pinfo") && !File.Exists("GameFiles/world1.winfo") && !File.Exists("GameFiles/world2.winfo") && !File.Exists("GameFiles/world3.winfo"))
        {
            File.CreateText("GameFiles/player.pinfo");
            File.CreateText("GameFiles/world1.winfo");
            File.CreateText("GameFiles/world2.winfo");
            File.CreateText("GameFiles/world3.winfo");
        }

        HideStart();

    }

    private void Form1_MouseMove
    (object sender, MouseEventArgs e)
    {
        mouseX = (int) Math.Floor(e.X / 32.0d);
        mouseY = (int) Math.Floor(e.Y / 32.0d);

        mMapX = MapX + mouseY;
        mMapY = MapY + mouseY;

    }

    private void timer1_Tick
    (object sender, EventArgs e)
    {
        rainbowpenstate += 1;

        if(rainbowpenstate == 17)
        {
            rainbowpenstate = 1;
        }

        switch(rainbowpenstate)
        {
            case 1:
                rainbowpen = Pens.Red;
                break;
            case 2:
                rainbowpen = Pens.OrangeRed;
                break;
            case 3:
                rainbowpen = Pens.Yellow;
                break;
            case 4:
                rainbowpen = Pens.YellowGreen;
                break;
            case 5:
                rainbowpen = Pens.Green;
                break;
            case 6:
                rainbowpen = Pens.DarkGreen;
                break;
            case 7:
                rainbowpen = Pens.Blue;
                break;
            case 8:
                rainbowpen = Pens.DarkBlue;
                break;
            case 9:
                rainbowpen = Pens.Purple;
                break;
            case 10:
                rainbowpen = Pens.MediumPurple;
                break;
            case 11:
                rainbowpen = Pens.Blue;
                break;
            case 12:
                rainbowpen = Pens.DarkGreen;
                break;
            case 13:
                rainbowpen = Pens.Green;
                break;
            case 14:
                rainbowpen = Pens.YellowGreen;
                break;
            case 15:
                rainbowpen = Pens.Yellow;
                break;
            case 16:
                rainbowpen = Pens.OrangeRed;
                break;

        }

    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
    }

    public void GetSourceRect(int X, int Y, int W, int H)
    {
        try
        {

            switch (Map[X, Y, 0])
            {
                //Grass
                case 0:

                    sRect = new Rectangle(0, 0, 32, 32);

                    break;

                //
                case 1:

                    sRect = new Rectangle(0, 32, 32, 32);

                    break;

                //
                case 2:
                    break;
            }

        }
        catch (Exception error) { Console.WriteLine(error); }
    }

    public void CheckGameFiles()
    {
        if(File.Exists("GameFiles/player.pinfo") && File.Exists("GameFiles/world1.winfo") && File.Exists("GameFiles/world2.winfo") && File.Exists("GameFiles/world3.winfo"))
        {
            button1.Text = "Continue!";
        }
        else
        {
            button1.Text = "Start";
        }
    }

    public void ChangeWorlds(string world)
    {

    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch(e.KeyCode)
        {
            case Keys.A:
                MessageBox.Show("Hi");
                Walking = true;
                break;
        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        Walking = false;
    }

    private void walkinganimation_Tick(object sender, EventArgs e)
    {
        if(Walking)
        {

        }
    }

Solution

  • Make sure your form's KeyPreview property is set to true so that the events you coded can work.

     private void Form1_Load
        (object sender, EventArgs e)
        {
    
            this.KeyDown += Form1_KeyDown;
            this.KeyUp += Form1_KeyUp;
            this.KeyPreview = true;