I want to check in my form if CtrlAlt are pressed on left mouse click. Is there any way of checking it?
2
-
Possible duplicate of stackoverflow.com/questions/1275371/…– ColselawJan 6, 2013 at 2:52
-
It would depend on the type of application you are making. Is it a console app? A Windows Forms app? A WPF app? WinRT app?– Jeff MercadoJan 6, 2013 at 2:53
Add a comment
|
3 Answers
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:
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:
if(ModifierKeys.Equals(Keys.Control|Keys.Alt)){
MessageBox.Show("Ctrl&Alt keys were pressed sametime");
}