VB6 key press at program start

TopCatOnyx
TopCatOnyx used Ask the Experts™
Using VB6, I'm trying to detect if a couple of keys are pressed at the time the icon to start the program is clicked.  I need to detect if a particular key combination is pressed before any forms are displayed, so I can't use something like form.keypress (no form loaded yet).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer
This is somewhat a strange question....i guess that the application starts...performs some "work" and then it shows...
May something like this :http://www.vbforums.com/showthread.php?27616-Recoginition-of-keypress-thru-API(Window-API) could help but i am not sure
Hi John,
Thanks for the quick reply.  I have seen programs which if started (clicking the icon) with a particular key held down (when the icon is clicked) sets a debug mode or changes the logging mode of the program.  In my case, what I want to do is to change the behavior of a routine which connects to a SQL server.  If the server is off (and proper key not pressed) a message is shown that the server is off and the program terminates.  If a key is pressed, and the server is off, the user is prompted to change to a different server.  I'm trying to make it as difficult as possible for a user to change to a different server.
John TsioumprisSoftware & Systems Engineer
Interesting thinking....how about loading a small hidden form that watches for the key and then proceed...
I found a post about running a process in memory but its a bit of a hack : http://www.vbforums.com/showthread.php?728585-How-do-I-execute-a-program-from-memory
Hi John,
Thanks for the reply.  I don't like "hacks" in production software.  Down the road, they tend to cause unforeseen problems.  I'm thinking of something using one of the 10 billion (seems like) APIs. Something like this:
Public Declare Function GetKeyState Lib "User32.Dll" (ByVal nVirtKey As Long) As Integer

Global iFlag As Integer


Public Sub Main()                                    ' Program entry point
    Dim lCnt As Long
    
    iFlag = GetKeyState(&HA1)        ' Right Shift key
    Form1.Show
    
End Sub

Open in new window



And then in Form1_Load:
Private Sub Form_Load()
    If iFlag = -128 Then
        Form1.labMain.Caption = "Key Pressed" & iFlag
    Else
        Form1.labMain.Caption = "Key not pressed" & iFlag
    End If    
End Sub

Open in new window

After trying this, I think I can make it work.  I'm trying to make it as in-obvious as possible.  There is also a toggle bit (&H01) which says that the key has been pressed at some time in the passed.  By the way, Both Excel and Word (and possibly other Office products use the CTRL key at start-up time to set the Safe Mode.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial