C#如何在KeyDown事件中响应方向键? 50
privatevoidForm1_KeyDown(objectsender,KeyEventArgse){switch(e.KeyCode){caseKeys.Left:...
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
aShape.MoveShape(1, this.aShape, aBody);
break;
case Keys.Right :
aShape.MoveShape(2, this.aShape, aBody);
break;
case Keys.Up :
aShape.ChangeShape(aShape, aBody);
break;
case Keys.Down :
timer1.Interval = 100;
break ;
}
}
没反应,我换'W','A','S','D'就很正常........
求大家帮我想想
写在KeyDown事件中....
MessageBox.Show(e.KeyCode.ToString());
按字母什么的都有MESSAGE
按方向键就没反映了...键盘是好的 展开
{
switch (e.KeyCode)
{
case Keys.Left:
aShape.MoveShape(1, this.aShape, aBody);
break;
case Keys.Right :
aShape.MoveShape(2, this.aShape, aBody);
break;
case Keys.Up :
aShape.ChangeShape(aShape, aBody);
break;
case Keys.Down :
timer1.Interval = 100;
break ;
}
}
没反应,我换'W','A','S','D'就很正常........
求大家帮我想想
写在KeyDown事件中....
MessageBox.Show(e.KeyCode.ToString());
按字母什么的都有MESSAGE
按方向键就没反映了...键盘是好的 展开
3个回答
展开全部
那你就把那个贪食蛇程序编译一下,然后运行试试呢?
那个程序肯定是可以运行的.
那么你可以用:
MessageBox.Show(e.KeyCode.ToString());
看看啊,是什么字符串,或者把我那个贪食蛇的程序对比一下不同点.
当然可以响应,参见:
http://post.baidu.com/f?ct=335675392&tn=baiduPostBrowser&sc=2178575176&z=191967117&pn=0&rn=50&lm=0&word=c%23
如下,小键盘也包括了:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
string key = e.KeyCode.ToString();
switch(key)
{
case "Up":
case "NumPad8":
break;
case "Right":
case "NumPad6":
break;
case "Down":
case "NumPad5":
case "Clear":
break;
case "Left":
case "NumPad4":
break;
}
}
那个程序肯定是可以运行的.
那么你可以用:
MessageBox.Show(e.KeyCode.ToString());
看看啊,是什么字符串,或者把我那个贪食蛇的程序对比一下不同点.
当然可以响应,参见:
http://post.baidu.com/f?ct=335675392&tn=baiduPostBrowser&sc=2178575176&z=191967117&pn=0&rn=50&lm=0&word=c%23
如下,小键盘也包括了:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
string key = e.KeyCode.ToString();
switch(key)
{
case "Up":
case "NumPad8":
break;
case "Right":
case "NumPad6":
break;
case "Down":
case "NumPad5":
case "Clear":
break;
case "Left":
case "NumPad4":
break;
}
}
展开全部
//方向键事件的处理,只能重写ProcessCmdKey,让其返回false
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return false;//如果要调用KeyDown,这里一定要返回false才行,否则只响应重写方法里的按
}
//再添加KeyDown,它就会进入方向键
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)//ctrl
switch (e.KeyValue)
{
case 83: //保存
File_Save();
break;
//case 17: //CTRL
// //鼠标放开时zuode
// break;
}
else
switch (e.KeyValue)
{
case 37://left
DealMoveLeft();
break;
case 39://right
DealMoveRight();
break;
case 38://up
DealMoveUp();
break;
case 40://dowm
DealMoveDown();
break;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return false;//如果要调用KeyDown,这里一定要返回false才行,否则只响应重写方法里的按
}
//再添加KeyDown,它就会进入方向键
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)//ctrl
switch (e.KeyValue)
{
case 83: //保存
File_Save();
break;
//case 17: //CTRL
// //鼠标放开时zuode
// break;
}
else
switch (e.KeyValue)
{
case 37://left
DealMoveLeft();
break;
case 39://right
DealMoveRight();
break;
case 38://up
DealMoveUp();
break;
case 40://dowm
DealMoveDown();
break;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
间值取到就可以了啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询