在win下用得最多的键盘模拟就是keybd_event
其函数原型:
Declare Sub keybd_event Lib "user32" (ByVal bVk As Long, ByVal bScan As Long, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
绝大多数人在应用这个函数时都没有正确输入参数:bScan、dwFlags,导致按键模拟只对普通窗体有效,而对directx游戏无效。bScan:普通按键对dx生效,dwFlags方向键等扩展键对dx生效。只有bScan,方向键是无效的。
bScan参数比较好获取,用函数MapVirtualKey(vkCode, 0)即可取得,但是dwFlags稍微麻烦点,可以在拦截键盘信息时获取。
将键盘、鼠标模拟封装成一个类如下:
Option Explicit
'copyright guoyong_cy 2010 QQ:37640946
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Long, ByVal bScan As Long, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'Private Const KEYEVENTF_EXTE