[英]Keydown Event not firing
嘗試在鍵盤上按下按鈕時觸發事件。 我將Form1屬性設置為也將KeyPreview設置為True。 但仍然,它沒有開火,我看不出怎么了。
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.A)
MessageBox.Show("A pressed");
else if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.F1)
MessageBox.Show("Combination of ALt and F1 pressed");
}
嘗試使用KeyCode:
if (e.KeyCode==Keys.A)
MessageBox.Show("A pressed");
...
還請記住,當您按下相應的按鈕時, Form1
必須具有焦點
將窗體的KeyPreview屬性設置為true。
否則,子控件將首先捕獲事件。
設置事件
this.KeyDown += Form5_KeyDown;
this.KeyPreview = true;
事件
void Form5_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
MessageBox.Show("A pressed");
else if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.F1)
MessageBox.Show("Combination of ALt and F1 pressed");
}
如果要處理所有keydown
,請不要忘記KeyPreview = true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.