1

Is it possible to suppress the windows global shortcuts while recording keypresses ?

I have a Windows Form application written in c#, and using this library to record keypresses to use later in macros. Now when I record the key combinations that are used by Windows (i.e. L Control + Win + Right Arrow to change virtual desktop on Win 10), I'd like my app to record it but avoid windows actually using it while I record which is quite annoying.

I have a checkbox to enable key capturing, on click event

m_KeyboardHookManager.KeyDown += HookManager_KeyDown;

the HookManager_KeyDown is simply like this

private void HookManager_KeyDown(object sender, KeyEventArgs e)
{
    Log(string.Format("KeyDown \t\t {0}\n", e.KeyCode));
    string [] sArr = new string [2];
    if (keyBindingArea1.Text != "")
    {
        sArr[0] = keyBindingArea1.Text;
        sArr[1] = string.Format("{0}", e.KeyCode);

        keyBindingArea1.Text = string.Join("+", sArr);
    }
    else
    {
        keyBindingArea1.Text = string.Format("{0}", e.KeyCode);
    }
}

which display the key combination in a comboText control. (This code is taken directly from the demo attached to the package.

Now the recording work well if for instance I press L Control + Win, then I release the keys and press the third one (i.e. Right Arrow), this will not trigger Windows shortcuts but it is quite annoying to have it work like that.

Appreciate any help. Thanks

Tom
  • 84
  • 1
  • 1
  • 5

1 Answers1

-1

Try to use e.Handled property of the event. If you set it to true it will terminate key procesing chain. As a rule oher applications in the processing chain will not get it. I am not sure it is going to work for such a low level windows staff as virtual desktop switch.

    private void OnKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        bool isThirdKey = //Some state evaluation to detect the third key was pressed
        if (isThirdKey) e.Handled = true;
    }
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • I've tried this already but doesn't work. It detect that I'm pressing the Left Win key for instance, but it does not suppress it. Code I've used:`if (e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin) { e.Handled = true; }` – Tom Sep 09 '15 at 15:21
  • Try some simple key, let' say 'a'. Start your application and type 'a'-s in notepad. You will see that 'a' is correctly suppressed. Then open let's say Microsoft Word and repeat the same. You will observe that suppression does not work for Word. The reason is that Word itself uses hooks and you have no chance to suppress it. As I said some applications can not be "cheated". See comments by @Hans Passant on this post: http://stackoverflow.com/questions/31054486/c-sharp-mousekeyhook-key-suppression-problems – George Mamaladze Sep 10 '15 at 09:45