2014-01-29 47 views
-1

我是新的C#,我使用下面的代碼,但代碼不起作用的Enter鍵和Tab鍵。請解決這個問題...C# - 無法處理輸入和製表鍵事件

private void Panel_Load(object sender, EventArgs e) 
{ 
    this.KeyDown += new KeyEventHandler(C_event); 
} 

private void C_event(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
     Label1.Text = "Enter Key"; 
     return; 
    } 
    if (e.keyCode == Keys.Tab) 
    { 
     Label1.text = "Tab Key"; 
     return; 
    } 

    label1.text = "Default"; 
} 
+0

僅作爲提示,嘗試其他類型的事件,也許類似的keyPressed做的工作。 – Rafa

+0

請看看SO鏈接http://stackoverflow.com/questions/10641721/c-sharp-unable-to-capture-enter-key?rq=1 –

+0

http://stackoverflow.com/questions/3752451/輸入鍵按下事件處理程序 –

回答

1

爲了能處理輸入/ Tab鍵preses你應該重寫ProcessCmdKey方法

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
     if (!this.ProcessKey(msg, keyData)) 
     { 
      return base.ProcessCmdKey(ref msg, keyData); 
     } 
     return false; 
    } 

    protected virtual bool ProcessKey(Message msg,Keys keyData) 
    { 
     if ((keyData & Keys.Enter) == Keys.Enter) 
     { 
      Label1.Text = "Enter Key"; 
      return true; 
     } 
     if ((keyData & Keys.Tab) == Keys.Tab) 
     { 
      Label1.Text = "Tab Key"; 
      return true; 
     } 
     return false; 
    } 
1

MSDN documentation是對這個很清楚:

某些鍵,如TABRETURN,ESC鍵,方向鍵被控制自動處理。

要使這些鍵提高KeyDown事件,必須在表單上的每個控件中覆蓋IsInputKey方法。

0

您需要將FormKeyPreview屬性設置爲True

試試這個:

private void Panel_Load(object sender, EventArgs e) 
{ 
    this.KeyPreview= true; //add this line 
    this.KeyDown += new KeyEventHandler(C_event); 
} 
+0

雖然這是真的,[你不應該使用'KeyPreview'雖然](http://stackoverflow.com/questions/2386695/disadvantage-of-setting-form-keypreview-true) 。 – CodeCaster

-2
// Structure contain information about low-level keyboard input event 
    [StructLayout(LayoutKind.Sequential)] 
    private struct KBDLLHOOKSTRUCT 
    { 
     public Keys key; 
     public int scanCode; 
     public int flags; 
     public int time; 
     public IntPtr extra; 
    } 

    //System level functions to be used for hook and unhook keyboard input 
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool UnhookWindowsHookEx(IntPtr hook); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp); 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr GetModuleHandle(string name); 
    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    private static extern short GetAsyncKeyState(Keys key); 

    //Declaring Global objects 
    private IntPtr ptrHook; 
    private LowLevelKeyboardProc objKeyboardProcess; 

    private IntPtr CaptureKey(int nCode, IntPtr wp, IntPtr lp) 
    { 
     if (nCode >= 0) 
     { 
      KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT)); 
      if (objKeyInfo.key == Keys.Tab || objKeyInfo.key == Keys.Enter) // Disabling Windows keys 
      { 
       MessageBox.Show("TAB or Enter PRESSED"); 
       return (IntPtr)1; 
      } 

     } 
     return CallNextHookEx(ptrHook, nCode, wp, lp); 
    } 
    public Form1() 
    { 
     InitializeComponent(); 


     //Get Current Module 
     ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule; 
     //Assign callback function each time keyboard process 
     objKeyboardProcess = new LowLevelKeyboardProc(CaptureKey); 
     //Setting Hook of Keyboard Process for current module 
     ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0); 
    } 
+1

只是傾銷一些代碼不是答案,而鍵盤鉤子是解決這個問題的一個特別糟糕的方法,所以-1。 – CodeCaster