VB模拟组合键
PostMessag无法模拟组合键,它似乎是发送一个按键以后才发送第二个!我试了不释放按键,还是没有效果!keybd_event似乎不可以向指定窗口发送呢。。还有别的方法...
PostMessag无法模拟组合键,它似乎是发送一个按键以后才发送第二个!我试了不释放按键,还是没有效果!
keybd_event 似乎不可以向指定窗口发送呢。。 还有别的方法吗?
我要向指定窗口发送组合键
请各位高手帮忙 展开
keybd_event 似乎不可以向指定窗口发送呢。。 还有别的方法吗?
我要向指定窗口发送组合键
请各位高手帮忙 展开
2个回答
展开全部
'这个代码在记事本上验证是可以的,动作是按ALT+F,但是不保证在游戏里是可以的,因为游戏里有些窗口是接收不到你的按键消息的,还有一些屏蔽了sendmessage这些动作,不明白的百度HI我吧
ALT键的消息跟别的不一样,其它组合键也不是那种你不发送弹起就可以的,要增加一个特殊的标志位才可以
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 "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (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 Const WM_SYSKEYDOWN = &H104
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 Command1_Click()
Dim jsb As Long
jsb = FindWindow("notepad", vbNullString)
Dim mhwnd As Long
mhwnd = FindWindowEx(jsb, 0, "edit", vbNullString)
PostMessage mhwnd, WM_SYSKEYDOWN, vbKeyF, &H20000000
End Sub
Private Function makelparam(ByVal VirtualKey As Long, ByVal flag As Boolean) As Long
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = False Then 'keydown
Firstbyte = "00"
Else
Firstbyte = "C0" 'keyup
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位,即发送次数
makelparam = Val("&H" & s)
End Function
ALT键的消息跟别的不一样,其它组合键也不是那种你不发送弹起就可以的,要增加一个特殊的标志位才可以
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 "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (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 Const WM_SYSKEYDOWN = &H104
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 Command1_Click()
Dim jsb As Long
jsb = FindWindow("notepad", vbNullString)
Dim mhwnd As Long
mhwnd = FindWindowEx(jsb, 0, "edit", vbNullString)
PostMessage mhwnd, WM_SYSKEYDOWN, vbKeyF, &H20000000
End Sub
Private Function makelparam(ByVal VirtualKey As Long, ByVal flag As Boolean) As Long
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = False Then 'keydown
Firstbyte = "00"
Else
Firstbyte = "C0" 'keyup
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位,即发送次数
makelparam = Val("&H" & s)
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询