c# - KeyUp And KeyDown arent registering -


i don't registered when i'm key down , key heres 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 :(

heres rest of code if 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 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 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 graphics object         g = graphics.fromimage(bb);          //bb 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)         {          }     } 

make sure form's keypreview property set true events coded can work.

 private void form1_load     (object sender, eventargs e)     {          this.keydown += form1_keydown;         this.keyup += form1_keyup;         this.keypreview = true; 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -