Solved

Ctrl & shift key state in VB

Posted on 2010-08-24
6
854 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

734 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