vb.net 当您在 WinForm 中按 ctrl + 单击按钮时运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17839869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:22:33  来源:igfitidea点击:

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

vb.netwinformsmouseeventkeyboard-events

提问by Landmine

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

当用户按住 ctrl 按钮并单击 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

回答by rheitzman

A generic method not reliant on MouseEventArgs:

不依赖于 MouseEventArgs 的通用方法:

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

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

您还可以检查 Alt、Shift....

回答by ToastyMallows

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

不太精通VB,但你也把它标记为C#,应该很容易切换。

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

回答by kpull1

You can use the regular Clickevent to read ModifierKeys, you don't need the MouseClick event. Also remember that Control, Shiftand Altare 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:

您可以使用常规Click事件来读取ModifierKeys,您不需要 MouseClick 事件。还要记住ControlShiftAlt都被视为Flags。如果您不将它们用作Flags,当用户单击同时按住 Shift 和 Control 的按钮时,您将不会注意到。当用户同时按住两个按钮时会触发这 3 个选项:

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 Shiftkey:

此选项仅在用户仅持有Shift密钥时触发:

if (ModifierKeys == Keys.Shift)