devxlogo

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

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) <> 0Dim isAltPressed As Boolean = (Control.ModifierKeys And Keys.Alt) <> 0Dim isControlPressed As Boolean = (Control.ModifierKeys And Keys.Control) <> 0MessageBox.Show("Shift pressed: " & isShiftPressed & Environment.NewLine & _    "Alt pressed:" & isAltPressed & Environment.NewLine & "Control pressed:" & _    isControlPressed)

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist