C#如何接收键盘的上下左右按键?
展开全部
一般来说,我们一个控件都会有三个事件,分别是
KeyDown,KeyPress和KeyUp
其中,KeyDown对应(键盘或者鼠标)按下
而KeyUp则对应松开,或者说弹起来。
而KeyPress则在你按下并松开按键的时候,会先后触发KeyDown事件和KeyUp事件
例如KeyDown我需要响应键盘的“上”
private void txtRemark_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
//codes what you want
}
}
KeyDown,KeyPress和KeyUp
其中,KeyDown对应(键盘或者鼠标)按下
而KeyUp则对应松开,或者说弹起来。
而KeyPress则在你按下并松开按键的时候,会先后触发KeyDown事件和KeyUp事件
例如KeyDown我需要响应键盘的“上”
private void txtRemark_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
//codes what you want
}
}
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class enterevent : Form
{
public enterevent()
{
InitializeComponent();
}
private void t1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t4.Focus();
}
else if (e.KeyValue == 40)
{
t4.Focus();
}
else if (e.KeyValue == 39)
{
t2.Focus();
}
else if (e.KeyValue == 38)
{
t7.Focus();
}
else if (e.KeyValue==37)
{
t9.Focus();
}
}
private void t2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t4.Focus();
}
else if (e.KeyValue == 40)
{
t5.Focus();
}
else if (e.KeyValue == 39)
{
t3.Focus();
}
else if (e.KeyValue == 38)
{
t8.Focus();
}
else if (e.KeyValue == 37)
{
t1.Focus();
}
}
private void t3_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t4.Focus();
}
else if (e.KeyValue == 40)
{
t6.Focus();
}
else if (e.KeyValue == 39)
{
t4.Focus();
}
else if (e.KeyValue == 38)
{
t9.Focus();
}
else if (e.KeyValue == 37)
{
t2.Focus();
}
}
private void t4_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t7.Focus();
}
else if (e.KeyValue == 40)
{
t7.Focus();
}
else if (e.KeyValue == 39)
{
t5.Focus();
}
else if (e.KeyValue == 38)
{
t1.Focus();
}
else if (e.KeyValue == 37)
{
t3.Focus();
}
}
private void t5_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t7.Focus();
}
else if (e.KeyValue == 40)
{
t8.Focus();
}
else if (e.KeyValue == 39)
{
t6.Focus();
}
else if (e.KeyValue == 38)
{
t2.Focus();
}
else if (e.KeyValue == 37)
{
t4.Focus();
}
}
private void t6_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t7.Focus();
}
else if (e.KeyValue == 40)
{
t9.Focus();
}
else if (e.KeyValue == 39)
{
t7.Focus();
}
else if (e.KeyValue == 38)
{
t3.Focus();
}
else if (e.KeyValue == 37)
{
t5.Focus();
}
}
private void t7_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t1.Focus();
}
else if (e.KeyValue == 40)
{
t1.Focus();
}
else if (e.KeyValue == 39)
{
t8.Focus();
}
else if (e.KeyValue == 38)
{
t4.Focus();
}
else if (e.KeyValue == 37)
{
t6.Focus();
}
}
private void t8_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t1.Focus();
}
else if (e.KeyValue == 40)
{
t2.Focus();
}
else if (e.KeyValue == 39)
{
t9.Focus();
}
else if (e.KeyValue == 38)
{
t5.Focus();
}
else if (e.KeyValue == 37)
{
t7.Focus();
}
}
private void t9_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t1.Focus();
}
else if (e.KeyValue == 40)
{
t3.Focus();
}
else if (e.KeyValue == 39)
{
t1.Focus();
}
else if (e.KeyValue == 38)
{
t6.Focus();
}
else if (e.KeyValue == 37)
{
t8.Focus();
}
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
str.AppendFormat("第一个输入值是:"+t1.Text);
str.AppendFormat("\n第二个输入值是:" + t2.Text);
str.AppendFormat("\n第三个输入值是:" + t3.Text);
str.AppendFormat("\n第四个输入值是:" + t4.Text);
str.AppendFormat("\n第五个输入值是:" + t5.Text);
str.AppendFormat("\n第六个输入值是:" + t6.Text);
str.AppendFormat("\n第七个输入值是:" + t7.Text);
str.AppendFormat("\n第八个输入值是:" + t8.Text);
str.AppendFormat("\n第九个输入值是:" + t9.Text);
MessageBox.Show(str.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void enterevent_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class enterevent : Form
{
public enterevent()
{
InitializeComponent();
}
private void t1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t4.Focus();
}
else if (e.KeyValue == 40)
{
t4.Focus();
}
else if (e.KeyValue == 39)
{
t2.Focus();
}
else if (e.KeyValue == 38)
{
t7.Focus();
}
else if (e.KeyValue==37)
{
t9.Focus();
}
}
private void t2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t4.Focus();
}
else if (e.KeyValue == 40)
{
t5.Focus();
}
else if (e.KeyValue == 39)
{
t3.Focus();
}
else if (e.KeyValue == 38)
{
t8.Focus();
}
else if (e.KeyValue == 37)
{
t1.Focus();
}
}
private void t3_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t4.Focus();
}
else if (e.KeyValue == 40)
{
t6.Focus();
}
else if (e.KeyValue == 39)
{
t4.Focus();
}
else if (e.KeyValue == 38)
{
t9.Focus();
}
else if (e.KeyValue == 37)
{
t2.Focus();
}
}
private void t4_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t7.Focus();
}
else if (e.KeyValue == 40)
{
t7.Focus();
}
else if (e.KeyValue == 39)
{
t5.Focus();
}
else if (e.KeyValue == 38)
{
t1.Focus();
}
else if (e.KeyValue == 37)
{
t3.Focus();
}
}
private void t5_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t7.Focus();
}
else if (e.KeyValue == 40)
{
t8.Focus();
}
else if (e.KeyValue == 39)
{
t6.Focus();
}
else if (e.KeyValue == 38)
{
t2.Focus();
}
else if (e.KeyValue == 37)
{
t4.Focus();
}
}
private void t6_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t7.Focus();
}
else if (e.KeyValue == 40)
{
t9.Focus();
}
else if (e.KeyValue == 39)
{
t7.Focus();
}
else if (e.KeyValue == 38)
{
t3.Focus();
}
else if (e.KeyValue == 37)
{
t5.Focus();
}
}
private void t7_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t1.Focus();
}
else if (e.KeyValue == 40)
{
t1.Focus();
}
else if (e.KeyValue == 39)
{
t8.Focus();
}
else if (e.KeyValue == 38)
{
t4.Focus();
}
else if (e.KeyValue == 37)
{
t6.Focus();
}
}
private void t8_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t1.Focus();
}
else if (e.KeyValue == 40)
{
t2.Focus();
}
else if (e.KeyValue == 39)
{
t9.Focus();
}
else if (e.KeyValue == 38)
{
t5.Focus();
}
else if (e.KeyValue == 37)
{
t7.Focus();
}
}
private void t9_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
t1.Focus();
}
else if (e.KeyValue == 40)
{
t3.Focus();
}
else if (e.KeyValue == 39)
{
t1.Focus();
}
else if (e.KeyValue == 38)
{
t6.Focus();
}
else if (e.KeyValue == 37)
{
t8.Focus();
}
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
str.AppendFormat("第一个输入值是:"+t1.Text);
str.AppendFormat("\n第二个输入值是:" + t2.Text);
str.AppendFormat("\n第三个输入值是:" + t3.Text);
str.AppendFormat("\n第四个输入值是:" + t4.Text);
str.AppendFormat("\n第五个输入值是:" + t5.Text);
str.AppendFormat("\n第六个输入值是:" + t6.Text);
str.AppendFormat("\n第七个输入值是:" + t7.Text);
str.AppendFormat("\n第八个输入值是:" + t8.Text);
str.AppendFormat("\n第九个输入值是:" + t9.Text);
MessageBox.Show(str.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void enterevent_Load(object sender, EventArgs e)
{
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果不需要太复杂的可以不用API
KeyEventArgs
这个结构里有,看你要从哪里获取了,如果你用事件的话,事件函数一定会带(KeyEventArgs e)类似这样的参数
用e.KeyCode可以确认哪个按下了
KeyEventArgs
这个结构里有,看你要从哪里获取了,如果你用事件的话,事件函数一定会带(KeyEventArgs e)类似这样的参数
用e.KeyCode可以确认哪个按下了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
keypreview
属性设为true
这个是键盘在窗体注册的属性
楼主你也可以用ascii码去实现
键盘上的每一个键都对应一个ascii码
属性设为true
这个是键盘在窗体注册的属性
楼主你也可以用ascii码去实现
键盘上的每一个键都对应一个ascii码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看二楼的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询