C# - 无法处理enter和tab键事件

时间:2014-01-29 08:01:04

标签: c# winforms

我是新的c#,我使用下面的代码,但代码不适用于Enter键和Tab键。请解决这个问题......

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";
}

4 个答案:

答案 0 :(得分:1)

MSDN documentation非常明确:

  

某些键,例如 TAB RETURN ,ESC和箭头键会由控件自动处理。

     

要让这些键引发KeyDown事件,您必须覆盖表单上每个控件中的IsInputKey方法。

答案 1 :(得分:1)

为了能够处理Enter / Tab键,你应该覆盖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;
    }

答案 2 :(得分:0)

您需要将KeyPreview的{​​{1}}属性设置为Form

试试这个:

True

答案 3 :(得分:-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);
    }