C#获取键盘的键
我是在控制台做的我想获取键盘上的键,比如按某个键就执行什么功能或者退出,我应该怎么获取键盘上的键...
我是在控制台做的
我想获取键盘上的键,比如按某个键就执行什么功能或者退出,我应该怎么获取键盘上的键 展开
我想获取键盘上的键,比如按某个键就执行什么功能或者退出,我应该怎么获取键盘上的键 展开
4个回答
展开全部
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+"","提示");
}
{
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+"","提示");
}
展开全部
class Program
{
static void Main(string[] args)
{
while(true)
{
ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine("\nyou typed {0}", cki.KeyChar);
}
}
}
{
static void Main(string[] args)
{
while(true)
{
ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine("\nyou typed {0}", cki.KeyChar);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也想晓得`!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先读取用户输入的按键,再判断
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询