Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe you are looking for global keyboard hooks. Here is a <a href="http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook" rel="nofollow" title="C# simple global keyboard hooks">simple C# example of usage</a></p> <p>If you use the <code>globalKeyboardHook</code> class from the above example, you need to do 4 things:</p> <p><strong>First</strong> add the self-titled .cs file to your project (and since there is a bug in that code, at least in the version from 5/30/07 modify it in the following way - according to the comment from Member 4120854):</p> <p>Beneath the line</p> <pre><code>public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam); </code></pre> <p>Add this line</p> <pre><code>private keyboardHookProc _keyboardHookProc; </code></pre> <p>And change the hook method to look like this:</p> <pre><code>public void hook() { IntPtr hInstance = LoadLibrary("User32"); _keyboardHookProc = new keyboardHookProc(hookProc); hhook = SetWindowsHookEx(WH_KEYBOARD_LL, _keyboardHookProc, hInstance, 0); } </code></pre> <p><strong>Second</strong>, in your Form class add a private member variable like this</p> <pre><code>globalKeyboardHook gkh = new globalKeyboardHook(); </code></pre> <p><strong>Third</strong>, on your Form_Load (or other place you want to start hooking the keys) add the key(s) you want to the HookedKeys collection Property of the gkh class and subscribe to the KeyDown and/or KeyUp events of the gkh class, like this:</p> <pre><code>private void Form1_Load(object sender, EventArgs e) { gkh.HookedKeys.Add(Keys.LControlKey); gkh.HookedKeys.Add(Keys.RControlKey); gkh.HookedKeys.Add(Keys.M); gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); gkh.KeyUp += new KeyEventHandler(gkh_KeyUp); } </code></pre> <p><strong>Fourth</strong>, then you can do what you want in your KeyUp or KeyDown handler, setting e.Handled to true to stop propagating the key event over the system, like so:</p> <p>void gkh_KeyUp(object sender, KeyEventArgs e) { // e.KeyCode.ToString() is the KeyCode of the released key e.Handled = true; }</p> <pre><code> void gkh_KeyDown(object sender, KeyEventArgs e) { // e.KeyCode.ToString() is the KeyCode of the pressed key e.Handled = true; } </code></pre> <p>To do something on a specific key combination, you need to record the state of keys in a variable and upon KeyDown check if both (or all) keys are down. So for "CTRL-M" you would add a member variable</p> <pre><code>bool controlDown = false; </code></pre> <p>then you would add this code to your KeyDown event handler</p> <pre><code>if ((e.KeyCode == System.Windows.Forms.Keys.LControlKey) || (e.KeyCode == System.Windows.Forms.Keys.RControlKey)) { controlDown = true; } if (e.KeyCode == System.Windows.Forms.Keys.M &amp;&amp; controlDown) { // Do CTRL-M action } </code></pre> <p>and in the KeyUp event handler you need to add this</p> <pre><code>if ((e.KeyCode == System.Windows.Forms.Keys.LControlKey) || (e.KeyCode == System.Windows.Forms.Keys.RControlKey)) { controlDown = false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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