我正在開發2D遊戲(pacman),通過c#改進自己在VB 2013中,我想通過點擊特定按鈕來激活我的按鍵事件(這是遊戲結束時顯示的重啓按鈕)。謝謝你的幫幫我。如何通過點擊特定的按鈕激活keydown事件?
//these are my keydown codes
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int r = pictureBox5.Location.X;
int t = pictureBox5.Location.Y;
if (pictureBox5.Top >= 33)
{
if (e.KeyCode == Keys.Up)
{
t = t - 15;
}
}
if (pictureBox5.Bottom <= 490)
{
if (e.KeyCode == Keys.Down)
{
t = t + 15;
}
}
if (pictureBox5.Right <= 520)
{
if (e.KeyCode == Keys.Right)
{
r = r + 15;
}
}
if (pictureBox5.Left >= 30)
{
if (e.KeyCode == Keys.Left)
{
r = r - 15;
}
}
if (e.KeyCode == Keys.Up && e.KeyCode == Keys.Right)
{
t = t - 15;
r = r + 15;
}
pictureBox5.Location = new Point(r, t);
}
//and that's the button I wanted to interlace with keydown event
private void button1_Click(object sender, EventArgs e)
{
}
當你按下按鈕時,你應該按什麼鍵? Form_Keydown中的代碼完全取決於e.KeyCode的值,因此如果沒有密鑰,則無法使用該代碼,因此無法使用該代碼。 – Steve
我希望激活所有4種主要方向方式(右下方左側) – KAMATLI
這是怎麼回事甚至可能嗎?代碼不能全部在一起。在你的form_keydown代碼中有一個不可能的if語句。 KeyCode不能等於Keys.Right和Keys.Up。 – Steve