关于C#中的KeyPresss事件中如何获取键盘按下的键是哪个呢?
如题,小弟目前只试出来了e.KeyChar.ToString()=="*"这种方法可以获取键盘按下的键是哪个。可是这种方法无法获得想Shift键,Enter键,方向键等。...
如题,小弟目前只试出来了e.KeyChar.ToString()=="*"这种方法可以获取键盘按下的键是哪个。可是这种方法无法获得想Shift键,Enter键,方向键等。该怎么样获取那些键呢?
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
int x = button1.Location.X;
int y = button1.Location.Y;
if (e.KeyChar.ToString()=="w")
{
button1.Location = new System.Drawing.Point(x, y - 10);
}
else if (e.KeyChar.ToString()=="s")
{
button1.Location = new System.Drawing.Point(x, y + 10);
}
else if (e.KeyChar.ToString()=="a")
{
button1.Location = new System.Drawing.Point(x - 10, y);
}
else if (e.KeyChar.ToString() == "d")
{
button1.Location = new System.Drawing.Point(x + 10, y);
}
string s = e.KeyChar.ToString();
MessageBox.Show("按下的键为:"+s+"","提示");
}
这是事件的代码,我是想用方向键代替WADS键不知道怎么办 展开
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
int x = button1.Location.X;
int y = button1.Location.Y;
if (e.KeyChar.ToString()=="w")
{
button1.Location = new System.Drawing.Point(x, y - 10);
}
else if (e.KeyChar.ToString()=="s")
{
button1.Location = new System.Drawing.Point(x, y + 10);
}
else if (e.KeyChar.ToString()=="a")
{
button1.Location = new System.Drawing.Point(x - 10, y);
}
else if (e.KeyChar.ToString() == "d")
{
button1.Location = new System.Drawing.Point(x + 10, y);
}
string s = e.KeyChar.ToString();
MessageBox.Show("按下的键为:"+s+"","提示");
}
这是事件的代码,我是想用方向键代替WADS键不知道怎么办 展开
4个回答
展开全部
//如果使用方向键 使用枚举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+"","提示");
}
别忘了采纳哦 ~~~~~~~
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+"","提示");
}
别忘了采纳哦 ~~~~~~~
追问
应该是我表达能力不佳吧,我其实想问的是事件为什么监听不了方向键,我曾经试过接受键盘上所有的按钮发现无论我怎么写都无法监听到方向键和回车键。就是说我写了针对他们的监听,到时候按下去照样没反应。你的方法我也用了,监听方向键的一样没反应。到底该怎么办啊???
追答
这个啊,我也不知道~~~~~~
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然无法获得,因为这个消息定义就是无法获得,要用Keydown消息就可以获得,同时说明水不是一日之寒,靠积累,积累阿,多看书,尤其外国的优秀的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
KeyEventArgs 指定是否有任一个组合键(Ctrl、Shift 或 Alt)在另一个键按下的同时也曾按下。(此修饰符信息也可以通过 Control 类的 ModifierKeys 属性获得。)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码解释
ConsoleKeyInfo info = Console.ReadKey(true );
info=DownArrow;获取向下键LeftArrow,向左RightArrow,向右UpArrow.向上
ConsoleKeyInfo info = Console.ReadKey(true );
info=DownArrow;获取向下键LeftArrow,向左RightArrow,向右UpArrow.向上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询