如何在C#系统中设置HotKey

在AutoIt v3中有一个叫做HotKeySet的函数。 它设置了一个热键,当按下时调用用户function。 这是一个全系统热键,这意味着当热键被设置时,密钥不能用于其他任何事情。

基本上,我想赶上Esc或任何其他键,如$`等,当用户按下它的任何地方,即使在应用程序之外,它应该让我知道。

例如,我会像循环内部的HotKeySet({ESC})那样做,当它完成时,程序将等待该键被按下。

 public static void work() { while (true) { string task = null; lock (locker) if (tasks.Count > 0) { task = tasks.Dequeue(); if (task == null) { return; } } if (task != null) { //MessageBox.Show("Performing task: " + task); Program.mainAnnounceWindow.setLogTextBox(task); Program.mainAnnounceWindow.setLogTrayTip(task); Program.windowStateChange("Show"); // Set the hotkey here SetHotkey(`); //Wait for hotkey to press when it's pressed... Execute some stuff Thread.Sleep(5000); // Simulate work... Program.windowStateChange("Hide"); } else { wh.WaitOne(); // No more tasks - wait for a signal } } } 

你需要看看user32.dll中的RegisterHotKey 。 在pinvoke.net, RegisterHotKey(user32)有一个很好的例子。

我想你需要设置一个系统钩子…

这里是一个例子: http : //www.axino.net/tutorial/2009/02/keylogger-in-c-introduction