关于 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 Long

Private 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 = &H11
Private Const VK_DECIMAL As Long = &H6E      ‘小数点号键
Private Const VK_DELETE As Long = &H2E
Private Const VK_DIVIDE As Long = &H6F       ‘除号键
Private Const VK_DOWN As Long = &H28
Private Const VK_END As Long = &H23
Private Const VK_ESCAPE As Long = &H1B
Private Const VK_F1 As Long = &H70
Private Const VK_F10 As Long = &H79
Private Const VK_F11 As Long = &H7A
Private Const VK_F12 As Long = &H7B
Private Const VK_F2 As Long = &H71
Private Const VK_F3 As Long = &H72
Private Const VK_F4 As Long = &H73
Private Const VK_F5 As Long = &H74
Private Const VK_F6 As Long = &H75
Private Const VK_F7 As Long = &H76
Private Const VK_F8 As Long = &H77
Private Const VK_F9 As Long = &H78
Private Const VK_HOME As Long = &H24
Private Const VK_INSERT As Long = &H2D
Private Const VK_LCONTROL As Long = &HA2
Private Const VK_LEFT As Long = &H25
Private Const VK_LMENU As Long = &HA4        ‘左 Alt 键
Private Const VK_LSHIFT As Long = &HA0
Private Const VK_LWIN As Long = &H5B
Private 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 = &H90
Private Const VK_NUMPAD0 As Long = &H60
Private Const VK_NUMPAD1 As Long = &H61
Private Const VK_NUMPAD2 As Long = &H62
Private Const VK_NUMPAD3 As Long = &H63
Private Const VK_NUMPAD4 As Long = &H64
Private Const VK_NUMPAD5 As Long = &H65
Private Const VK_NUMPAD6 As Long = &H66
Private Const VK_NUMPAD7 As Long = &H67
Private Const VK_NUMPAD8 As Long = &H68
Private Const VK_NUMPAD9 As Long = &H69
Private Const VK_PAUSE As Long = &H13
Private Const VK_PRINT As Long = &H2A
Private Const VK_PRIOR As Long = &H21        ‘上翻页键
Private Const VK_RCONTROL As Long = &HA3
Private Const VK_RETURN As Long = &HD        ‘回车键
Private Const VK_RIGHT As Long = &H27
Private Const VK_RMENU As Long = &HA5        ‘右 Alt 键
Private Const VK_RSHIFT As Long = &HA1
Private Const VK_RWIN As Long = &H5C
Private Const VK_SCROLL As Long = &H91       ‘Scroll Lock 键
Private Const VK_SEPARATOR As Long = &H6C    ‘小键盘上的回车键
Private Const VK_SHIFT As Long = &H10
Private Const VK_SLEEP As Long = &H5F        ‘休眠键
Private Const VK_SNAPSHOT As Long = &H2C     ‘Print Screen 键
Private Const VK_SPACE As Long = &H20
Private Const VK_SUBTRACT As Long = &H6D     ‘减号键
Private Const VK_TAB As Long = &H9
Private Const VK_UP As Long = &H26
Private 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 Long

Private 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 = &H11
Private Const VK_DECIMAL As Long = &H6E      ‘小数点号键
Private Const VK_DELETE As Long = &H2E
Private Const VK_DIVIDE As Long = &H6F       ‘除号键
Private Const VK_DOWN As Long = &H28
Private Const VK_END As Long = &H23
Private Const VK_ESCAPE As Long = &H1B
Private Const VK_F1 As Long = &H70
Private Const VK_F10 As Long = &H79
Private Const VK_F11 As Long = &H7A
Private Const VK_F12 As Long = &H7B
Private Const VK_F2 As Long = &H71
Private Const VK_F3 As Long = &H72
Private Const VK_F4 As Long = &H73
Private Const VK_F5 As Long = &H74
Private Const VK_F6 As Long = &H75
Private Const VK_F7 As Long = &H76
Private Const VK_F8 As Long = &H77
Private Const VK_F9 As Long = &H78
Private Const VK_HOME As Long = &H24
Private Const VK_INSERT As Long = &H2D
Private Const VK_LCONTROL As Long = &HA2
Private Const VK_LEFT As Long = &H25
Private Const VK_LMENU As Long = &HA4        ‘左 Alt 键
Private Const VK_LSHIFT As Long = &HA0
Private Const VK_LWIN As Long = &H5B
Private 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 = &H90
Private Const VK_NUMPAD0 As Long = &H60
Private Const VK_NUMPAD1 As Long = &H61
Private Const VK_NUMPAD2 As Long = &H62
Private Const VK_NUMPAD3 As Long = &H63
Private Const VK_NUMPAD4 As Long = &H64
Private Const VK_NUMPAD5 As Long = &H65
Private Const VK_NUMPAD6 As Long = &H66
Private Const VK_NUMPAD7 As Long = &H67
Private Const VK_NUMPAD8 As Long = &H68
Private Const VK_NUMPAD9 As Long = &H69
Private Const VK_PAUSE As Long = &H13
Private Const VK_PRINT As Long = &H2A
Private Const VK_PRIOR As Long = &H21        ‘上翻页键
Private Const VK_RCONTROL As Long = &HA3
Private Const VK_RETURN As Long = &HD        ‘回车键
Private Const VK_RIGHT As Long = &H27
Private Const VK_RMENU As Long = &HA5        ‘右 Alt 键
Private Const VK_RSHIFT As Long = &HA1
Private Const VK_RWIN As Long = &H5C
Private Const VK_SCROLL As Long = &H91       ‘Scroll Lock 键
Private Const VK_SEPARATOR As Long = &H6C    ‘小键盘上的回车键
Private Const VK_SHIFT As Long = &H10
Private Const VK_SLEEP As Long = &H5F        ‘休眠键
Private Const VK_SNAPSHOT As Long = &H2C     ‘Print Screen 键
Private Const VK_SPACE As Long = &H20
Private Const VK_SUBTRACT As Long = &H6D     ‘减号键
Private Const VK_TAB As Long = &H9
Private Const VK_UP As Long = &H26
Private 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
Private 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等得加吧。

