Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trigger to respond to clipboard changes
#1
QuickMacro has a trigger "Clipboard copy" - I would like to write code in LA that responds to clipboard changes. How can that be done?
I checked "Other Triggers", found the pointers to WinEventHook.WinEventHook - but I can't find anything related to clipboard changes in the list of Event Constants - so maybe it uses a different mechanism. I'm afraid I don't know these mechanisms well enough - so would appreciate some help Smile
#2
Click menu TT -> Other triggers.
Insert this text near the end, before the last }.
Click Run.
 
Code:
Copy      Help
    [Triggers]
    void ClipboardTriggers() {
        var w1 = WndUtil.CreateMessageOnlyWindow(static nint (wnd w, int msg, nint wp, nint lp) => {
            if (msg == api.WM_CLIPBOARDUPDATE) {
                print.it("Clipboard trigger");
                
                //if want to get clipboard text, do it with a delay, else it may interfere with other clipboard programs or scripts
                timer.after(100, _ => {
                    var s = clipboard.text;
                    print.it(s);
                });
            }

            return api.DefWindowProc(w, msg, wp, lp);
        },
"#32770");
        api.AddClipboardFormatListener(w1);
    }

    
    unsafe class api : NativeApi {
        [
DllImport("user32.dll", EntryPoint = "DefWindowProcW")]
        internal static extern nint DefWindowProc(wnd hWnd, int msg, nint wParam, nint lParam);
        
        internal const int WM_CLIPBOARDUPDATE = 0x31D;
        
        [
DllImport("user32.dll")]
        internal static extern bool AddClipboardFormatListener(wnd hwnd);
    }
#3
Thank you - that's exactly what I needed! Cool


Forum Jump:


Users browsing this thread: 1 Guest(s)