Click here to Skip to main content
16,018,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I wrote the following code for Alt+C ShotCut
C#
private void Window_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.Key == Key.C && (Keyboard.Modifiers & (ModifierKeys.Alt)) == (ModifierKeys.Alt))
        MessageBox.Show("DONE");
}

its working good for "right alt"
But it fails for "Left alt" please help me to solve the problem

What I have tried:

C#
private void Window_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.Key == Key.C && (Keyboard.Modifiers & (ModifierKeys.Alt)) == (ModifierKeys.Alt))
        MessageBox.Show("DONE");
}
Posted
Updated 29-Mar-16 2:02am
v2

1 solution

You need to check SystemKey for the Alt key (the right "Alt" key is often actually "Alt Gr" (Alt Graphics) and not recognised as the System key which is why that one works when the left one doesn't).

E.g. Try
C#
var key = (e.Key == Key.System ? e.SystemKey : e.Key);
if (key == Key.C && (Keyboard.Modifiers & (ModifierKeys.Alt)) == (ModifierKeys.Alt))
    MessageBox.Show("DONE");

(Adapted from a solution by Julien Lebosquain at this post[^])
 
Share this answer
 
Comments
Member 10296413 30-Mar-16 1:21am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900