A customer didn’t understand why their Win32 accelerator key for Ctrl+M was triggering spuriously. Specifically, it was triggering when the user hit the Enter key, which is nothing like the Ctrl+M two-key combination.

They defined their accelerator table like this:

IDA_MAIN ACCELERATORS BEGIN “^M”, IDM_MUMBLE END

Accelerator key definitions can be done by character or by virtual key code. If you use a quoted string, then you are defining a character accelerator which triggers when that character is entered by whatever means. For example, if you define a character accelerator for “0”, it will trigger if the user presses the 0 key on the top row of the keyboard, or if they press the Numpad0 key on the numeric keypad, or even if they type Alt+Numpad4,Numpad8 to type the character by entering its character code on the numeric keypad.

In the above case, the accelerator was defined as the character ^M, which is shorthand for Ctrl+M, or character code 13. There are multiple ways to enter that character code. You could type Ctrl+M on the keyboard, or you could press the Enter key.

If you want the accelerator to trigger only for the case of Ctrl+M, then you want to define a virtual key accelerator, not a character accelerator.

IDA_MAIN ACCELERATORS BEGIN “M”, IDM_MUMBLE, CONTROL, VIRTKEY END

Virtual key accelerators trigger only when the specified keys are pressed. In our case, we want the M virtual key in combination with the CTRL key.

Conversely, if you want the accelerator to trigger only for the Enter key, you would specify that key as a virtual key.

IDA_MAIN ACCELERATORS BEGIN VK_RETURN, IDM_MUMBLE, VIRTKEY END

There are two more key-to-control-character combinations that you may stumble across.

Backspace becomes the character ^H.Tab becomes the character ^I.

Technically, there is a third combination:

Esc becomes the character ^[.

However, the Resource Compiler does not accept “^[” as a control key, so you aren’t going to run into that one by mistake.

The post Why does my <KBD>Ctrl</KBD>+<KBD>M</KBD> accelerator key activate when I press the <KBD>Enter</KBD> key? appeared first on The Old New Thing.


From The Old New Thing via this RSS feed