日期:2014-05-17  浏览次数:20977 次

c#键盘事件,为啥我的程序没反应
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 123
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }
  private void Form1_KeyPress(object sender,KeyPressEventArgs e)
  {
  if (e.KeyChar == (char)Keys.Enter)
  label1.Text = "你按下了〈Enter〉键";  
  }

  }
}
各位前辈帮我看看,谢了

------解决方案--------------------
C# code
protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            if (m.Msg == WM_HOTKEY)
            {
                int id = m.WParam.ToInt32();
                if (id == "注册热键的ID")
            {
                //响应程序
            }
            }
            base.WndProc(ref m);
        } 
/// <summary>
        /// 注册热键
        /// </summary>
        /// <param name="fsModifiers"></param>
        /// <param name="vk"></param>
        /// <returns></returns>
        public bool RegisterHotKey(int KeyId, uint fsModifiers, uint vk)
        {
            UnregisterHotKey(KeyId);
            return Base.RegisterHotKey(this.Handle, KeyId, fsModifiers, vk);
        }
        /// <summary>
        /// 注销热键
        /// </summary>
        /// <returns></returns>
        public bool UnregisterHotKey(int KeyId)
        {
            return Base.UnregisterHotKey(this.Handle, KeyId);
        }

//注册热键 RegisterHotKey(text.keyId, (uint)text.wParam, (uint)text.lParam)

//调用DLL
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);