Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extended toolbar Item events using modifier keys
#1
I want to distinguish inside an TBItem event if left or right mouse was pressed and furthermore if modifier keys were used for that trigger as following but it seems not to work.

- RightClick open up a context menu for the item (probably internal menu with edit itemand find file option).
- MiddleClick is ignored (maybe problem of Windows 10 since design change)
- Win and Alt key modifier close menu and cant be used
- Key modifier access seems to work well

    void ToolbarExample(TriggerArgs ta = null) {
        var t = new toolbar();
        t["LeftorRightClick"] = o => {
          if (mouse.isPressed(MButtons.Left)) print.it("MouseLeft");
          if (mouse.isPressed(MButtons.Right)) print.it("MouseRight");
          if (keys.isPressed(KKey.Shift)) print.it("Shift");
          if (keys.isPressed(KKey.Ctrl)) print.it("Ctrl");
          if (keys.isPressed(KKey.Alt)) print.it("Alt");
          if (keys.isPressed(KKey.Win)) print.it("Win");
        };
        t.Show(ta);
    }

1. What shall I do to use other mouseclicks than left too?
2. Can I distinuish double and single click the same way by using the Click event of TBItem to implement the logic there, as I have it done already for trayIcon?
#2
It is possible with a workaround or low-level code.

Code:
Copy      Help
// class "Toolbar_Subclass.cs"
using Au.Triggers;

partial class Program {
    [
Toolbars]
    void Toolbar_Subclass_Triggers() {
        //print.clear();
        Toolbar_Subclass();
    }

    
    void Toolbar_Subclass(TriggerArgs ta = null) {
        KMod mod = 0;
        
        var t = new toolbar();
        t["LeftorRightClick"] = o => {
            print.it(mod);
        };

        t["other"] = null;
        
        t.Show(ta);
        
        WndUtil.Subclass(t.Hwnd, _WndProc);
        
        nint _WndProc(wnd w, int msg, nint wp, nint lp) {
            //WndUtil.PrintMsg(w, msg, wp, lp);
            
            switch (msg) {
            case napi.WM_LBUTTONDOWN:
                print.it("WM_LBUTTONDOWN");
                mod = keys.gui.getMod(); //save for the LeftorRightClick button code. It will run when mouse button released.
                if (!(mod is 0 or KMod.Shift)) { //0 click, Shift move. For others the button action is not called. This is a workaround.
                    if (mod.Has(KMod.Alt)) keys.more.sendKey(KKey.Alt, false);
                    if (mod.Has(KMod.Ctrl)) keys.more.sendKey(KKey.Ctrl, false);
                    if (mod.Has(KMod.Shift)) keys.more.sendKey(KKey.Shift, false);
                    if (mod.Has(KMod.Win)) keys.more.sendKey(KKey.Win, false);
                }

                break;
            case napi.WM_RBUTTONDOWN:
                print.it("WM_RBUTTONDOWN");
                break;
            case napi.WM_MBUTTONDOWN:
                print.it("WM_MBUTTONDOWN");
                //Your LeftorRightClick button code will not run on right or middle click.
                //There is no public function to get the clicked button. But it is possible.

                int i = t.GetClickedButton(lp);
                print.it("Button index: ", i);
                break;
            }

            
            //To steal a message from the toolbar, return without calling DefSubclassProc. Can return 0, or `api.DefWindowProc(w, msg, wp, lp)`, or whatever.
            //If window messages are new to you, need some learning.
            //See also _WndProc in: https://github.com/qgindi/LibreAutomate/blob/master/Au/GUI/toolbar/toolbar.cs

            
            return WndUtil.DefSubclassProc(w, msg, wp, lp);
        }
        
    }
}


static class Ext8745 {
    /// <summary>
    ///
When called on a mouse button message, returns 0-based index of the clicked button.
    /// Returns -1 if not a button.
    /// Uses a private function vis reflection, therefore may stop working in the future.
    /// </summary>
    public static int GetClickedButton(this toolbar t, nint lp) {
        var p = Math2.NintToPOINT(lp);
        _HitTestD ??= typeof(toolbar).GetMethod("_HitTest", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).CreateDelegate<_HitTestT>(t);
        return _HitTestD(p);
    }

    
    delegate int _HitTestT(POINT p);
    static _HitTestT _HitTestD;
    
}


#pragma warning disable 649, 169 //field never assigned/used
unsafe class napi : NativeApi {
    internal const ushort WM_LBUTTONDOWN = 0x201;
    internal const ushort WM_RBUTTONDOWN = 0x204;
    internal const ushort WM_MBUTTONDOWN = 0x207;
}

#pragma warning restore 649, 169 //field never assigned/used
#3
Thank you very much Gintaras for pointing me the way to go.
#4
https://prnt.sc/c-XhuUoJbNQ3
https://prnt.sc/VrjxcZOy1N9j


please . im hopping some one share they complit script because more i random pick scrip and more eror i get ,
I have already watched several videos about the basics of C#, but their content is different from the needs of an LA script.


Forum Jump:


Users browsing this thread: 2 Guest(s)