simonwait
asked on
Ctrl & shift key state in VB
I am trying to do an if statement which checks to see if the ctrl & shift keys are pressed when the user clicked on a button and run some code if they were. I keep finding things on other sites but it doesnt seem to work
Quick search gives:
The Shift key code is passed to the MouseDown event, so you can check it the shift key is pressed when the mouse is clicked.
Case 1
Debug.Print "Shift"
Case 2
Debug.Print "Ctrl"
Case 4
Debug.Print "Alt"
Case 3
Debug.Print "Shift + Ctrl"
Case 5
Debug.Print "Shift + Alt"
Case 6
Debug.Print "Ctrl + Alt"
Case 7
Debug.Print "Shift + Ctrl + Alt"
The Shift key code is passed to the MouseDown event, so you can check it the shift key is pressed when the mouse is clicked.
Case 1
Debug.Print "Shift"
Case 2
Debug.Print "Ctrl"
Case 4
Debug.Print "Alt"
Case 3
Debug.Print "Shift + Ctrl"
Case 5
Debug.Print "Shift + Alt"
Case 6
Debug.Print "Ctrl + Alt"
Case 7
Debug.Print "Shift + Ctrl + Alt"
Sorry, that code is for VB.Net. I'm don't know how to do it in previous versions.
Wayne
Wayne
In VB6 the SHIFT As Integer parameter (described in the previous post) is passed in the Key down, key up, mouse down, and mouse up events, but not in the key press or mouse click events, so to take advantage of it, you'll have to handle the event slightly differently.
Either detect the ctrl+shift+alt modifier in the mouse down routine, store it in a static variable, and then read it back during the click event, or perform click events in the mouse down or mouse up handlers.
--
Alain
Either detect the ctrl+shift+alt modifier in the mouse down routine, store it in a static variable, and then read it back during the click event, or perform click events in the mouse down or mouse up handlers.
--
Alain
Hi, you should hook the keyboard trough windows api like here http://www.codeguru.com/columns/vb/article.php/c4829 and read the keycode combination, I know, that I already have done this, but I can't find the example for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If My.Computer.Keyboard.CtrlK
MsgBox("Success!")
End If
End Sub
Wayne