【问题标题】:How can i check if ctrl,alt are pressed on left mouse click in c#?如何检查 c# 中的鼠标左键是否按下 ctrl、alt?
【发布时间】:2013-01-09 20:28:01
【问题描述】:

如果在单击鼠标左键时按下了 CtrlAlt,我想检查我的表单。有什么方法可以检查吗?

【问题讨论】:

  • 这取决于您正在制作的应用程序的类型。它是控制台应用程序吗? Windows 窗体应用程序? WPF 应用程序? WinRT 应用?

标签: c#


【解决方案1】:
void window_MouseLeftButtonDown_1(object sender, MouseEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control && Control.ModifierKeys == Keys.Alt)
    {
        //...
    }
}

【讨论】:

    【解决方案2】:

    WPF: 在 xaml 中将事件添加到您的窗口:

    MouseLeftButtonDown="window_MouseLeftButtonDown_1"
    

    或在后面的代码中:

    public MainWindow()
    {
       InitializeComponent();
    
       this.MouseLeftButtonDown += window_MouseLeftButtonDown_1;
    }
    

    然后你可以检查回调中的按键

    private void window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
    {
       if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.LeftAlt))
       {
          // ...
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2012-11-16
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多