Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: beginner
Dec 23, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Retrieving the state of the Shift, Alt and Ctrl keys, at any time

You can know the state of the Shift, Alt and Control keys at any time, not just from inside a Keyxxx event. The shared property Control.ModifierKeys returns a bit coded value that identifies which of those keys are pressed, and you can use the And bit operator if a specific key is pressed. The following code shows an example:

Dim isShiftPressed As Boolean = (Control.ModifierKeys And Keys.Shift) <> 0
Dim isAltPressed As Boolean = (Control.ModifierKeys And Keys.Alt) <> 0
Dim isControlPressed As Boolean = (Control.ModifierKeys And Keys.Control) <> 0

MessageBox.Show("Shift pressed: " & isShiftPressed & Environment.NewLine & _
    "Alt pressed:" & isAltPressed & Environment.NewLine & "Control pressed:" & _
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date