Solved

Ctrl & shift key state in VB

Posted on 2010-08-24
6
858 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
Independent Software Vendors: 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 86

Accepted Solution

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

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month6 days, 18 hours left to enroll

623 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