devxlogo

Simulate Pressed Control Key for Multiple Selections in List Box

Simulate Pressed Control Key for Multiple Selections in List Box

When selecting items in a normal list box with the MultiSelect property set to 1 – Simple or 2 – Extended,the user needs to press the control key while clicking on the items in order to continuously select multipleitems without also deselecting the items currently selected. This method lets the user select multiple itemscontinuously without pressing the control key. Place this code in a module:

 Declare Function GetKeyboardState Lib _        "user32" (pbKeyState As Byte) _        As LongDeclare Function SetKeyboardState Lib _        "user32" (lppbKeyState As Byte) _        As LongPublic Const VK_CONTROL = &H11Public KeyState(256) As Byte

Place this code in the MouseDown event procedure in a list box (List1) with MultiSelect property set aseither Simple or Extended:

 ' Sets the control key state to' "pressed"GetKeyboardState KeyState(0)KeyState(VK_CONTROL) = _        KeyState(VK_CONTROL) Or &H80SetKeyboardState KeyState(0) 

Place this code in any procedure where the pressed control key is to be released, such as theList1_LostFocus event procedure:

 ' release the control key state from ' "pressed"GetKeyboardState KeyState(0)KeyState(VK_CONTROL) = _        KeyState(VK_CONTROL) And &H7FSetKeyboardState KeyState(0)
See also  Redefining Energy Management: Advanced Solutions for Modern Buildings
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