6

Below is the code for Ctrl + F (from another SO post). But how do you detect a Ctrl + ForwardSlash? or Ctrl + / (note: divide does not work)

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (1 == 1) //keyData == (Keys.Control | Keys.F))
        {
            MessageBox.Show("What the Ctrl+F?");
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
Jasper
  • 2,166
  • 4
  • 30
  • 50
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

1 Answers1

9

Divide should work fine.

For Ctrl + \:

if (keyData == (Keys.Control | Keys.OemPipe) )

For Ctrl + /:

if (keyData == (Keys.Control | Keys.OemQuestion) )

For some reason (not sure why), when you trap Ctrl + these keys, they're mapping to the "shifted" keymappings.


Edit:

One trick for finding this, or any other key. Set a breakpoint on any line in that method, and look at the keyData value when you press the key you're trying to trap. I recommend doing this without hitting control. You can then use reflector to get all of the specific values for Keys, and find the "key" with the appropriate value.

Jasper
  • 2,166
  • 4
  • 30
  • 50
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • didn't work for me. note: I'm talking about the / above enter or near it. Not on the numeric keypad (if you are on laptop). Any simple way to capture actual key codes in a messagebox and then just rewrite using numeric codes? – BuddyJoe Sep 04 '09 at 19:13
  • @Tyndall: I just tested this. My new answer will give you the correct response. – Reed Copsey Sep 04 '09 at 19:43
  • On my keyboard, the backslash is above enter. – recursive Sep 04 '09 at 19:48
  • @recursive. It is on mine too. – Reed Copsey Sep 04 '09 at 19:50
  • sorry I meant / below and to the left of enter on the ? key. Wonder, why the / maps to shifted with the Ctrl pressed. Seems like a bug. I play around with this more. May just have the users use F12, if I can't make this reliable across my corp's desktops and laptops (7 or 8 models) – BuddyJoe Sep 06 '09 at 17:56
  • This is quite an old post, but for posterity, I found a detailed explanation of different key capture approaches on this [SO post](https://stackoverflow.com/questions/8459067/winforms-capturing-the-combination-of-key-presses-using-processcmdkey-vs-keydo) – Memetican Jul 27 '17 at 21:31