Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if the SHIFT or CTRL key was pressed when launching the application

I need to be able to determine if the SHIFT or CTRL keys were pressed when the application is launched

How can I do this for a Windows Forms Application?

like image 717
slayernoah Avatar asked Mar 18 '14 10:03

slayernoah


People also ask

How do I know if shiftKey is pressed?

The mouseEvent shiftKey property is used to define whether the shift key is pressed or not. It is a boolean value. When the shift key is pressed then on click of the mouse left button, it returns true and if the shift key is not pressed then it returns false.

How do I know which key I pressed?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.


2 Answers

Not sure if this is what you are looking for. The following will return True or False depending on whether the key is pressed

My.Computer.Keyboard.CtrlKeyDown
My.Computer.Keyboard.ShiftKeyDown 

Example

    If My.Computer.Keyboard.CtrlKeyDown Or My.Computer.Keyboard.ShiftKeyDown Then
        MsgBox("SHIFT or CTRL key down")
    End If

If you are asking about event handling, KeyEventArgs Class is needed. Here you can view some examples how to detect shift/ctrl keypress

like image 97
ɐsɹǝʌ ǝɔıʌ Avatar answered Oct 18 '22 14:10

ɐsɹǝʌ ǝɔıʌ


Alternative solution using Control.ModifierKeys:

    If Control.ModifierKeys = Keys.Shift Or Control.ModifierKeys = Keys.Control Then
        MsgBox("SHIFT or CTRL key pressed.")
    End If
like image 25
tezzo Avatar answered Oct 18 '22 14:10

tezzo