急:求助postmessage和sendmessage的消息问题---急
急:求助postmessage和sendmessage的消息问题---急急死我了,搞了几个晚上了还是没结果。。。请高手指教!!!不胜感谢!!!
如下由spy++监控到的,正常键盘按0得到的。
<00028> 000201D0 P WM_KEYDOWN nVirtKey:'0' cRepeat:1 ScanCode:0B fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00029> 000201D0 P WM_CHAR chCharCode:'0030' (48) cRepeat:1 ScanCode:0B fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00030> 000201D0 P WM_KEYUP nVirtKey:'0' cRepeat:1 ScanCode:0B fExtended:0 fAltDown:0 fRepeat:1 fUp:1
我的程序对应部分:
模块部分申明:
Public Const WM_CHAR = &H102
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const VK_0 = &H30
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Public 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
窗体
PostMessage hWnd5, WM_KEYDOWN, VK_0, MakeKeyLparam(VK_0, WM_KEYDOWN)
PostMessage hWnd5, WM_CHAR, Asc("0"), MakeKeyLparam(VK_0, WM_KEYDOWN)
PostMessage hWnd5, WM_KEYUP, VK_0, MakeKeyLparam(VK_0, WM_KEYUP)
所以游戏程序应该是没有屏蔽sendmessage和postmessage的,但从spy++监控到的模拟按键变成了5条消息,其中crepeat不对,ScanCode:也不对,我想多出来的两条消息应该是没有关系的,
主要是3,4,5条消息也不对连scancode都不对,另外第5行更是fRepeat:1 fUp:1也不对了。红色标的是不一样的地方,蓝色是多出来的两条消息。
<00003> 000201D0 P WM_KEYDOWN nVirtKey:'0' cRepeat:61908 ScanCode:13 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00004> 000201D0 P WM_CHAR chCharCode:'0030' (48) cRepeat:61908 ScanCode:13 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00005> 000201D0 P WM_KEYUP nVirtKey:'0' cRepeat:61908 ScanCode:13 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00006> 000201D0 P WM_CHAR chCharCode:'0030' (48) cRepeat:61908 ScanCode:13 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00007> 000201D0 P WM_CHAR chCharCode:'0030' (48) cRepeat:61908 ScanCode:13 fExtended:0 fAltDown:0 fRepeat:0 fUp:0 ------------多出来的。。。
请问高手们要怎么办呢。游戏是一个没人玩的游戏叫做光之国度。也有看到论坛说要用call,不过小弟不会,又正好看到有人跟帖说可以用消息,主要是参数。正在学习模拟按键想先搞懂下后台发送的,
不想用全局的。sendmessage或者postmessage的这个api怎么才能让他模拟出来cRepeat:1 ScanCode:0B和正确的fRepeat:1 fUp:1。是不是可以有办法自定义sendmessage或者postmessage的结构呢?或者
是不是MakeKeyLparam()这个函数不对呢?又或是我理解的完全错了。不胜感谢!!!!小弟是菜鸟没有什么分。。还是希望有好心的大虾相助。。。
--------------------编程问答-------------------- 共同学习,本人VB不太熟,不知道怎么写好,怕写错了,所以给出Delphi的代码:
// 按下键
lp := $01000001 or (MapVirtualKey(Ord(cc), 0) shl 16);
PostMessage(win, WM_KEYDOWN, WPARAM(cc), lp);
Sleep(10);
// 放开键
lp := $C1000001 or (MapVirtualKey(Ord(cc), 0) shl 16);
PostMessage(win, WM_KEYUP, WPARAM(cc), lp);
Sleep(10);
// 上面代码中:
or 是“或”的意思,
shl是左移操作
cc是要发送的虚拟键或字符 --------------------编程问答-------------------- 上面少说了一句,就是不需要在KEY_DOWN和KEY_UP之间发送WM_CHAR消息,那个是发送KEY_DOWN后系统产生的,只要在两个消息中间稍微停顿一下(Sleep(10))就可以了。 --------------------编程问答-------------------- 楼上的有理,要做一个适当的延时。 --------------------编程问答-------------------- 把 postmessage,sendmessage 的lparam声明成 byval 或者 写成byval xx ,如果不行,是你的代码问题
补充:VB , API