时间: 2024-10-13 11:57:26

关于 keybd_event (vb篇)的相关文章

诗经 全文

诗经 全文 (带注释和译文) http://www.edu009.com/Article/HTML/Article_60756.html <诗经> 春秋·孔丘 <诗经>是我国第一部诗歌总集,先秦时代称为“诗”或“诗三百”,孔子加以了整理.汉武帝采纳董仲舒“罢黜百家,独尊儒术”的建议,尊“诗”为经典,定名为<诗经>. <诗经>现存诗歌 305 篇,包括西周初年到春秋中叶共 500 余年的民歌和朝庙乐章,分为风.雅.颂三章. “风”包括周南.召南.邶.鄘.卫.王

vb.net版机房收费系统重构的成长历程(功能实现篇)

重构机房收费系统,功能是不变的,需求是不变的,系统背后的代码,架构发生了改变.做过vb6.0版的系统 后,利用VS的VB.NET平台, 通过调整程序架构,加入了设计模式,改善软件的质量.性能,提高软件的扩展性和维护性.相比旧版系统,新版系统主要亮点有: 亮点一:代码分层管理,清晰明了 亮点二:加入工厂和接口,代码复用较多,减少了重复或相似的代码. 亮点三:用到了存储过程.一方面减少了代码量,另一方面也相当于是一个事务的执行.比如注册或者结账需要同 时操作多张表,这几个表必须同时被操作成功后才可以

第一篇随笔:用VB.NET搞点简单事情(1)

网络上能搜索到的爬虫文章大多是用python做的,也有少部分是C#做的(小声:所以用VB.NET也可以做爬虫.本文写的是第一步:获取网页) 使用代码前先imports以下内容 Imports System.IO, System.IO.Compression, System.Text, System.Net 写程序前先开浏览器(我用的Chrome),随便上个网页,F12看下header,粘下来useragent备用,也可以粘下accept,cookie等(在本文中用不到 用httpwebreque

三层登录实例VB.NET版具体解释---理论加实战篇

层,百度百科这样解释,首先-重叠起来的东西:重叠起来的东西中的一部分:层次|表层|大气层.其次-重叠.反复:层峦叠嶂|层出不穷.最后-量词,用于能够分出层次的事物.女孩儿强烈的第六感,三层中的层一定是第三个意思,三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据訪问层(DAL).区分层次的目的即为了"高内聚,低耦合"的思想. 接下来,以登录为例.结合分层,总结一下在这个小样例中的所思所想.首先,来

VB API 第四课 字体应用篇之二

首先来四个API函数,分别是DeltetObject,CreateFontIndirect,SelectOBject,TextOut.先分别对这几个函数的说明做下介绍. DeltetObject 函数功能:该函数删除一个逻辑笔.画笔.字体.位图.区域或者调色板,释放所有与该对象有关的系统资源,在对象被删除之后,指定的句柄也就失效了. 函数原型:BOOL DeleteObject(HGDIOBJ hObject): 参数: hObject:逻辑笔.画笔.字体.位图.区域或者调色板的句柄. 返回值:

第十篇 Integration Services:高级事件行为

本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining methods of task execution state management using the MaximumErrorCount and ForceExecutionResult properties.我们还学习了SSIS控制流任务错误.事件处理程序和容器之间的关系.这一篇我们重点关注事件的行为.在

第九篇 Integration Services:控制流任务错误

本篇文章是Integration Services系列的第九篇,详细内容请参考原文. 简介在前面三篇文章,我们创建了一个新的SSIS包,学习了脚本任务和优先约束,并检查包的MaxConcurrentExecutables属性.我们检查.演示并测试优先约束赋值为"成功"."完成"."失败"时对工作流的影响.我们学习了SSIS变量和表达式,并将它们应用到优先约束.这一篇,we introduce fault tolerance by examinin

VB已死?还是会在Roslyn之下焕发新生?

(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 由于最初的ASP.NET 5测试版并未支持VB,导致社区有一种声音:觉得VB将死.今天我们就来简单的讨论一下这个有意思的话题. 我入门计算机编程就是初三的时候从一本GWBasic教程开始的,后来学习QB,VB.大学毕业前去考的MCSD也是VB语言.虽然在.NET平台上一开始就是以C#为主,其中只是把VB6移植到.NET上用过VB.NET.不过对VB还是有感情的,对VB也一直比较关注.今天InfoQ中

【VB.NET】全局观概括

视频总算是看完了,但是收获不多.总的来说,基础还是VB,只不过增加了面向对象的技术.看完之后,感觉自己能力有限,不能理解老师讲的内容.所以,觉得不能只在理论上学习了,必须在实践中寻找答案!这篇博客是一篇概括性文章. OOP基础 面向对象是现代软件开发的基本要素.VB通过.NET技术完整地支持了面向对象的功能. BCL是微软所提出的一组标准库可提供.NET Framework所有语言使用.BCL相当于VB中的API函数. 新一代Win 无论在任何窗体平台上开发GUI应用程序,它都是通过事件与使用者