Results 1 to 11 of 11

Thread: keyboard events

  1. #1

    Thread Starter
    Junior Member inxil's Avatar
    Join Date
    Mar 2000
    Posts
    23

    Question

    I'm having trouble sending a keyboard event to a particular program. I'd like to send UpArrow, DnArrow, LeftArrow, And RightArrow keypresses to a certain program while it doesn't have focus. I've tried SendMessageByNum using WM_KEYDOWN, VK_RETURN, VK_SPACE, and WM_KEYUP (as you might be able to see, i don't really know what i'm doing, and i just test things out until they work). Next I tried keyboard_event but as far as I can tell, you can't send it to a particular program. Anyone that could help me, it'd be greatly appreciated. Thanks!

  2. #2
    Tygur
    Guest
    SendMessageMyNum should work ok. I'd be sending both the WM_KEYDOWN and WM_KEYUP messages, since many programs expect the key to go down, then up.

    Here's how you can send UpArrow to a textbox. This is just an example. You can take out Text1.hwnd and put the variable name that has the hWnd for the window you're trying to send to.
    SendMessageByNum Text1.hwnd, WM_KEYDOWN, VK_UP, 1
    SendMessageByNum Text1.hwnd, WM_KEYUP, VK_UP, 1

    Do you know how to get the hWnd for the program you're trying to send the messages to?

  3. #3
    Megatron
    Guest
    Try this.
    Code:
    ' In General Section of Form
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Const WM_KEYDOWN = &H100
    Private Const WM_KEYUP = &H101
    Usage:
    Code:
    PostMessage MyHandle, WM_KEYDOWN, vbKeyUp, 0
    PostMessage MyHandle, WM_KEYUP, vbKeyUp, 0

  4. #4

    Thread Starter
    Junior Member inxil's Avatar
    Join Date
    Mar 2000
    Posts
    23

    neither work

    Thanks for trying, but neither of those ideas work .... Anyone else have an idea?

  5. #5
    Megatron
    Guest
    Just a few questions: What program are you trying to send it to? What do you want to accomplish by doing it? (Reason for the questions is there is probably another method or you're not using this one right)

  6. #6

    Thread Starter
    Junior Member inxil's Avatar
    Join Date
    Mar 2000
    Posts
    23
    What program are you trying to send it to?
    I'm actually trying to send it to a game. It's a several year old 3D game (windows based, but built upon the doom 3d engine i believe). When you're playing the game, there are 3 'parts' of the main form. One is the visual part, one is the text, and one is your inventory. Depending on which part of the form is selected, a yellow highlight surrounds the part. When the program is out of focus, the yellow highlight disappears. When I move a window spy over the visual area, it appears to not be a separate control, but part of the main form, because the class and handle are the same as the form itself. I'm afraid this might mean that the program has to have focus and the visual part needs to be selected for anything to work, but so far, the only way i've gotten anything to work is by useing the keyboard_event sub [Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)] which only works when the form has focus and the visual part is selected.


    What do you want to accomplish by doing it?
    All I want to be able to do is have a program send forward,back,left and right commands (shift and alt would be nice... like shift+left or alt+left not separate) to the visual part of the form WHILE IT IS OUT OF FOCUS so that i move forward, back et cetera appropriately.

  7. #7
    Megatron
    Guest
    Well, since using the keybd_event function works then the Form is in focus, what if you use SetForegroundWindow to set the window to the foreground, then use keybd_event to send the keys?

  8. #8

    Thread Starter
    Junior Member inxil's Avatar
    Join Date
    Mar 2000
    Posts
    23
    I want it to be minimized while the program is sending the keystrokes to it, and I want to be able to freely use the computer while the program is running, without the game form popping up all the time...

  9. #9
    Megatron
    Guest
    Since you said it's all part of the same window, we can't distinguish them by handles. How do you get the yellow rectangle to appear on the game? e.g: If it's a mouse click, we could simulate the mouse click, then simulate the KeyPress.

  10. #10

    Thread Starter
    Junior Member inxil's Avatar
    Join Date
    Mar 2000
    Posts
    23
    yeah, it's a mouseclick

    hey, do you have aim or icq? if so, private message me and we can talk for real...

  11. #11
    Megatron
    Guest
    This should work for clicking the mouse in the specified spot (assuming you want to click the LEFT button; if now, change to [b]WM_RBUTTON (DOWN/UP)
    Code:
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    
    Function MakeWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
       MakeWord = (HiWord * &H10000) Or (LoWord And &HFFFF&)
    End Function
    
    '---------------------------------------
    ' hWnd = Handle of window to send to
    ' X = X coordinate
    ' Y = Y coordinate
    ' --------------------------------------
    Sub ClickMouse(ByVal hWnd As Long, ByVal X As Integer, ByVal Y As Integer)
        Dim DWORD As Long
        DWORD = MakeWord(X, Y)
        PostMessage hWnd, WM_LBUTTONDOWN, 1, DWORD
        PostMessage hWnd, WM_LBUTTONUP, 1, DWORD
    End Sub
    Usage:
    Code:
    'Clicks the mouse at coordinates 100 x 100
    ClickMouse Handle_To_Window, 100, 100
    If it doesn't work, then you can PM me.

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