2012-05-17 64 views
3

我有一個簡單的形式,由我帶輸入:無法捕捉回車鍵

12個按鍵,1個文本框(禁用&只讀)

enter image description here

這是我做的處理輸入

Login_KeyDown()是常用的方法我要求所有的KeyDown每UI組件&形式本身..

private void Login_KeyDown(object sender, KeyEventArgs e) 
{    
    if (e.KeyCode == Keys.Escape) 
    { 
    Application.Exit(); 
    } 
    else if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9) 
    { 
    button3.BackgroundImage = Properties.Resources.button_hover; 
    button3.ForeColor = Color.White; 
    pin.Text = pin.Text + "9"; 
    } 
    else if (e.KeyCode == Keys.Back) 
    { 
    button11.BackgroundImage = Properties.Resources.button_hover; 
    button11.ForeColor = Color.White; 
    if (pin.Text.Length > 0) 
     pin.Text = pin.Text.Substring(0, pin.Text.Length - 1); 
    } 
    else if (e.KeyCode == Keys.Enter) 
    { 
    MessageBox.Show(pin.Text); 
    } 
} 

此代碼工作正常,當我啓動應用程序,但我點擊任何組件後,其餘代碼工作正常,但「輸入關鍵條件」不起作用。

我的猜測是「輸入關鍵條件」不適用於UI組件或類似的東西。

我一直在使用「按鍵事件」它採用KeyPressEventArgs然後檢查KeyChar == 13但也沒有工作也試過。

什麼問題,我該如何解決?

p.s. 我沒有設置任何按鈕點擊事件的任何按鈕,該應用程序是基於100%KBoard。

回答

0

您是否嘗試過使用

Keys.Return 

相反

編輯: 就想到了這一點。你有爲主表單設置的接受按鈕嗎?

+0

或把一個斷點功能,看看會發生什麼當你按回車鍵。 – dvallejo

+0

http://stackoverflow.com/questions/1557758/net-keyeventargs-return-vs-enter這說他們是一樣的。不會傷害嘗試它,你永遠不知道。 –

+0

沒有幫助.... – Moon

2

檢出PreviewKeyDown。返回引發按鈕控件上的事件。

private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
    { 
     if (e.KeyCode == Keys.Return) 
      MessageBox.Show("I found return"); 

    } 

或者你也可以強制使用,以提高在keydown事件的特殊鍵:

private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
    { 
     if (e.KeyCode == Keys.Return) 
      e.IsInputKey = true; 
    } 

的更多信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx

+0

另一個相關的SO討論:http://stackoverflow.com/問題/ 267198 /總是把手最previewkeydown事件-IN-A-基本形式 – deepee1