Saturn
Saturn

Reputation: 18149

Check if a certain key is being pressed?

I need to check if the SHIFT or CTRL keys are being pressed in my VB.net application, any ideas? (get a boolean)

Upvotes: 9

Views: 46602

Answers (4)

alldayremix
alldayremix

Reputation: 753

If Control.ModifierKeys = Keys.Shift Or Control.ModifierKeys = Keys.Control Then
    ' Shift, Ctrl, or Shift+Ctrl is being pressed
Else
    ' Neither Shift nor Ctrl is being pressed
End If

Upvotes: 5

OfficeAddinDev
OfficeAddinDev

Reputation: 1125

The following will return True or False depending on whether the key is pressed at that moment. From the wording of your question, I assume you are not asking about event handling, which is what the other answers have addressed.

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

Upvotes: 28

Nathua
Nathua

Reputation: 8826

I assume you want to see on whole application. To do this, forms or controls have "keypress" or "keydown" events. You can check them with those events. Click events tab and you'll see them

Upvotes: 2

Alan
Alan

Reputation: 652

Check that link : http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx#

It's about KeyEventArgs Class with some examples how to detect shift/ctrl keypress etc.

Upvotes: 0

Related Questions