.NET KeyDown Events Like Shift+Click or Alt+Click

By anil soni

Explains the approach for implementing the combination shortcut in keydown and keyup events.

Often it is required to create a combinational shortcut like shift+Left Mouse click  etc which we cannot implement using the available keyboard events like KeyDown, KeyPress or KeyUp event. So to implement the same we can use the Microsoft.VisualBasic.Devices.KeyBoard class available in Microsoft.VisualBasic assembly available in GAC.

E.g. Lets assume that we need to create a combinational shortcut Shift + Click for a button. So we have handled the btnTest_Click event and in it we have used KeyBoard class ShiftKeyDown property.

private void btnTest_Click(object sender, EventArgs e)
{
           var keyBoard = new Microsoft.VisualBasic.Devices.Keyboard();

            if (keyBoard.ShiftKeyDown)
            {
               Console.WriteLine("Shift+Clicked ocuured");
            }
            else if (keyBoard.CtrlKeyDown && keyBoard.AltKeyDown)
            {
               Console.WriteLine("Ctrl + Alt + Button Clicke occured");
            }
            else
            {
               Console.WriteLine("Click event occured");
            }

            keyBoard = null;
 }

.NET KeyDown Events Like Shift+Click or Alt+Click  (1902 Views)