VB後台模擬按鍵postmessage鍵盤按鍵碼函數

http://www.suntw.com/vb6/vb-hou-tai-an-jian-post-messag.html 
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 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
 
Private Const WM_CHAR = &H102
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
 
'按Y鍵
PostMessage Handle, WM_CHAR, vbKeyY, 0
'按y鍵
PostMessage Handle, WM_KEYDOWN, vbKeyY, 0
'或
PostMessage Handle, WM_CHAR, vbKeyS, MakeKeyLparam(vbKeyS, WM_KEYDOWN)
 
Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
     Dim s As String
     Dim Firstbyte As String    'lparam參數的24-31位
     If flag = WM_KEYDOWN Then  '如果是按下鍵
         Firstbyte = "00"
     Else
         Firstbyte = "C0"       '如果是釋放鍵
     End If
     Dim Scancode As Long
     '獲得鍵的掃瞄碼
     Scancode = MapVirtualKey(VirtualKey, 0)
     Dim Secondbyte As String   'lparam參數的16-23位,即虛擬鍵掃瞄碼
     Secondbyte = Right("00" & Hex(Scancode), 2)
     s = Firstbyte & Secondbyte & "0001"  '0001為lparam參數的0-15位,即發送次數和其它擴展信息
     MakeKeyLparam = Val("&H" & s)
 End Function

留言

本月最夯

偷用電腦,怎知?事件檢視器全記錄!(開機時間、啟動項時間...)