我需要抓住KeyDown
& {strong}特别 KeyUp
1,2,3,4,5,6,7,8,9
键盘按钮的事件。
它是怎么做的?
我可以抓住KeyDown
事件,但 KeyUp 呢?
请提供一些简单的代码。
答案 0 :(得分:6)
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
}
void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.NumPad1:
break;
case Keys.NumPad2:
break;
//...
}
}
答案 1 :(得分:0)
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.D1) // number 1
{
MessageBox.Show("Hello");
}
}
答案 2 :(得分:0)
如果您需要逻辑完全相同,那么您可以将同一事件处理程序连接到要捕获输入的控件的KeyUp和KeyDown事件。
// this occurs as part of Initialisation via the designer or you can hook up manually
myControl.KeyDown += myControl_KeyChange;
myControl.KeyUp += myControl_KeyChange;
// ...
private void myControl_KeyChange(object sender, KeyEventArgs e)
{
switch( e.KeyCode )
{
case Keys.1:
{
// handle the 1 key being pressed
break;
}
case Keys.2:
{
// handle the 2 key being pressed
break;
}
// etc
}
}