VB: how to send wm_keydown and wm_keyup messages

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.