Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Advanced
Aug 1, 1997



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

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 multiple items without also deselecting the items currently selected. This method lets the user select multiple items continuously without pressing the control key. Place this code in a module:
Declare Function GetKeyboardState Lib _
        "user32" (pbKeyState As Byte) _
        As Long
Declare Function SetKeyboardState Lib _
        "user32" (lppbKeyState As Byte) _
        As Long
Public Const VK_CONTROL = &H11
Public KeyState(256) As Byte
Place this code in the MouseDown event procedure in a list box (List1) with MultiSelect property set as either Simple or Extended:
' Sets the control key state to
' "pressed"
GetKeyboardState KeyState(0)
KeyState(VK_CONTROL) = _
        KeyState(VK_CONTROL) Or &H80
SetKeyboardState KeyState(0

Place this code in any procedure where the pressed control key is to be released, such as the List1_LostFocus event procedure:
' release the control key state from 
' "pressed"
GetKeyboardState KeyState(0)
KeyState(VK_CONTROL) = _
        KeyState(VK_CONTROL) And &H7F
SetKeyboardState KeyState(0)
Shangzhi Ren
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