C#.net无法获取我的键盘事件?

初学.net想写一个简单的键盘事件,写完之后没有报错但是运行时我的键盘录入并没有被读取到

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tank
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblX.Text = "X坐标:"+  picTank.Top;
            lblY.Text = "Y坐标:" + picTank.Left;
            lblMsg.Text = "请按上下左右键控制坦克!";
            picTank.Image = picTankU.Image;
            picTankU.Visible = false;
            picTankD.Visible = false;
            picTankL.Visible = false;
            picTankR.Visible = false;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    picTank.Image = picTankU.Image;
                    if (picTank.Top + picTank.Height <= 0)
                    {
                        picTank.Top = this.Height;
                    }
                    else
                    {
                        picTank.Top -= 10;
                    }
                    break;
                case Keys.Down:
                    picTank.Image = picTankD.Image;
                    if (picTank.Top >= this.Height)
                    {
                        picTank.Top = 0-picTank.Height;
                    }
                    else
                    {
                        picTank.Top += 10;
                    }
                    break;
                case Keys.Left:
                    picTank.Image = picTankL.Image;
                    if (picTank.Width + picTank.Left <= 0)
                    {
                        picTank.Left = this.Width;
                    }
                    else
                    {
                        picTank.Left -= 10;
                    }
                    break;
                case Keys.Right:
                    picTank.Image = picTankR.Image;
                    if (picTank.Left >= this.Width)
                    {
                        picTank.Left = 0 - picTank.Height;
                    }
                    else
                    {
                        picTank.Left += 10;
                    }
                    break;
            }
            lblX.Text = "X坐标:" + picTank.Top;
            lblY.Text = "Y坐标:" + picTank.Left;
            lblMsg.Text = "现在按下" + e.KeyCode.ToString() + "键,键值为" + e.KeyCode + "!!";
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            lblMsg.Text = "请按上下左右键控制坦克!";
        }
    }
}


1个回答

窗体的keypreview属性设置为true

leticia_m
leticia_m 回复caozhy: 没有
11 个月之前 回复
caozhy
贵阳老马马善福专门编写代码的老马就是我! lblMsg.Text = "现在按下" + e.KeyCode.ToString() + "键,键值为" + e.KeyCode + "!!"; 这个没有输出么
11 个月之前 回复
leticia_m
leticia_m 设置过了
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!