In fact, I have nothing to say, but I have asked a lot of people recently, so I wrote a few words.
To put it simply, there are two points to note: one is to use postmessage to send messages, the other is that the lparam parameters of the two messages are complicated, and the lparam parameter must be constructed during message sending, the following sample code is provided:
Option explicit
Private declare function mapvirtualkey lib "USER32" alias "mapvirtualkeya" (byval wcode as long, byval wmaptype as long) as long
Private declare function sendmessage lib "USER32" alias "sendmessagea" (byval hwnd as long, byval wmsg as long, byval wparam as long, lparam as any) as long
Private declare function findwindow lib "USER32" alias "find0000wa" (byval lpclassname as string, byval lpwindowname as string) as long
Private declare function find1_wex lib "USER32" alias "find1_wexa" (byval hwnd1 as long, byval hwnd2 as long, byval lpsz1 as string, byval lpsz2 as string) as long
Private const wm_keydown = & h100
Private const wm_keyup = & H101
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 sub commandementclick ()
Dim JSB as long
JSB = findwindow ("Notepad", vbnullstring)
Dim mhwnd as long
Mhwnd = find1_wex (JSB, 0, "edit", vbnullstring)
Dim lparam as long
Lparam = makelparam (vbkey5, false)
Postmessage mhwnd, wm_keydown, vbkey5, lparam
Lparam = makelparam (vbkey5, true)
Postmessage mhwnd, wm_keyup, vbkey5, lparam
End sub
Private function makelparam (byval virtualkey as long, byval flag as Boolean) as long
Dim s as string
24-31 digits of the dim firstbyte as string 'lparam Parameter
If flag = false then' keydown
Firstbyte = "00"
Else
Firstbyte = "C0" 'keyup
End if
Dim scancode as long
'Obtain the virtual key scan code
Scancode = mapvirtualkey (virtualkey, 0)
The 16-23 bits of the dim secondbyte as string 'lparam parameter, that is, the virtual key scan code.
Secondbyte = right ("00" & hex (scancode), 2)
S = firstbyte & secondbyte & "0001" '0001 is the 0-15 bits of the lparam parameter, that is, the number of sent messages.
Makelparam = Val ("& H" & S)
End Function