How To Send Keystrokes To Extern Win Application ?

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

All,
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 ?

Sergio.

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.

Const WM_NULL                         = $000
Const WM_CREATE                       = $001
Const WM_DESTROY                      = $002
Const WM_MOVE                         = $003
Const WM_SIZE                         = $005
Const WM_ACTIVATE                     = $006
Const WM_SETFOCUS                     = $007
Const WM_KILLFOCUS                    = $008
Const WM_ENABLE                       = $00A
Const WM_SETREDRAW                    = $00B
Const WM_SETTEXT                      = $00C
Const WM_GETTEXT                      = $00D
Const WM_GETTEXTLENGTH                = $00E
Const WM_PAINT                        = $00F
Const WM_CLOSE                        = $010
Const WM_QUERYENDSESSION              = $011
Const WM_QUIT                         = $012
Const WM_QUERYOPEN                    = $013
Const WM_ERASEBKGND                   = $014
Const WM_SYSCOLORCHANGE               = $015
Const WM_ENDSESSION                   = $016
Const WM_SHOWWINDOW                   = $018
Const WM_WININICHANGE                 = $01A
Const WM_DEVMODECHANGE                = $01B
Const WM_ACTIVATEAPP                  = $01C
Const WM_FONTCHANGE                   = $01D
Const WM_TIMECHANGE                   = $01E
Const WM_CANCELMODE                   = $01F
Const WM_SETCURSOR                    = $020
Const WM_MOUSEACTIVATE                = $021
Const WM_CHILDACTIVATE                = $022
Const WM_QUEUESYNC                    = $023
Const WM_GETMINMAXINFO                = $024
Const WM_PAINTICON                    = $026
Const WM_ICONERASEBKGND               = $027
Const WM_NEXTDLGCTL                   = $028
Const WM_SPOOLERSTATUS                = $02A
Const WM_DRAWITEM                     = $02B
Const WM_MEASUREITEM                  = $02C
Const WM_DELETEITEM                   = $02D
Const WM_VKEYTOITEM                   = $02E
Const WM_CHARTOITEM                   = $02F
Const WM_SETFONT                      = $030
Const WM_GETFONT                      = $031
Const WM_SETHOTKEY                    = $032
Const WM_GETHOTKEY                    = $033
Const WM_QUERYDRAGICON                = $037
Const WM_COMPAREITEM                  = $039
Const WM_COMPACTING                   = $041
Const WM_COMMNOTIFY                   = $044  '/* no longer suported */
Const WM_WINDOWPOSCHANGING            = $046
Const WM_WINDOWPOSCHANGED             = $047
Const WM_POWER                        = $048
Const WM_COPYDATA                     = $04A
Const WM_CANCELJOURNAL                = $04B
Const WM_NOTIFY                       = $04E
Const WM_INPUTLANGCHANGEREQUEST       = $050
Const WM_INPUTLANGCHANGE              = $051
Const WM_TCARD                        = $052
Const WM_HELP                         = $053
Const WM_USERCHANGED                  = $054
Const WM_NOTIFYFORMAT                 = $055
Const WM_CONTEXTMENU                  = $07B
Const WM_STYLECHANGING                = $07C
Const WM_STYLECHANGED                 = $07D
Const WM_DISPLAYCHANGE                = $07E
Const WM_GETICON                      = $07F
Const WM_SETICON                      = $080
Const WM_NCCREATE                     = $081
Const WM_NCDESTROY                    = $082
Const WM_NCCALCSIZE                   = $083
Const WM_NCHITTEST                    = $084
Const WM_NCPAINT                      = $085
Const WM_NCACTIVATE                   = $086
Const WM_GETDLGCODE                   = $087
Const WM_SYNCPAINT                    = $088
Const WM_NCMOUSEMOVE                  = $0A0
Const WM_NCLBUTTONDOWN                = $0A1
Const WM_NCLBUTTONUP                  = $0A2
Const WM_NCLBUTTONDBLCLK              = $0A3
Const WM_NCRBUTTONDOWN                = $0A4
Const WM_NCRBUTTONUP                  = $0A5
Const WM_NCRBUTTONDBLCLK              = $0A6
Const WM_NCMBUTTONDOWN                = $0A7
Const WM_NCMBUTTONUP                  = $0A8
Const WM_NCMBUTTONDBLCLK              = $0A9
Const WM_NCXBUTTONDOWN                = $0AB
Const WM_NCXBUTTONUP                  = $0AC
Const WM_NCXBUTTONDBLCLK              = $0AD
Const WM_INPUT                        = $0FF
Const WM_KEYFIRST                     = $100
Const WM_KEYDOWN                      = $100
Const WM_KEYUP                        = $101
Const WM_CHAR                         = $102
Const WM_DEADCHAR                     = $103
Const WM_SYSKEYDOWN                   = $104
Const WM_SYSKEYUP                     = $105
Const WM_SYSCHAR                      = $106
Const WM_SYSDEADCHAR                  = $107
Const WM_UNICHAR                      = $109
Const WM_KEYLAST                      = $109
Const WM_IME_STARTCOMPOSITION         = $10D
Const WM_IME_ENDCOMPOSITION           = $10E
Const WM_IME_COMPOSITION              = $10F
Const WM_IME_KEYLAST                  = $10F
Const WM_INITDIALOG                   = $110
Const WM_COMMAND                      = $111
Const WM_SYSCOMMAND                   = $112
Const WM_TIMER                        = $113
Const WM_HSCROLL                      = $114
Const WM_VSCROLL                      = $115
Const WM_INITMENU                     = $116
Const WM_INITMENUPOPUP                = $117
Const WM_MENUSELECT                   = $11F
Const WM_MENUCHAR                     = $120
Const WM_ENTERIDLE                    = $121
Const WM_MENURBUTTONUP                = $122
Const WM_MENUDRAG                     = $123
Const WM_MENUGETOBJECT                = $124
Const WM_UNINITMENUPOPUP              = $125
Const WM_MENUCOMMAND                  = $126
Const WM_CHANGEUISTATE                = $127
Const WM_UPDATEUISTATE                = $128
Const WM_QUERYUISTATE                 = $129
Const WM_CTLCOLORMSGBOX               = $132
Const WM_CTLCOLOREDIT                 = $133
Const WM_CTLCOLORLISTBOX              = $134
Const WM_CTLCOLORBTN                  = $135
Const WM_CTLCOLORDLG                  = $136
Const WM_CTLCOLORSCROLLBAR            = $137
Const WM_CTLCOLORSTATIC               = $138
Const MN_GETHMENU                     = $1E1
Const WM_MOUSEFIRST                   = $200
Const WM_MOUSEMOVE                    = $200
Const WM_LBUTTONDOWN                  = $201
Const WM_LBUTTONUP                    = $202
Const WM_LBUTTONDBLCLK                = $203
Const WM_RBUTTONDOWN                  = $204
Const WM_RBUTTONUP                    = $205
Const WM_RBUTTONDBLCLK                = $206
Const WM_MBUTTONDOWN                  = $207
Const WM_MBUTTONUP                    = $208
Const WM_MBUTTONDBLCLK                = $209
Const WM_MOUSEWHEEL                   = $20A
Const WM_XBUTTONDOWN                  = $20B
Const WM_XBUTTONUP                    = $20C
Const WM_XBUTTONDBLCLK                = $20D
Const WM_MOUSELAST                    = $20A
Const WM_PARENTNOTIFY                 = $210
Const WM_ENTERMENULOOP                = $211
Const WM_EXITMENULOOP                 = $212
Const WM_NEXTMENU                     = $213
Const WM_SIZING                       = $214
Const WM_CAPTURECHANGED               = $215
Const WM_MOVING                       = $216
Const WM_POWERBROADCAST               = $218
Const WM_DEVICECHANGE                 = $219
Const WM_MDICREATE                    = $220
Const WM_MDIDESTROY                   = $221
Const WM_MDIACTIVATE                  = $222
Const WM_MDIRESTORE                   = $223
Const WM_MDINEXT                      = $224
Const WM_MDIMAXIMIZE                  = $225
Const WM_MDITILE                      = $226
Const WM_MDICASCADE                   = $227
Const WM_MDIICONARRANGE               = $228
Const WM_MDIGETACTIVE                 = $229
Const WM_MDISETMENU                   = $230
Const WM_ENTERSIZEMOVE                = $231
Const WM_EXITSIZEMOVE                 = $232
Const WM_DROPFILES                    = $233
Const WM_MDIREFRESHMENU               = $234
Const WM_IME_SETCONTEXT               = $281
Const WM_IME_NOTIFY                   = $282
Const WM_IME_CONTROL                  = $283
Const WM_IME_COMPOSITIONFULL          = $284
Const WM_IME_SELECT                   = $285
Const WM_IME_CHAR                     = $286
Const WM_IME_REQUEST                  = $288
Const WM_IME_KEYDOWN                  = $290
Const WM_IME_KEYUP                    = $291
Const WM_MOUSEHOVER                   = $2A1
Const WM_MOUSELEAVE                   = $2A3
Const WM_NCMOUSEHOVER                 = $2A0
Const WM_NCMOUSELEAVE                 = $2A2
Const WM_WTSSESSION_CHANGE            = $2B1
Const WM_TABLET_FIRST                 = $2c0
Const WM_TABLET_LAST                  = $2df
Const WM_CUT                          = $300
Const WM_COPY                         = $301
Const WM_PASTE                        = $302
Const WM_CLEAR                        = $303
Const WM_UNDO                         = $304
Const WM_RENDERFORMAT                 = $305
Const WM_RENDERALLFORMATS             = $306
Const WM_DESTROYCLIPBOARD             = $307
Const WM_DRAWCLIPBOARD                = $308
Const WM_PAINTCLIPBOARD               = $309
Const WM_VSCROLLCLIPBOARD             = $30A
Const WM_SIZECLIPBOARD                = $30B
Const WM_ASKCBFORMATNAME              = $30C
Const WM_CHANGECBCHAIN                = $30D
Const WM_HSCROLLCLIPBOARD             = $30E
Const WM_QUERYNEWPALETTE              = $30F
Const WM_PALETTEISCHANGING            = $310
Const WM_PALETTECHANGED               = $311
Const WM_HOTKEY                       = $312
Const WM_PRINT                        = $317
Const WM_PRINTCLIENT                  = $318
Const WM_APPCOMMAND                   = $319
Const WM_THEMECHANGED                 = $31A
Const WM_HANDHELDFIRST                = $358
Const WM_HANDHELDLAST                 = $35F
Const WM_AFXFIRST                     = $360
Const WM_AFXLAST                      = $37F
Const WM_PENWINFIRST                  = $380
Const WM_PENWINLAST                   = $38F

