Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Every once in a while a request comes up for hooking keys from a console program. The <a href="http://www.codeproject.com/Articles/2357/Console-Event-Handling" rel="nofollow">standard events like <code>CTRL_C_EVENT</code> and <code>CTRL_CLOSE_EVENT</code></a> do not include the Pause-event. I've tried doing so using a background thread, but I don't seem to manage. However, there's a not-so-hard workaround: use an extra process.</p> <p>Download this easy-to-use <a href="http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook" rel="nofollow">global keyboard hook for C#</a>. Then, when you open that project, take the following code and put it in the Form1.cs:</p> <pre><code>public partial class Form1 : Form { globalKeyboardHook globalKeyboardHook = new globalKeyboardHook(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { globalKeyboardHook.HookedKeys.Add(Keys.Pause); globalKeyboardHook.KeyDown += new KeyEventHandler(globalKeyboardHook_KeyDown); globalKeyboardHook.KeyUp += new KeyEventHandler(globalKeyboardHook_KeyUp); } void globalKeyboardHook_KeyUp(object sender, KeyEventArgs e) { // remove this when you want to run invisible lstLog.Items.Add("Up\t" + e.KeyCode.ToString()); // this prevents the key from bubbling up in other programs e.Handled = true; } void globalKeyboardHook_KeyDown(object sender, KeyEventArgs e) { // remove this when you want to run without visible window lstLog.Items.Add("Down\t" + e.KeyCode.ToString()); // this prevents the key from bubbling up in other programs e.Handled = true; } } </code></pre> <p>Then, the rest becomes trivial:</p> <ol> <li>Change your program to start the above program and than run normally</li> <li>Try to type the pause key</li> <li>It'll be caught by the other program</li> <li>Your program will NOT be paused.</li> </ol> <p>I tried the above myself and it works.</p> <p>PS: I don't mean that there isn't a possible way straight from a Console program. There may very well be, I just didn't find it, and the above global keyhook library didn't work from within a Console application.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload