首页 > 代码库 > 关于 keybd_event (vb篇)
关于 keybd_event (vb篇)
最近有个VB的项目,做到64位的win7上,传统的VB6.0的sendkeys()函数没法用,可能是由于微软的证书验证机制造成的,于是最常用的一句话指令没法用了“SendKeys "{Home}+{End}"”(也就是全选当前的文本框字符)。
很自然的想到的API,轻松的改完:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As LongPrivate Const VK_ADD As Long = &H6B ‘加号键Private Const VK_APPS As Long = &H5D ‘Applications 键(相当于鼠标右键)Private Const VK_BACK As Long = &H8 ‘BackSpace 键Private Const VK_CAPITAL As Long = &H14 ‘Caps Lock 键(大小写转换键)Private Const VK_CANCEL As Long = &H3 ‘Ctrl + Break 过程Private Const VK_CONTROL As Long = &H11Private Const VK_DECIMAL As Long = &H6E ‘小数点号键Private Const VK_DELETE As Long = &H2EPrivate Const VK_DIVIDE As Long = &H6F ‘除号键Private Const VK_DOWN As Long = &H28Private Const VK_END As Long = &H23Private Const VK_ESCAPE As Long = &H1BPrivate Const VK_F1 As Long = &H70Private Const VK_F10 As Long = &H79Private Const VK_F11 As Long = &H7APrivate Const VK_F12 As Long = &H7BPrivate Const VK_F2 As Long = &H71Private Const VK_F3 As Long = &H72Private Const VK_F4 As Long = &H73Private Const VK_F5 As Long = &H74Private Const VK_F6 As Long = &H75Private Const VK_F7 As Long = &H76Private Const VK_F8 As Long = &H77Private Const VK_F9 As Long = &H78Private Const VK_HOME As Long = &H24Private Const VK_INSERT As Long = &H2DPrivate Const VK_LCONTROL As Long = &HA2Private Const VK_LEFT As Long = &H25Private Const VK_LMENU As Long = &HA4 ‘左 Alt 键Private Const VK_LSHIFT As Long = &HA0Private Const VK_LWIN As Long = &H5BPrivate Const VK_MENU As Long = &H12 ‘Alt 键Private Const VK_MULTIPLY As Long = &H6A ‘乘号键Private Const VK_NEXT As Long = &H22 ‘下翻页键Private Const VK_NUMLOCK As Long = &H90Private Const VK_NUMPAD0 As Long = &H60Private Const VK_NUMPAD1 As Long = &H61Private Const VK_NUMPAD2 As Long = &H62Private Const VK_NUMPAD3 As Long = &H63Private Const VK_NUMPAD4 As Long = &H64Private Const VK_NUMPAD5 As Long = &H65Private Const VK_NUMPAD6 As Long = &H66Private Const VK_NUMPAD7 As Long = &H67Private Const VK_NUMPAD8 As Long = &H68Private Const VK_NUMPAD9 As Long = &H69Private Const VK_PAUSE As Long = &H13Private Const VK_PRINT As Long = &H2APrivate Const VK_PRIOR As Long = &H21 ‘上翻页键Private Const VK_RCONTROL As Long = &HA3Private Const VK_RETURN As Long = &HD ‘回车键Private Const VK_RIGHT As Long = &H27Private Const VK_RMENU As Long = &HA5 ‘右 Alt 键Private Const VK_RSHIFT As Long = &HA1Private Const VK_RWIN As Long = &H5CPrivate Const VK_SCROLL As Long = &H91 ‘Scroll Lock 键Private Const VK_SEPARATOR As Long = &H6C ‘小键盘上的回车键Private Const VK_SHIFT As Long = &H10Private Const VK_SLEEP As Long = &H5F ‘休眠键Private Const VK_SNAPSHOT As Long = &H2C ‘Print Screen 键Private Const VK_SPACE As Long = &H20Private Const VK_SUBTRACT As Long = &H6D ‘减号键Private Const VK_TAB As Long = &H9Private Const VK_UP As Long = &H26Private Const VK_OEM_1 As Long = &HBA ‘Windows 2000:对于 US 标准键盘,是“;:”键Private Const VK_OEM_2 As Long = &HBF ‘Windows 2000:对于 US 标准键盘,是“/?”键Private Const VK_OEM_3 As Long = &HC0 ‘Windows 2000:对于 US 标准键盘,是“`~”键Private Const VK_OEM_4 As Long = &HDB ‘Windows 2000:对于 US 标准键盘,是“[{”键Private Const VK_OEM_5 As Long = &HDC ‘Windows 2000:对于 US 标准键盘,是“\|”键Private Const VK_OEM_6 As Long = &HDD ‘Windows 2000:对于 US 标准键盘,是“]}”键Private Const VK_OEM_7 As Long = &HDE ‘Windows 2000:对于 US 标准键盘,是“单/双引号”键Private Const VK_OEM_COMMA As Long = &HBC ‘Windows 2000:对于任何国家/地区,是“,”键Private Const VK_OEM_MINUS As Long = &HBD ‘Windows 2000:对于任何国家/地区,是“-”键Private Const VK_OEM_PERIOD As Long = &HBE ‘Windows 2000:对于任何国家/地区,是“.”键Private Const VK_OEM_PLUS As Long = &HBB ‘Windows 2000:对于任何国家/地区,是“+”键Private Const KEYEVENTF_KEYUP = &H2
Public Sub SendKeysAPI_HOME_END(frmCur As String)‘下面是发送 组合 键码:Call keybd_event(VK_HOME, 0, 0, 0)Call keybd_event(VK_LSHIFT,0, 0, 0)Call keybd_event(VK_END, 0,0, 0)Call keybd_event(VK_END, 0, KEYEVENTF_EXTENDEDKEY, 0)Call keybd_event(VK_LSHIFT, 0,KEYEVENTF_EXTENDEDKEY , 0)Call keybd_event(VK_HOME, 0, KEYEVENTF_EXTENDEDKEY , 0)End Sub
可是测试的时候很多问题,这个函数很简单,网上大家都这么用,为什么我这不行呢,现象是有时没反应,有时shift按下不放,无奈啊,抱着无解的心态打开MSDN,看了下函数说明,才知道用的有问题,修改如下:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As LongPrivate Const VK_ADD As Long = &H6B ‘加号键Private Const VK_APPS As Long = &H5D ‘Applications 键(相当于鼠标右键)Private Const VK_BACK As Long = &H8 ‘BackSpace 键Private Const VK_CAPITAL As Long = &H14 ‘Caps Lock 键(大小写转换键)Private Const VK_CANCEL As Long = &H3 ‘Ctrl + Break 过程Private Const VK_CONTROL As Long = &H11Private Const VK_DECIMAL As Long = &H6E ‘小数点号键Private Const VK_DELETE As Long = &H2EPrivate Const VK_DIVIDE As Long = &H6F ‘除号键Private Const VK_DOWN As Long = &H28Private Const VK_END As Long = &H23Private Const VK_ESCAPE As Long = &H1BPrivate Const VK_F1 As Long = &H70Private Const VK_F10 As Long = &H79Private Const VK_F11 As Long = &H7APrivate Const VK_F12 As Long = &H7BPrivate Const VK_F2 As Long = &H71Private Const VK_F3 As Long = &H72Private Const VK_F4 As Long = &H73Private Const VK_F5 As Long = &H74Private Const VK_F6 As Long = &H75Private Const VK_F7 As Long = &H76Private Const VK_F8 As Long = &H77Private Const VK_F9 As Long = &H78Private Const VK_HOME As Long = &H24Private Const VK_INSERT As Long = &H2DPrivate Const VK_LCONTROL As Long = &HA2Private Const VK_LEFT As Long = &H25Private Const VK_LMENU As Long = &HA4 ‘左 Alt 键Private Const VK_LSHIFT As Long = &HA0Private Const VK_LWIN As Long = &H5BPrivate Const VK_MENU As Long = &H12 ‘Alt 键Private Const VK_MULTIPLY As Long = &H6A ‘乘号键Private Const VK_NEXT As Long = &H22 ‘下翻页键Private Const VK_NUMLOCK As Long = &H90Private Const VK_NUMPAD0 As Long = &H60Private Const VK_NUMPAD1 As Long = &H61Private Const VK_NUMPAD2 As Long = &H62Private Const VK_NUMPAD3 As Long = &H63Private Const VK_NUMPAD4 As Long = &H64Private Const VK_NUMPAD5 As Long = &H65Private Const VK_NUMPAD6 As Long = &H66Private Const VK_NUMPAD7 As Long = &H67Private Const VK_NUMPAD8 As Long = &H68Private Const VK_NUMPAD9 As Long = &H69Private Const VK_PAUSE As Long = &H13Private Const VK_PRINT As Long = &H2APrivate Const VK_PRIOR As Long = &H21 ‘上翻页键Private Const VK_RCONTROL As Long = &HA3Private Const VK_RETURN As Long = &HD ‘回车键Private Const VK_RIGHT As Long = &H27Private Const VK_RMENU As Long = &HA5 ‘右 Alt 键Private Const VK_RSHIFT As Long = &HA1Private Const VK_RWIN As Long = &H5CPrivate Const VK_SCROLL As Long = &H91 ‘Scroll Lock 键Private Const VK_SEPARATOR As Long = &H6C ‘小键盘上的回车键Private Const VK_SHIFT As Long = &H10Private Const VK_SLEEP As Long = &H5F ‘休眠键Private Const VK_SNAPSHOT As Long = &H2C ‘Print Screen 键Private Const VK_SPACE As Long = &H20Private Const VK_SUBTRACT As Long = &H6D ‘减号键Private Const VK_TAB As Long = &H9Private Const VK_UP As Long = &H26Private Const VK_OEM_1 As Long = &HBA ‘Windows 2000:对于 US 标准键盘,是“;:”键Private Const VK_OEM_2 As Long = &HBF ‘Windows 2000:对于 US 标准键盘,是“/?”键Private Const VK_OEM_3 As Long = &HC0 ‘Windows 2000:对于 US 标准键盘,是“`~”键Private Const VK_OEM_4 As Long = &HDB ‘Windows 2000:对于 US 标准键盘,是“[{”键Private Const VK_OEM_5 As Long = &HDC ‘Windows 2000:对于 US 标准键盘,是“\|”键Private Const VK_OEM_6 As Long = &HDD ‘Windows 2000:对于 US 标准键盘,是“]}”键Private Const VK_OEM_7 As Long = &HDE ‘Windows 2000:对于 US 标准键盘,是“单/双引号”键Private Const VK_OEM_COMMA As Long = &HBC ‘Windows 2000:对于任何国家/地区,是“,”键Private Const VK_OEM_MINUS As Long = &HBD ‘Windows 2000:对于任何国家/地区,是“-”键Private Const VK_OEM_PERIOD As Long = &HBE ‘Windows 2000:对于任何国家/地区,是“.”键Private Const VK_OEM_PLUS As Long = &HBB ‘Windows 2000:对于任何国家/地区,是“+”键Private Const KEYEVENTF_KEYUP = &H2Private Const KEYEVENTF_EXTENDEDKEY = &H1
Public Sub SendKeysAPI_HOME_END(frmCur As String)‘下面是发送 组合 键码:Call keybd_event(VK_HOME, MapVirtualKey(VK_HOME, 0), KEYEVENTF_EXTENDEDKEY Or 0, 0)Call keybd_event(VK_LSHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_EXTENDEDKEY Or 0, 0)Call keybd_event(VK_END, MapVirtualKey(VK_END, 0), KEYEVENTF_EXTENDEDKEY Or 0, 0)Call keybd_event(VK_END, MapVirtualKey(VK_END, 0), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)Call keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)Call keybd_event(VK_HOME, MapVirtualKey(VK_HOME, 0), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)End Sub
也就是与上KEYEVENTF_EXTENDEDKEY(扩展键)的标识就行了,至于原因,我猜某些特殊键,例如shift等得加吧。
关于 keybd_event (vb篇)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。