如何获取整个屏幕的KeyPress事件
我建了一个Winform程序,现在想在整个屏幕的任意位置按下键盘的的a键,弹出消息框(不只是在这个Form上),可是只能定义Form的KeyPress事件,鼠标出了Form
就没用了...不知道该怎么办,麻烦各位了帮帮忙...
public Form1() { InitializeComponent(); RegisterHotKey(Handle, 100, 2, Keys.Up); // 热键一:Control +光标上箭头 RegisterHotKey(Handle, 200, 2, Keys.Down); // 热键一:Control +光标下箭头 } [StructLayout(LayoutKind.Sequential)] public struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public int dwAttributes; public string szDisplayName; public string szTypeName; } public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); private void ProcessHotkey(Message m)//触发热键 { try { IntPtr id = m.WParam; string sid = id.ToString(); switch (sid) { case "100": playup(); break; case "200": playnext(); break; } } catch (Exception W) { this.label_Error.Text = "错误47: " + W.Message; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e)//卸载热键 { try { UnregisterHotKey(Handle, 100); //卸载第1个快捷键 UnregisterHotKey(Handle, 200); //缷载第2个快捷键 }