C# - 无法处理输入和制表键事件

问题描述:

我是新的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://*.com/questions/10641721/c-sharp-unable-to-capture-enter-key?rq=1 –

+0

http://*.com/questions/3752451/输入键按下事件处理程序 –

为了能处理输入/ 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; 
    } 

MSDN documentation是对这个很清楚:

某些键,如TABRETURN,ESC键,方向键被控制自动处理。

要使这些键提高KeyDown事件,必须在表单上的每个控件中覆盖IsInputKey方法。

您需要将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://*.com/questions/2386695/disadvantage-of-setting-form-keypreview-true) 。 – CodeCaster

// 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