KeyUp 和 KeyDown 未注册



当我按下键和调高键时,我没有注册任何内容 这是我的代码:

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;
}

我什么都没:(

如果有帮助,这是我的其余代码

公共分部类 表格 1 : 表格 {

//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)
{
}
}

确保窗体的KeyPreview属性设置为 true,以便您编码的事件可以正常工作。

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

我真的不知道问题是什么,但这部分代码

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

相关内容

  • 没有找到相关文章

最新更新