关于C#中的KeyPresss事件中如何获取键盘按下的键是哪个呢?

2025-02-23 10:23:13
推荐回答(4个)
回答1:

//如果使用方向键 使用枚举Enum;如果使用WASD,在button1按钮的KeyDown事件;
enum Direction
{
Up,Right,Down,Left
}
public partial class Form1 : Form
{
Direction dir;
private void getDirection()
{
int x = button1.Location.X;
int y = button1.Location.Y;
switch (dir)
{
case Direction.Up:
button1.Location = new System.Drawing.Point(x, y - 10);
break;
case Direction.Down:
button1.Location = new System.Drawing.Point(x + 10, y);
break;
case Direction.Right:
button1.Location = new System.Drawing.Point(x, y + 10);
break;
case Direction.Left:
button1.Location = new System.Drawing.Point(x - 10, y);
break;
default:
break;
}
string s = e.KeyChar.ToString();
MessageBox.Show("按下的键为:"+s+"","提示");
}
private void Form1_Load(object sender, EventArgs e)
{
private void getDirection();
}

//用按钮button1的KeyDown事件啊~~~~~
private void btnHelp_KeyDown(object sender, KeyEventArgs e)
{
int x = button1.Location.X;
int y = button1.Location.Y;
switch (e.KeyCode)
{
case Keys.W:
button1.Location = new System.Drawing.Point(x, y - 10);
break;
case Keys.D:
button1.Location = new System.Drawing.Point(x + 10, y);
break;
case Keys.S:
button1.Location = new System.Drawing.Point(x, y + 10);
break;
case Keys.A:
button1.Location = new System.Drawing.Point(x - 10, y);
break;
default:
break;
}
string s = e.KeyChar.ToString();
MessageBox.Show("按下的键为:"+s+"","提示");
}
别忘了采纳哦 ~~~~~~~

回答2:

当然无法获得,因为这个消息定义就是无法获得,要用Keydown消息就可以获得,同时说明水不是一日之寒,靠积累,积累阿,多看书,尤其外国的优秀的!

回答3:

KeyEventArgs 指定是否有任一个组合键(Ctrl、Shift 或 Alt)在另一个键按下的同时也曾按下。(此修饰符信息也可以通过 Control 类的 ModifierKeys 属性获得。)

回答4:

代码解释
ConsoleKeyInfo info = Console.ReadKey(true );
info=DownArrow;获取向下键LeftArrow,向左RightArrow,向右UpArrow.向上