Solved

Ctrl & shift key state in VB

Posted on 2010-08-24
6
853 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:simonwait
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33509890
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
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 33509903
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"
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33509914
Sorry, that code is for VB.Net. I'm don't know how to do it in previous versions.

Wayne
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:alainbryden
ID: 33509943
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
0
 
LVL 4

Expert Comment

by:Fugas
ID: 33510110
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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33510456
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question