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

2

12

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?

Size answered 18/3, 2014 at 10:19 Comment(0)
M
25

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

Menses answered 18/3, 2014 at 10:52 Comment(1)
Thanks a lot! Adding the above code in the Form_Load event of the start-up form enables me to achieve this.Size
S
8

Alternative solution using Control.ModifierKeys:

    If Control.ModifierKeys = Keys.Shift Or Control.ModifierKeys = Keys.Control Then
        MsgBox("SHIFT or CTRL key pressed.")
    End If
Shoestring answered 18/3, 2014 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.