Some values may only be valid on a certain OS

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.

Sergio.

P.S.
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,

Const WM_SETTEXT = $00C

Extern "Win32"

	Function FindWindow( lpClassName$z, lpWindowName$z ) = "FindWindowA@8"
	Function FindWindowEx( hwnd1:Int, hwnd2:Int, lpsz1$z, lpsz2$z ) = "FindWindowExA@16"
	Function SendMessage( hwnd, msg, wparam:Byte Ptr, lparam:Byte Ptr ) = "SendMessageA@16"
	Function GetLastError()

End Extern

'win=FindWindow( "SciCalc", "Calculator" )

win=FindWindow( "NotePad", "Untitled - Notepad" )
edit=FindWindowEx( win, Null, "Edit", Null )

SendMessage( edit, WM_SETTEXT, Null, "hi" )

open notepad.exe before you run that.

Here is another example using WM_CHAR,
Const WM_CHAR = $102

Extern "Win32"

	Function FindWindow( lpClassName$z, lpWindowName$z ) = "FindWindowA@8"
	Function FindWindowEx( hwnd1:Int, hwnd2:Int, lpsz1$z, lpsz2$z ) = "FindWindowExA@16"
	Function SendMessage( hwnd, msg, wparam:Byte Ptr, lparam:Byte Ptr ) = "SendMessageA@16"
	Function GetLastError()

