在我的項目中我需要修改這個預製代碼來改變遊戲的佈局。所以我想在我的表單上添加關鍵事件以便導航到'WASD',而不是使用按鈕來移動蛇。但是這裏的問題是,一旦我將該函數添加到窗體並測試它在控制檯上顯示出來,它不會給出任何事件,而不會發生事件錯誤。我不是這方面的專家,所以我希望有足夠的人帶領我走上正確的道路,謝謝。winform中的關鍵事件問題c#
這是我的項目的代碼和屏幕截圖。
public Form1()
{
InitializeComponent();
backgroundMusic.PlayLooping();
this.AutoSize = true;
boardPanel.AutoSize = true;
//Set up the main board
mainBoard = new Board(this);
//Set up the game timer at the given speed
clock = new Timer();
clock.Interval = speed; //Set the clock to tick every 500ms
clock.Tick += new EventHandler(refresh); //Call the refresh method at every tick to redraw the board and snake.
duration = 0;
score = 0;
level = 1;
modeLBL.Text = mode;
gotoNextLevel(level);
}
private void keyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.W)
{
Console.WriteLine("W is pressed");
}
}
}
您還沒有實際註冊的處理程序' this.KeyDown = new KeyEventHandler(keyDown);' –