Solved

Ctrl & shift key state in VB

Posted on 2010-08-24
6
843 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now