Landmine
Landmine

Reputation: 1789

Run when when you ctrl + click a button in a WinForm?

I'm trying to run a different code when a user hold down the ctrl button and clicks on the NotifyIcon.

My code doesn't work, but I feel it clearly explains when I'm trying to do. This is under a Mouse Click Event.

        Private Sub NotifyIcon_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles NotifyIcon.MouseClick
        If (e.Modifiers = Keys.Control) Then
            MsgBox("CTRL was pressed !")
        Else
            MsgBox("CTRL was not pressed !")
        End If
        End Sub

Upvotes: 2

Views: 9385

Answers (3)

kpull1
kpull1

Reputation: 1663

You can use the regular Click event to read ModifierKeys, you don't need the MouseClick event. Also remember that Control, Shift and Alt are treated as Flags. If you don't use them as Flags, when user clicks over button holding simultaneously Shift and Control, you won't notice. These 3 options triggers when user holds both buttons:

if (ModifierKeys.HasFlag(Keys.Shift))
if (ModifierKeys.HasFlag(Keys.Control))
if (ModifierKeys.HasFlag(Keys.Shift) && ModifierKeys.HasFlag(Keys.Control))

This option only triggers when user holds only Shift key:

if (ModifierKeys == Keys.Shift)

Upvotes: 0

rheitzman
rheitzman

Reputation: 2297

A generic method not reliant on MouseEventArgs:

            If My.Computer.Keyboard.CtrlKeyDown Then
                ...
            Else
                ...
            End If

You can also check for Alt, Shift....

Upvotes: 5

ToastyMallows
ToastyMallows

Reputation: 4273

Not well versed in VB, but you tagged this as C# as well, should be trivial for you to switch over.

private void Form1_MouseClick(object sender, MouseEventArgs e) {
    if (Control.ModifierKeys == Keys.Control) {
        Console.WriteLine("Ctrl+Click");
    }
}

Upvotes: 6

Related Questions