Please help support this free site and future BRL projects!

Login or Signup

How To Send Keystrokes To Extern Win Application ?

Miscellaneous Forums/Win32 Discussion/How To Send Keystrokes To Extern Win Application ?

semar(Posted 1+ years ago) #1
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 ?

Anyone ?


Perturbatio(Posted 1+ years ago) #2
download freepascal and look at the win32 units.

Sarge(Posted 1+ years ago) #3
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.

Sweenie(Posted 1+ years ago) #4

Some values may only be valid on a certain OS

semar(Posted 1+ years ago) #5
Thanks all, but I still need help...

This command close the target application:
SendMessageA( hWnd,WM_CLOSE,0,0)

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
and with
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 !

Sarge(Posted 1+ years ago) #6
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.

Sarge(Posted 1+ years ago) #7
Here is another example using WM_CHAR,

semar(Posted 1+ years ago) #8
Great Sarge, thanks !


Do you know how to figure out also ALT key ?


Sarge(Posted 1+ years ago) #9
Here are all the keycodes for WM_CHAR,

Ex: SendMessage( edit, WM_CHAR, Byte Ptr(VK_A), Null )

semar(Posted 1+ years ago) #10
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

Sarge(Posted 1+ years ago) #11
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"

semar(Posted 1+ years ago) #12
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.

Best regards,

gman(Posted 1+ years ago) #13
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 :)