|
-
Apr 23rd, 2001, 06:07 AM
#1
Thread Starter
Junior Member
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!
-
Apr 23rd, 2001, 11:48 AM
#2
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?
-
Apr 23rd, 2001, 02:33 PM
#3
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
-
Apr 23rd, 2001, 03:11 PM
#4
Thread Starter
Junior Member
neither work
Thanks for trying, but neither of those ideas work .... Anyone else have an idea?
-
Apr 23rd, 2001, 04:29 PM
#5
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)
-
Apr 23rd, 2001, 05:26 PM
#6
Thread Starter
Junior Member
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.
-
Apr 23rd, 2001, 06:42 PM
#7
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?
-
Apr 23rd, 2001, 06:46 PM
#8
Thread Starter
Junior Member
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...
-
Apr 23rd, 2001, 06:54 PM
#9
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.
-
Apr 23rd, 2001, 07:02 PM
#10
Thread Starter
Junior Member
yeah, it's a mouseclick
hey, do you have aim or icq? if so, private message me and we can talk for real...
-
Apr 24th, 2001, 03:17 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|