Does anyone know if you can detect the shift or control keys down when you start a VB app?

Posted on 2008-11-05
Medium Priority
Last Modified: 2010-04-21
I am looking for a way in the first lines of code in a vb app to detect if the shift or control key is pressed.  Does anyone know how to do this if a form is not present yet?

Question by:onemorecoke
  • 2
LVL 29

Accepted Solution

nffvrxqgrcfqvvc earned 2000 total points
ID: 22887401
GetAsyncKeyState Function
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long
Public Sub IsShiftOrControl()
    If GetAsyncKeyState(vbKeyShift) Then
        Debug.Print "shift key"
    ElseIf GetAsyncKeyState(vbKeyControl) Then
        Debug.Print "control key"
    End If
End Sub

Open in new window

LVL 29

Expert Comment

ID: 22887429
Forgot to mention to add the procedure {IsShiftOrControl} to a timer control with say 200-500 milliseconds

Author Closing Comment

ID: 31513565
Works perfect.  Thank you!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 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