End Extern

win=FindWindow( "NotePad", "Untitled - Notepad" )
edit=FindWindowEx( win, Null, "Edit", Null )

SendMessage( edit, WM_CHAR, Byte Ptr(9), Null ) ' Tab
SendMessage( edit, WM_CHAR, Byte Ptr(10), Null ) ' New Line


Great Sarge, thanks !

:)

[EDITED]
Do you know how to figure out also ALT key ?

Sergio.

Here are all the keycodes for WM_CHAR,
Const VK_0 = $30
Const VK_1 = $31
Const VK_2 = $32
Const VK_3 = $33
Const VK_4 = $34
Const VK_5 = $35
Const VK_6 = $36
Const VK_7 = $37
Const VK_8 = $38
Const VK_9 = $39
Const VK_A = $41
Const VK_B = $42
Const VK_C = $43
Const VK_D = $44
Const VK_E = $45
Const VK_F = $46
Const VK_G = $47
Const VK_H = $48
Const VK_I = $49
Const VK_J = $4A
Const VK_K = $4B
Const VK_L = $4C
Const VK_M = $4D
Const VK_N = $4E
Const VK_O = $4F
Const VK_P = $50
Const VK_Q = $51
Const VK_R = $52
Const VK_S = $53
Const VK_T = $54
Const VK_U = $55
Const VK_V = $56
Const VK_W = $57
Const VK_X = $58
Const VK_Y = $59
Const VK_Z = $5A
Const VK_ADD = $6B
Const VK_ATTN = $F6
Const VK_BACK = $8
Const VK_CANCEL = $3
Const VK_CAPITAL = $14
Const VK_CLEAR = $C
Const VK_CONTROL = $11
Const VK_CRSEL = $F7
Const VK_DECIMAL = $6E
Const VK_DELETE = $2E
Const VK_DIVIDE = $6F
Const VK_DOWN = $28
Const VK_END = $23
Const VK_EREOF = $F9
Const VK_ESCAPE = $1B
Const VK_EXECUTE = $2B
Const VK_EXSEL = $F8
Const VK_F1 = $70
Const VK_F10 = $79
Const VK_F11 = $7A
Const VK_F12 = $7B
Const VK_F13 = $7C
Const VK_F14 = $7D
Const VK_F15 = $7E
Const VK_F16 = $7F
Const VK_F17 = $80
Const VK_F18 = $81
Const VK_F19 = $82
Const VK_F2 = $71
Const VK_F20 = $83
Const VK_F21 = $84
Const VK_F22 = $85
Const VK_F23 = $86
Const VK_F24 = $87
Const VK_F3 = $72
Const VK_F4 = $73
Const VK_F5 = $74
Const VK_F6 = $75
Const VK_F7 = $76
Const VK_F8 = $77
Const VK_F9 = $78
Const VK_HELP = $2F
Const VK_HOME = $24
Const VK_INSERT = $2D
Const VK_LBUTTON = $1
Const VK_LCONTROL = $A2
Const VK_LEFT = $25
Const VK_LMENU = $A4
Const VK_LSHIFT = $A0
Const VK_MBUTTON = $4
Const VK_MENU = $12
Const VK_MULTIPLY = $6A
Const VK_NEXT = $22
Const VK_NONAME = $FC
Const VK_NUMLOCK = $90
Const VK_NUMPAD0 = $60
Const VK_NUMPAD1 = $61
Const VK_NUMPAD2 = $62
Const VK_NUMPAD3 = $63
Const VK_NUMPAD4 = $64
Const VK_NUMPAD5 = $65
Const VK_NUMPAD6 = $66
Const VK_NUMPAD7 = $67
Const VK_NUMPAD8 = $68
Const VK_NUMPAD9 = $69
Const VK_OEM_CLEAR = $FE
Const VK_PA1 = $FD
Const VK_PAUSE = $13
Const VK_PLAY = $FA
Const VK_PRINT = $2A
Const VK_PRIOR = $21
Const VK_PROCESSKEY = $E5
Const VK_RBUTTON = $2
Const VK_RCONTROL = $A3
Const VK_RETURN = $D
Const VK_RIGHT = $27
Const VK_RMENU = $A5
Const VK_RSHIFT = $A1
Const VK_SCROLL = $91
Const VK_SELECT = $29
Const VK_SEPARATOR = $6C
Const VK_SHIFT = $10
Const VK_SNAPSHOT = $2C
Const VK_SPACE = $20
Const VK_SUBTRACT = $6D
Const VK_TAB = $9
Const VK_UP = $26
Const VK_ZOOM = $FB

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"

[edit]
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.

Best regards,
Sergio.

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 :)