Results 1 to 10 of 10

Thread: detect key press.......urgent!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    Hi,

    I've posted this before.....but haven't found a solution as yet.....so here I am re-posting.....

    I am running a .exe file from my VB application. I need to detect when a key has been pressed and on detecting a keypress have to stop the .exe file. How do I detect a keypress in this case????

    Also how do I deetect if the mouse has been clicked when the .exe file is running.
    Am using VB6.
    Thanx.


  2. #2
    Guest
    Use GetAsyncKeyState
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
        'If escape is pressed then quit
        If GetAsyncKeyState(vbKeyEscape) Then Unload Me
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    Megatron,

    I need to stop the .exe file no matter which key is
    pressed.

  4. #4
    Guest
    Simply loop through all the keys.
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
        'If any key is pressed then quit
        For I = 3 To 255
            If GetAsyncKeyState(I) Then Unload Me
        Next I
    End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    Megatron,

    Tried ur code, used a timer with an interval of 10 secs....and in the form load event started the exe.
    This is my code:

    Code:
    Public Sub End_exe()
        Dim retval As Long
        retval = FindWindow(vbNullString, "Flash")
        SendMessage retval, WM_CLOSE, 0, 0
        End
    End Sub
    
    Private Sub Timer1_Timer()
        'If any key is pressed then quit
        For I = 3 To 255
            If GetAsyncKeyState(I) Then End_exe
        Next I
    End Sub
    
    Flash --> name of the .exe file

    The problem I'm facing is that the exe starts and then stops immediately. Any ideas?? Must I refresh the key states or anything like that???

    Thanx.

    [Edited by rammy on 09-09-2000 at 03:16 PM]

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298

    Red face

    help..........

  7. #7
    Guest
    Try this - Set the Form's KeyPreview property to true and use this code:

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
    For i = 3 To 255
    If KeyAscii = i Then End_exe
    Next i
    End Sub

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    Mathew,

    That will work if I have detect a keypress on the form. But I have to detect a keypress in an external application.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298

    Red face

    doesn't anybody have any idea????????????? :-((

  10. #10
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    Try this code.
    /code
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function ToAscii Lib "user32" (ByVal uVirtKey As Long, ByVal uScanCode As Long, lpbKeyState As Byte, lpwTransKey As Long, ByVal fuState As Long) As Long


    Private Sub Form_Load()
    Timer1.Interval = 50
    End Sub

    Private Sub Timer1_Timer()
    Dim ikey As Integer
    Dim bkey(255) As Byte
    Dim iasc As Long



    For ikey = 0 To 255
    'Use GetAsyncKeyState to Monitor Keypress From Anywhere in the O/S.
    If GetAsyncKeyState(ikey) And ikey <> VK_SHIFT Then Exit For
    Next


    If ikey < 256 Then

    Call GetKeyboardState(bkey(0))
    Call ToAscii(ikey, 0&, bkey(0), iasc, 0&)

    If iasc Then
    'Store Keypress to Log Here
    Text1.Text = Text1.Text + Chr(iasc)
    End If
    DoEvents

    While GetAsyncKeyState(ikey)
    'Wait for Key to be Released
    Wend

    End If









    End Sub

    code/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width