How To Send Keystrokes To Extern Win Application ?Miscellaneous Forums/Win32 Discussion/How To Send Keystrokes To Extern Win Application ?
I'm using BMAX and I'm trying to control an external windows application.
So far I can successfully use FindWindow, which returns the handle of the application I want to control.
Then, I'm using PostMessage (or SendMessage), but I'm stuck here; so far I can send a WM_CLOSE message but:
1) How to send keystrokes to an application ? Like the SendKey ala VB..
2) I don't know the windows message code values; for example, the WM_CLOSE message is 16, I can read it from the Win32.bmx, but where to find a complete reference list of the available windows messages code values ?
| download freepascal and look at the win32 units. |
| The thing i used to do is,|
Print "Hwnd: "+ int(hwnd) + "MSG: "+ int(msg) + "LParam: "+ int(LParam) + "WParam: " + int(WParam)
in the WinProc then you see what messages the program is getting.
Some values may only be valid on a certain OS
| Thanks all, but I still need help...|
This command close the target application:
Now, how to send, for example, an '9' keystroke to a calculator ? I've tryed with : (57 is the 9)
SendMessageA( hWnd,57,0,0) '9 key
SendMessageA( hWnd,57,WM_KEYDOWN,0) '9 key down
SendMessageA( hWnd,57,WM_KEYUP,0) '9 key up
still no joy. I'm still reaching and googling, so far I can't find an easy example on how to accomplish this task.
Sarge, what do you mean with WinProc ?
Sweenie, that's really useful - thanks !
| I was thinking you were creating a window thats why i said WinProc, dont worry about that.|
I was just playing around and created this, it might give you some tips,
open notepad.exe before you run that.
| Here is another example using WM_CHAR,|
| Great Sarge, thanks !|
Do you know how to figure out also ALT key ?
| Here are all the keycodes for WM_CHAR,|
Ex: SendMessage( edit, WM_CHAR, Byte Ptr(VK_A), Null )
| Argh !|
Const VK_F1 = $70
PostMessageA( hWnd,WM_CHAR,Byte Ptr(VK_F1),Null)
Gives an error:
unable to convert from 'Byte Ptr' to 'Int'
The same if I declare:
Const VK_F1 = 70
| Sorry about that i forgot to add all the $, i just updated it with all of them.|
Anyway about the "unable to convert from 'Byte Ptr' to 'Int'" its because you dont have Wparam and Lparam as :byte ptr in the Extern "Win32"
Ex: Function SendMessage( hwnd, msg, wparam:Byte Ptr, lparam:Byte Ptr ) = "SendMessageA@16"
Function PostMessageA( hwnd, msg, wparam:Byte Ptr, lparam:Byte Ptr ) 'or even Function PostMessage( hwnd, msg, wparam:Byte Ptr, lparam:Byte Ptr ) = "PostMessageA@16"
| Great !|
It works for most of the purpose I need, thank you Sarge, really appreciated.
I just have to figure out how to simulate Function keys, since this:
PostMessageA( hWnd,WM_KEYDOWN,Byte Ptr(VK_F1),null))
does not fire the help (F1).
Also how to get the ALT key working (to access the menu) is still obscure to me.
| i used keybd_event for FranknStik keystroke mappings. a couple of things to keep in mind...|
a) it posts to the top window
b) you must do each part of the stroke, both depress and release
Framework BRL.Blitz Const KEYEVENTF_KEYUP = $00000002 Const VK_MENU = $12 ' the ALT key Const VK_F1 = $70 Const VK_F = $46 Extern "Win32" Function keybd_event(bVK:Byte,bScan:Byte,dwFlags:Int,dwExtraInfo:Int) End Extern keybd_event(VK_MENU,0,0,0) ' press the ALT key keybd_event(VK_F,0,0,0) ' press the F key for the FILE menu keybd_event(VK_F,0,KEYEVENTF_KEYUP,0) ' let up the F key keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0) ' let up the ALT key ' comment the ALT block and uncomment this to do the HELP. ' F1 wont fire If the FILE menu is open which is the result of the above strokes 'keybd_event(VK_F1,0,0,0) ' fire the F1 help 'keybd_event(VK_F1,0,KEYEVENTF_KEYUP,0) ' let up the F1 key
hope this helps you a bit. FranknStik runs in the background and posts to the current window. in order to test this you may want to click the build and run and then switch to notepad quick :)