简体   繁体   English

如何检查 c# 中的鼠标左键是否按下 ctrl、alt?

[英]How can i check if ctrl,alt are pressed on left mouse click in c#?

I want to check in my form if Ctrl Alt are pressed on left mouse click.如果在鼠标左键单击时按下Ctrl Alt ,我想检查我的表单。 Is there any way of checking it?有什么方法可以检查吗?

void window_MouseLeftButtonDown_1(object sender, MouseEventArgs e)
{
    if (Control.ModifierKeys == Keys.Control && Control.ModifierKeys == Keys.Alt)
    {
        //...
    }
}

WPF: Add event to your window in xaml: WPF:在 xaml 中将事件添加到您的窗口:

MouseLeftButtonDown="window_MouseLeftButtonDown_1"

or in code behind:或在后面的代码中:

public MainWindow()
{
   InitializeComponent();

   this.MouseLeftButtonDown += window_MouseLeftButtonDown_1;
}

and then you can check for the key presses in the call back然后您可以检查回调中的按键

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

To check if more than one modifier keys are presses use |要检查是否按下了多个修饰键,请使用| operator.操作员。

ModifierKeys.Equals(Keys.Control|Keys.Alt)

Or或者

Control.ModifierKeys==(Keys.Control|Keys.Alt)

This example show a message if both of ctrl and alt keys are pressed when mouse clicked:如果在单击鼠标时同时按下 ctrl 和 alt 键,此示例将显示一条消息:

if(ModifierKeys.Equals(Keys.Control|Keys.Alt)){
      MessageBox.Show("Ctrl&Alt keys were pressed sametime");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM