Results 1 to 15 of 15

Thread: VB 6.0: Keypress from another app?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    VB 6.0: Keypress from another app?

    How would you detect keypresses when another applacation has focus?

    For example Ctrl-v.

    I'm trying to make a 3ed party pasting program for a game called PlaneShift that dosen't currently have any copy or past support.

  2. #2
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: VB 6.0: Keypress from another app?

    How can a program receive a keyboard input if the program does not have Focus?
    Or do you mean, prog-A receive a keyboard input and send it to prog-b ?

  3. #3
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: VB 6.0: Keypress from another app?

    Boys,
    you can send a CTRL + V to a form even if it does not have focus as follows.

    Code:
    SendMessage(hwnd,WM_CHAR, 22, 0, 0) ' Ctrl+c =3 , Ctrl+X=24, ctrl + v=22

  4. #4
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: VB 6.0: Keypress from another app?

    I believe I've seen code for hooking keyboard events, even when your own app doesn't have focus.
    But I've never used it.

    Try searching for 'Key Logger' code

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB 6.0: Keypress from another app?

    Longwolf has the idea.
    and its
    How can a program receive a keyboard input if the program does not have Focus?

  6. #6
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: VB 6.0: Keypress from another app?

    here's a search I did at PSC. May have something you can use
    http://www.planet-source-code.com/vb...=Keyboard+hook

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: VB 6.0: Keypress from another app?

    Wouldn't something like this work?

    Code:
    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Timer1_Timer()
    
        Static KeysInUse As Boolean
        
        If GetAsyncKeyState(vbKeyControl) < 0 And GetAsyncKeyState(vbKeyV) < 0 Then
            If KeysInUse = False Then
                KeysInUse = True
                Debug.Print "Control+V Pressed"
                ' Call Your Sub/Function here
                Exit Sub
            End If
        Else
            KeysInUse = False
        End If
        
    End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB 6.0: Keypress from another app?

    Is there anyway to make it more event driven?

  9. #9
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: VB 6.0: Keypress from another app?

    Does the program have focus?
    If not, Timer, or something needs to be sent to it so an event will occur.
    How can an event happen?
    Timer, mouse move, key pressed, program received somthing via 'SendMessage',.....

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB 6.0: Keypress from another app?

    The game would have the focus, so no.

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB 6.0: Keypress from another app?

    You mean to say that you want to detect if ctrl+v is pressed while the game is in focus?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB 6.0: Keypress from another app?

    Correct.
    Edgemeal's method works, but the timer needs to be set to a very fast speed in order for the program to respond to it. Right now I have the timer set to 10.

  13. #13
    Addicted Member
    Join Date
    Jul 2006
    Location
    Adelaide, Australia
    Posts
    204

    Re: VB 6.0: Keypress from another app?

    how about a global hotkey

    most likely you want to detect a combination of keys being pressed - a hotkey it doesnt matter which app has focus.

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB 6.0: Keypress from another app?

    Have you tried the suggestion of longwolf in post #6?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15
    New Member
    Join Date
    Jun 2008
    Posts
    8

    Re: VB 6.0: Keypress from another app?

    Quote Originally Posted by Edgemeal View Post
    Wouldn't something like this work?

    Code:
    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Timer1_Timer()
    
        Static KeysInUse As Boolean
        
        If GetAsyncKeyState(vbKeyControl) < 0 And GetAsyncKeyState(vbKeyV) < 0 Then
            If KeysInUse = False Then
                KeysInUse = True
                Debug.Print "Control+V Pressed"
                ' Call Your Sub/Function here
                Exit Sub
            End If
        Else
            KeysInUse = False
        End If
        
    End Sub
    i think this code best than hot keys codes thx

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