Can I detect a modified click on a LINQPad.Control?

Some time ago, I created a ClickableImage class based on LINQPad.Controls.Image. I assign its click handler with

base.Click += (_, e) => Process.Start(new ProcessStartInfo(imageFilePath) { UseShellExecute = true });

I use it in a subclassed ClickableThumbnail, and it works perfectly, launching the full-sized image in my default file viewer.

Now I would like to enable different actions. I know right-click is out as the images render in a browser, so the browser uses that for its own purposes, but I'm wondering if I might be able to use a modified click like Ctrl-Click.

Is it possible to detect that a modifier key was pressed when the click happened in the click handler I assign?

I tried the following but e is PropertyEventArgs and converts to KeyEventArgs as null so that doesn't work.

base.Click += (_, e) =>
{
    var ke = e as KeyEventArgs;
    if (ke != null && ke.Control)
    {
        Console.WriteLine("Control-Click detected");
    }
    else
    {
        Process.Start(new ProcessStartInfo(imageFilePath) { UseShellExecute = true });
    }
};

Best Answer

  • Because you're already on the C# side, the simplest solution might be to use the WPF Keyboard class:

    base.Click += (s, e) => System.Windows.Input.Keyboard.Modifiers.Dump();
    

    Alternatively, you could pass this info via JavaScript. Look at the sample Custom HTML event handling - capturing event data. You'll end up with something like this:

    var lbl = new Label ("click me");
    
    lbl.HtmlElement.AddEventListener (
        "mousedown",
        new[]
        {
            "Shift: args.shiftKey",
            "Control: args.ctrlKey"
        },
        (sender, args) => args.Properties.Dump());
    
    lbl.Dump();
    

Answers