Link to home
Start Free TrialLog in
Avatar of simonwait
simonwaitFlag for United Kingdom of Great Britain and Northern Ireland

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
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Try something like this....

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.ShiftKeyDown Then
            MsgBox("Success!")
        End If
    End Sub

Wayne
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"
Sorry, that code is for VB.Net. I'm don't know how to do it in previous versions.

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
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
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial