Udostępnij za pośrednictwem


Display Context Menus Where The Cursor Is, Not Where the Mouse Is

This is a little user interface rant of mine since I'm speech and keyboard-oriented.  While editing text, when I say "Press Shift F Ten" or press the Application Key (to the right of the spacebar on Windows keyboards), I expect the context menu to appear at the text cursor location, since that's where the action is going to take place.

However, some applications assume the mouse activitated the functionality and positions the context menu wherever the mouse is.  Since I'm using speech or typing and haven't touched the mouse in a while, the menu appears nowhere near where the cursor or selection is.

A more common variant of this is when the menu appears in the upper-left corner of the edit box when activated by keyboard. 

The article titled Using Menus on MSDN contains sample code that always uses lParam for the X/Y location to display the menu.  The documentation on WM_CONTEXTMENU is clear:

If the context menu is generated from the keyboard—for example, if the user types SHIFT+F10—then the x- and y-coordinates are -1 and the application should display the context menu at the location of the current selection rather than at (xPos, yPos).

That advice is ignored in the Using Menus topic, so I used the "Add Community Content" to add a note, and I'll file a bug on this so that it can be fixed in the future.

Using MSDN's Community Content feature, I added the following to the Using Menus article:

Remember when processing the WM_CONTEXTMENU message, that the X/Y coordinates might be -1/-1 which indicates that the keyboard generated the menu, thus, the menu should be shown at the cursor location or at the location of the selection - NOT at -1/-1 or the mouse pointer location.

The samples currently in this article do not account for this and will attempt to display the menu at -1/-1, which is confusing to the keyboard user.  Pressing the Application Key on Windows keyboards (to the right of the spacebar) generates a VK_APPS virtual scan code which by default generates a WM_CONTEXTMENU.  You can also get this if the user presses SHIFT+F10.

Never handle SHIFT+F10 or VK_APPS to popup a context menu, rely on the WM_CONTEXTMENU message and if the location given is -1/-1, revert to using the text cursor and/or selection information to place the menu.