Detect right-click?

I'm trying to figure out a way to detect a right-click (or other mouse buttons) in Igor. I found the following code in the Igor help for setting up a user-contextual menu, but in implementing it the menu activates whenever you click, be it right-click or left-click. When you right-click, the custom contextual menu shows, followed by the trace popup or other standard contextual menu when the custom one is deactivated.

SetWindow kwTopWin hook=TableHook, hookevents=1 // mouse down events
Function TableHook(infoStr)
    String infoStr
    String event= StringByKey("EVENT",infoStr)
    Print "EVENT= ",event
    strswitch(event)
        case "mousedown":
            Variable xpix= NumberByKey("MOUSEX",infoStr)
            Variable ypix= NumberByKey("MOUSEY",infoStr)
            PopupContextualMenu/C=(xpix, ypix) "yes;no;maybe;"
            strswitch(S_selection)
                case "yes":
                    // do something because "yes" was chosen
                    break;
                case "no":
                    break;
                case "maybe":
                    // do something because "maybe" was chosen
                    break;
            endswitch
    endswitch
    return 0
End


I'd like to modify it to detect a right-click so the system only shows the contextual menu when issuing a left-click, and not when invoking the right-click.

On the same note, would there be a similar way to detect other mouse clicks (ie, middle-click, or 4th and 5th buttons)? The goal ultimately would be to sequester various functions to different contextual menus invoked by different mouse clicks. I know you can use the system modifier keys using the "getkeystate" function to invoke different menus, but would prefer to have this done via mouse buttons if possible.
In the window hook, test the MODIFIERS value with bit 4, which will be nonzero when a right-click is set:

MODIFIERS:flags Bits set based on state of certain keys:
    bit 0:  Mouse button is down.
    bit 1:  Shift key is down.
    bit 2:  Option (Macintosh ) or Alt (Windows ) is down.
    bit 3:  Command (Macintosh ) or Ctrl (Windows ) is down.
    bit 4:  Contextual menu click: right-click or Control-click (Macintosh , or right-click (Windows ).


So, start with something like:

Variable isContextualClick= NumberByKey("MODIFIERS", infoStr) & 0x16


The other mouse buttons aren't checked or reported, sorry!

--Jim Prouty
Software Engineer, WaveMetrics, Inc.
I'm not sure, but if I remember correctly you have to also tell Igor that you took care of the event by returning 1 instead of 0. This way, the standard right-click menu will be suppressed. Anyway, I would recommend the structure-implemented version of the hook function to make some things easier (for example getting mouse location). Here:
SetWindow kwTopWin hook=TableHook, hookevents=1 // mouse down events
Function TableHook(s)
    STRUCT WMWinHookStruct &s

    Variable HookEvent = 0  // tells, if event was used
    switch(s.EventCode)
        case 3:                     // equals "mousedown"
            If (s.EventMod == 16)       // looking for a right click (bit 4) => right click menu
            HookEvent = 1           // event used
            PopupContextualMenu/C=(s.MouseLoc.h, s.MouseLoc.v) "yes;no;maybe;"
            strswitch(S_selection)
                case "yes":
                    // do something because "yes" was chosen
                    break;
                case "no":
                    break;
                case "maybe":
                    // do something because "maybe" was chosen
                    break;
            endswitch
    endswitch
    return HookEvent
End
Oh that's perfect! Works great! Its unfortunate other mouse inputs aren't handled (maybe a feature in the future?) but for now I can definitely get by with this and using modifier keys. Thanks!
tkessler wrote:
...
On the same note, would there be a similar way to detect other mouse clicks (ie, middle-click, or 4th and 5th buttons)? The goal ultimately would be to sequester various functions to different contextual menus invoked by different mouse clicks. I know you can use the system modifier keys using the "getkeystate" function to invoke different menus, but would prefer to have this done via mouse buttons if possible.


I am curious how a mouse has middle, 4th, and 5th buttons?

In any case, those "buttons" must send specific code sequences to distinguish them from each other, since they are otherwise just "clicks" to the system. It would seem then that capturing the specific code sequence corresponding to the specific button is what you want to do here.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
jjweimer wrote:
tkessler wrote:
...
On the same note, would there be a similar way to detect other mouse clicks (ie, middle-click, or 4th and 5th buttons)? The goal ultimately would be to sequester various functions to different contextual menus invoked by different mouse clicks. I know you can use the system modifier keys using the "getkeystate" function to invoke different menus, but would prefer to have this done via mouse buttons if possible.


I am curious how a mouse has middle, 4th, and 5th buttons?

In any case, those "buttons" must send specific code sequences to distinguish them from each other, since they are otherwise just "clicks" to the system. It would seem then that capturing the specific code sequence corresponding to the specific button is what you want to do here.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville


Many mice have more than the standard left and right clicks. In most scroll mice you can depress the scroll wheel as a 3rd click, and then others yet have side buttons and other inputs that can be assigned to many programs. Most generic drivers in operating systems recognize these button presses so applications should be able to make use of them rather easily.

Unfortunately it sounds like Igor currently only accepts the first two mouse inputs from the driver and isnt yet built to configure additional inputs.
tkessler wrote:
Many mice have more than the standard left and right clicks. ...


Well yes. After sending my reply, I realized that my Kensington track ball with four buttons, a scroll ball, and a scroll wheel must fit that model somehow. :-/

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville