VB如何用WinIO打字?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB如何用WinIO打字?相关的知识,希望对你有一定的参考价值。
如题,成功了我追加悬赏!!!
先用里面的InitializeWinIo函数安装驱动程序,然后就可以用GetPortVal来读取端口或者用SetPortVal来写入端口了。好,让我们来做一个驱动级的键盘模拟吧。先把winio的3个文件拷贝到你的程序的文件夹下,然后在VB中新建一个工程,添加一个模块,在模块中加入下面的winio函数声明:Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
' ------------------------------------以上是WINIO函数声明-------------------------------------------
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
'-----------------------------------以上是WIN32 API函数声明-----------------------------------------
再添加下面这个过程:
Sub KBCWait4IBE() '等待键盘缓冲区为空
Dim dwVal As Long
Do
GetPortVal &H64, dwVal, 1
'这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中
'GetPortVal函数的用法是GetPortVal 端口号,存放读出数据的变量,读入的长度
Loop While (dwVal And &H2)
End Sub
上面的是一个根据KBC规范写的过程,它的作用是在向键盘端口写入数据前等待一段时间,后面将会用到。
然后再添加如下过程,这2个过程用来模拟按键:
Public Const KBC_KEY_CMD = &H64 '键盘命令端口
Public Const KBC_KEY_DATA = &H60 '键盘数据端口
Sub MyKeyDown(ByVal vKeyCoad As Long)
'这个用来模拟按下键,参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '发送数据前应该先等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
'SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度
KBCWait4IBE
SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键
End Sub
Sub MyKeyUp(ByVal vKeyCoad As Long)
'这个用来模拟释放键,参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键
End Sub
定义了上面的过程后,就可以用它来模拟键盘输入了。在窗体模块中添加一个定时器控件,然后加入以下代码:
Private Sub Form_Load()
If InitializeWinIo = False Then
'用InitializeWinIo函数加载驱动程序,如果成功会返回true,否则返回false
MsgBox "驱动程序加载失败!"
Unload Me
End If
Timer1.Interval = 3000
Timer1.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShutdownWinIo '程序结束时记得用ShutdownWinIo函数卸载驱动程序
End Sub
Private Sub Timer1_Timer()
Dim VK_A As Long = &H41
MyKeyDown VK_A
MyKeyUp VK_A '模拟按下并释放A键
End Sub
运行上面的程序,就会每隔3秒钟模拟按下一次A键,试试看,怎么样,是不是对所有程序都有效果了?
需要注意的问题:
要在VB的调试模式下使用WINIO,需要把那3个文件拷贝到VB的安装目录中。
键盘上有些键属于扩展键(比如键盘上的方向键就是扩展键),对于扩展键不应该用上面的MyKeyDown和MyKeyUp过程来模拟,可以使用下面的2个过程来准确模拟扩展键:
Sub MyKeyDownEx(ByVal vKeyCoad As Long) '模拟扩展键按下,参数vKeyCoad是扩展键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键
End Sub
Sub MyKeyUpEx(ByVal vKeyCoad As Long) '模拟扩展键弹起
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键
End Sub
还应该注意的是,如果要从扩展键转换到普通键,那么普通键的KeyDown事件应该发送两次。也就是说,如果我想模拟先按下一个扩展键,再按下一个普通键,那么就应该向端口发送两次该普通键被按下的信息。比如,我想模拟先按下左方向键,再按下空格键这个事件,由于左方向键是扩展键,空格键是普通键,那么流程就应该是这样的:
MyKeyDownEx VK_LEFT '按下左方向键
Sleep 200 '延时200毫秒
MyKeyUpEx VK_LEFT '释放左方向键
Sleep 500
MyKeyDown VK_SPACE '按下空格键,注意要发送两次
MyKeyDown VK_SPACE
Sleep 200
MyKeyUp VK_SPACE '释放空格键
追问:
那怎么模拟打汉字?
回答:
这个只能调用输入法,然后模拟键盘输入了,要输入什么都要在程序里定义好。这样还不如别这么麻烦,设定好要输入的中文后,发送到剪切板,然后复制出来更简单。
追问:
对啊,你真聪明,那Ctrl+V的VK_**是多少?
回答:
虚拟按键的总结
符号常量 十六进制值 指定的鼠标或键盘按键
VK_LBUTTON 01 鼠标左键
VK_RBUTTON 02 鼠标右键
VK_CANCEL 03 Control-break 过程
VK_MBUTTON 04 鼠标中键
VK_BACK 08 BACKSPACE 键
VK_TAB 09 TAB 键
VK_CLEAR 0C CLEAR 键
VK_RETURN 0D ENTER 键
VK_SHIFT 10 SHIFT 键
VK_CONTROL 11 CTRL 键
VK_MENU 12 ALT 键
VK_PAUSE 13 PAUSE 键
VK_CAPITAL 14 CAPS LOCK 键
VK_ESCAPE 1B ESC 键
VK_SPACE 20 SPACEBAR
VK_PRIOR 21 PAGE UP 键
VK_NEXT 22 PAGE DOWN 键
VK_END 23 END 键
VK_HOME 24 HOME 键
VK_LEFT 25 LEFT ARROW 键
VK_UP 26 UP ARROW 键
VK_RIGHT 27 RIGHT ARROW 键
VK_DOWN 28 DOWN ARROW 键
VK_SELECT 29 SELECT 键
VK_EXECUTE 2B EXECUTE 键
VK_SNAPSHOT 2C PRINT SCREEN键(用于Windows 3.0及以后版本)
VK_INSERT 2D INS 键
VK_DELETE 2E DEL 键
VK_HELP 2F HELP键
/////////////////////
对于非小键盘上的数字键和字母键,直接在单引号中加入该键就行.
比如: a 键: 'A'
1 键: '1'
/////////////////////////////////
VK_LWIN 5B Left Windows 键 (Microsoft自然键盘)
VK_RWIN 5C Right Windows 键 (Microsoft自然键盘)
VK_APPS 5D Applications 键 (Microsoft自然键盘)
VK_NUMPAD0 60 数字小键盘上的 0 键
VK_NUMPAD1 61 数字小键盘上的 1 键
VK_NUMPAD2 62 数字小键盘上的 2 键
VK_NUMPAD3 63 数字小键盘上的 3 键
VK_NUMPAD4 64 数字小键盘上的 4 键
VK_NUMPAD5 65 数字小键盘上的 5 键
VK_NUMPAD6 66 数字小键盘上的 6 键
VK_NUMPAD7 67 数字小键盘上的 7 键
VK_NUMPAD8 68 数字小键盘上的 8 键
VK_NUMPAD9 69 数字小键盘上的 9 键
VK_MULTIPLY 6A Multiply 键
VK_ADD 6B Add键
VK_SEPARATOR 6C Separator 键
VK_SUBTRACT 6D Subtract 键
VK_DECIMAL 6E Decimal 键
VK_DIVIDE 6F Divide 键
VK_F1 70 F1 键
VK_F2 71 F2 键
VK_F3 72 F3 键
VK_F4 73 F4 键
VK_F5 74 F5 键
VK_F6 75 F6 键
VK_F7 76 F7 键
VK_F8 77 F8 键
VK_F9 78 F9 键
VK_F10 79 F10 键
VK_F11 7A F11 键
VK_F12 7B F12 键
VK_F13 7C F13 键
VK_F14 7D F14 键
VK_F15 7E F15 键
VK_F16 7F F16 键
VK_F17 80H F17 键
VK_F18 81H F18 键
VK_F19 82H F19 键
VK_F20 83H F20 键
VK_F21 84H F21 键
VK_F22 85H F22 键
VK_F23 86H F23 键
VK_F24 87H F24 键
VK_NUMLOCK 90 NUM LOCK 键
VK_SCROLL 91 SCROLL LOCK 键
VK_ATTN F6 Attn 键
VK_CRSEL F7 CrSel 键
VK_EXSEL F8 ExSel 键
VK_EREOF F9 Erase EOF 键
VK_PLAY FA Play 键
VK_ZOOM FB Zoom 键
VK_OEM_CLEAR FE Clear 键 参考技术A 先用里面的InitializeWinIo函数安装驱动程序,然后就可以用GetPortVal来读取端口或者用SetPortVal来写入端口了。好,让我们来做一个驱动级的键盘模拟吧。先把winio的3个文件拷贝到你的程序的文件夹下,然后在VB中新建一个工程,添加一个模块,在模块中加入下面的winio函数声明: Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean ' ------------------------------------以上是WINIO函数声明------------------------------------------- Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long '-----------------------------------以上是WIN32 API函数声明----------------------------------------- 再添加下面这个过程: Sub KBCWait4IBE() '等待键盘缓冲区为空 Dim dwVal As Long Do GetPortVal &H64, dwVal, 1 '这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中 'GetPortVal函数的用法是GetPortVal 端口号,存放读出数据的变量,读入的长度 Loop While (dwVal And &H2) End Sub 上面的是一个根据KBC规范写的过程,它的作用是在向键盘端口写入数据前等待一段时间,后面将会用到。 然后再添加如下过程,这2个过程用来模拟按键: Public Const KBC_KEY_CMD = &H64 '键盘命令端口 Public Const KBC_KEY_DATA = &H60 '键盘数据端口 Sub MyKeyDown(ByVal vKeyCoad As Long) '这个用来模拟按下键,参数vKeyCoad传入按键的虚拟码 Dim btScancode As Long btScancode = MapVirtualKey(vKeyCoad, 0) KBCWait4IBE '发送数据前应该先等待键盘缓冲区为空 SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令 'SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度 KBCWait4IBE SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键 End Sub Sub MyKeyUp(ByVal vKeyCoad As Long) '这个用来模拟释放键,参数vKeyCoad传入按键的虚拟码 Dim btScancode As Long btScancode = MapVirtualKey(vKeyCoad, 0) KBCWait4IBE '等待键盘缓冲区为空 SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令 KBCWait4IBE SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键 End Sub 定义了上面的过程后,就可以用它来模拟键盘输入了。在窗体模块中添加一个定时器控件,然后加入以下代码: Private Sub Form_Load() If InitializeWinIo = False Then '用InitializeWinIo函数加载驱动程序,如果成功会返回true,否则返回false MsgBox "驱动程序加载失败!" Unload Me End If Timer1.Interval = 3000 Timer1.Enabled = True End Sub Private Sub Form_Unload(Cancel As Integer) ShutdownWinIo '程序结束时记得用ShutdownWinIo函数卸载驱动程序 End Sub Private Sub Timer1_Timer() Dim VK_A As Long = &H41 MyKeyDown VK_A MyKeyUp VK_A '模拟按下并释放A键 End Sub 运行上面的程序,就会每隔3秒钟模拟按下一次A键,试试看,怎么样,是不是对所有程序都有效果了? 需要注意的问题: 要在VB的调试模式下使用WINIO,需要把那3个文件拷贝到VB的安装目录中。 键盘上有些键属于扩展键(比如键盘上的方向键就是扩展键),对于扩展键不应该用上面的MyKeyDown和MyKeyUp过程来模拟,可以使用下面的2个过程来准确模拟扩展键: Sub MyKeyDownEx(ByVal vKeyCoad As Long) '模拟扩展键按下,参数vKeyCoad是扩展键的虚拟码 Dim btScancode As Long btScancode = MapVirtualKey(vKeyCoad, 0) KBCWait4IBE '等待键盘缓冲区为空 SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令 KBCWait4IBE SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息 KBCWait4IBE '等待键盘缓冲区为空 SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令 KBCWait4IBE SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键 End Sub Sub MyKeyUpEx(ByVal vKeyCoad As Long) '模拟扩展键弹起 Dim btScancode As Long btScancode = MapVirtualKey(vKeyCoad, 0) KBCWait4IBE '等待键盘缓冲区为空 SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令 KBCWait4IBE SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息 KBCWait4IBE '等待键盘缓冲区为空 SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令 KBCWait4IBE SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键 End Sub 还应该注意的是,如果要从扩展键转换到普通键,那么普通键的KeyDown事件应该发送两次。也就是说,如果我想模拟先按下一个扩展键,再按下一个普通键,那么就应该向端口发送两次该普通键被按下的信息。比如,我想模拟先按下左方向键,再按下空格键这个事件,由于左方向键是扩展键,空格键是普通键,那么流程就应该是这样的: MyKeyDownEx VK_LEFT '按下左方向键 Sleep 200 '延时200毫秒 MyKeyUpEx VK_LEFT '释放左方向键 Sleep 500 MyKeyDown VK_SPACE '按下空格键,注意要发送两次 MyKeyDown VK_SPACE Sleep 200 MyKeyUp VK_SPACE '释放空格键 参考技术B 1 '写入按键信息;WinIo,它的作用是在向键盘端口写入数据前等待一段时间, (btScancode Or &H80), 0)
KBCWait4IBE ',添加一个模块,注意要发送两次
MyKeyDown VK_SPACE
Sleep 200
MyKeyUp VK_SPACE ', &HE0;发送键盘写入命令
'.dll", dwVal;发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA,然后在VB中新建一个工程;按下空格键,我想模拟先按下左方向键;WinIo。在窗体模块中添加一个定时器控件.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShutdownWinIo ',欲写入的数据;WinIo.dll"。
键盘上有些键属于扩展键(比如键盘上的方向键就是扩展键), 1 ',试试看:
要在VB的调试模式下使用WINIO.dll",让我们来做一个驱动级的键盘模拟吧;写入扩展键标志信息
KBCWait4IBE '模拟扩展键弹起
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad.dll", ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib ", ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib ",后面将会用到; (ByVal PortAddr As Integer,按下键
End Sub
Sub MyKeyUpEx(ByVal vKeyCoad As Long) '程序结束时记得用ShutdownWinIo函数卸载驱动程序
End Sub
Private Sub Timer1_Timer()
Dim VK_A As Long = &H41
MyKeyDown VK_A
MyKeyUp VK_A '释放左方向键
Sleep 500
MyKeyDown VK_SPACE 'WinIo;模拟按下并释放A键
End Sub
运行上面的程序.Interval = 3000
Timer1,就可以用它来模拟键盘输入了, &HE0;等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD。也就是说, 1 ', ByRef PortVal As Long, ByVal PortVal As Long; (ByVal PhysMemHandle, 1
',参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad;发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib ", btScancode, &HD2;用InitializeWinIo函数加载驱动程序;写入按键信息,如果要从扩展键转换到普通键;user32",需要把那3个文件拷贝到VB的安装目录中, 1 ', ByVal wMapType As Long) As Long
',空格键是普通键;写入按键信息,对于扩展键不应该用上面的MyKeyDown和MyKeyUp过程来模拟,读入的长度
Loop While (dwVal And &H2)
End Sub
上面的是一个根据KBC规范写的过程,存放读出数据的变量;发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, 0)
KBCWait4IBE 'WinIo:
Public Const KBC_KEY_CMD = &H64 '?
需要注意的问题。先把winio的3个文件拷贝到你的程序的文件夹下, 0)
KBCWait4IBE ' ------------------------------------以上是WINIO函数声明-------------------------------------------
Declare Function MapVirtualKey Lib ",参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad;这个用来模拟按下键, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "发送数据前应该先等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD.dll"WinIo, &HD2,写入数据的长度
KBCWait4IBE
SetPortVal KBC_KEY_DATA;延时200毫秒
MyKeyUpEx VK_LEFT ', ByVal PhysSize As Long, 1 '。好;发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA,如果成功会返回true;写入扩展键标志信息
KBCWait4IBE ', 0)
KBCWait4IBE '按下左方向键
Sleep 200 ',释放键
End Sub
还应该注意的是,是不是对所有程序都有效果了, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib ",那么普通键的KeyDown事件应该发送两次.dll",再按下一个普通键, (btScancode Or &H80),它的用法是SetPortVal 端口号:
Sub KBCWait4IBE() ':
Declare Function MapPhysToLin Lib "驱动程序加载失败; (ByVal wCode As Long;等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD:
Private Sub Form_Load()
If InitializeWinIo = False Then
':
MyKeyDownEx VK_LEFT ' (ByVal DriverPath As String;WinIo;
Unload Me
End If
Timer1.dll"GetPortVal函数的用法是GetPortVal 端口号,可以使用下面的2个过程来准确模拟扩展键:
Sub MyKeyDownEx(ByVal vKeyCoad As Long) ',然后加入以下代码, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib ", &HD2;这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中
'!",那么流程就应该是这样的, &HD2, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib ".dll"。
然后再添加如下过程,在模块中加入下面的winio函数声明,然后就可以用GetPortVal来读取端口或者用SetPortVal来写入端口了;写入按键信息;这个用来模拟释放键;SetPortVal函数用于向端口写入数据,由于左方向键是扩展键,如果我想模拟先按下一个扩展键;键盘数据端口
Sub MyKeyDown(ByVal vKeyCoad As Long)
',按下键
End Sub
Sub MyKeyUp(ByVal vKeyCoad As Long)
',再按下空格键这个事件;MapVirtualKeyA", 1 ', 1 ', 1 ' (ByVal PhysAddr As Long,就会每隔3秒钟模拟按下一次A键; (ByVal PortAddr As Integer, 1 ' () As Boolean
Declare Function InstallWinIoDriver Lib ", 1 ' Alias "等待键盘缓冲区为空
Dim dwVal As Long
Do
GetPortVal &H64;等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD。比如.dll",参数vKeyCoad是扩展键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad; (ByVal PhysAddr As Long,那么就应该向端口发送两次该普通键被按下的信息, 1 ', 1 ', &HD2, btScancode;模拟扩展键按下; () As Boolean
Declare Function ShutdownWinIo Lib "WinIo,这2个过程用来模拟按键,否则返回false
MsgBox "等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD;-----------------------------------以上是WIN32 API函数声明-----------------------------------------
再添加下面这个过程;发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HD2.dll"键盘命令端口
Public Const KBC_KEY_DATA = &H60 ',怎么样,释放键
End Sub
定义了上面的过程后;等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD; (ByVal PhysAddr As Long; () As Boolean
'WinIo;WinIo先用里面的InitializeWinIo函数安装驱动程序 参考技术C 先用里面的InitializeWinIo函数安装驱动程序,然后就可以用GetPortVal来读取端口或者用SetPortVal来写入端口了。好,让我们来做一个驱动级的键盘模拟吧。先把winio的3个文件拷贝到你的程序的文件夹下,然后在VB中新建一个工程,添加一个模块,在模块中加入下面的winio函数声明:
Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
' ------------------------------------以上是WINIO函数声明-------------------------------------------
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
'-----------------------------------以上是WIN32 API函数声明-----------------------------------------
再添加下面这个过程:
Sub KBCWait4IBE() '等待键盘缓冲区为空
Dim dwVal As Long
Do
GetPortVal &H64, dwVal, 1
'这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中
'GetPortVal函数的用法是GetPortVal 端口号,存放读出数据的变量,读入的长度
Loop While (dwVal And &H2)
End Sub
上面的是一个根据KBC规范写的过程,它的作用是在向键盘端口写入数据前等待一段时间,后面将会用到。
然后再添加如下过程,这2个过程用来模拟按键:
Public Const KBC_KEY_CMD = &H64 '键盘命令端口
Public Const KBC_KEY_DATA = &H60 '键盘数据端口
Sub MyKeyDown(ByVal vKeyCoad As Long)
'这个用来模拟按下键,参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '发送数据前应该先等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
'SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度
KBCWait4IBE
SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键
End Sub
Sub MyKeyUp(ByVal vKeyCoad As Long)
'这个用来模拟释放键,参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键
End Sub
定义了上面的过程后,就可以用它来模拟键盘输入了。在窗体模块中添加一个定时器控件,然后加入以下代码:
Private Sub Form_Load()
If InitializeWinIo = False Then
'用InitializeWinIo函数加载驱动程序,如果成功会返回true,否则返回false
MsgBox "驱动程序加载失败!"
Unload Me
End If
Timer1.Interval = 3000
Timer1.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShutdownWinIo '程序结束时记得用ShutdownWinIo函数卸载驱动程序
End Sub
Private Sub Timer1_Timer()
Dim VK_A As Long = &H41
MyKeyDown VK_A
MyKeyUp VK_A '模拟按下并释放A键
End Sub
运行上面的程序,就会每隔3秒钟模拟按下一次A键,试试看,怎么样,是不是对所有程序都有效果了?
需要注意的问题:
要在VB的调试模式下使用WINIO,需要把那3个文件拷贝到VB的安装目录中。
键盘上有些键属于扩展键(比如键盘上的方向键就是扩展键),对于扩展键不应该用上面的MyKeyDown和MyKeyUp过程来模拟,可以使用下面的2个过程来准确模拟扩展键:
Sub MyKeyDownEx(ByVal vKeyCoad As Long) '模拟扩展键按下,参数vKeyCoad是扩展键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键
End Sub
Sub MyKeyUpEx(ByVal vKeyCoad As Long) '模拟扩展键弹起
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键
End Sub
还应该注意的是,如果要从扩展键转换到普通键,那么普通键的KeyDown事件应该发送两次。也就是说,如果我想模拟先按下一个扩展键,再按下一个普通键,那么就应该向端口发送两次该普通键被按下的信息。比如,我想模拟先按下左方向键,再按下空格键这个事件,由于左方向键是扩展键,空格键是普通键,那么流程就应该是这样的:
MyKeyDownEx VK_LEFT '按下左方向键
Sleep 200 '延时200毫秒
MyKeyUpEx VK_LEFT '释放左方向键
Sleep 500
MyKeyDown VK_SPACE '按下空格键,注意要发送两次
MyKeyDown VK_SPACE
Sleep 200
MyKeyUp VK_SPACE '释放空格键 参考技术D 先用里面的InitializeWinIo函数安装驱动程序,然后就可以用GetPortVal来读取端口或者用SetPortVal来写入端口了。好,让我们来做一个驱动级的键盘模拟吧。先把winio的3个文件拷贝到你的程序的文件夹下,然后在VB中新建一个工程,添加一个模块,在模块中加入下面的winio函数声明:
Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
' ------------------------------------以上是WINIO函数声明-------------------------------------------
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
'-----------------------------------以上是WIN32 API函数声明-----------------------------------------
再添加下面这个过程:
Sub KBCWait4IBE() '等待键盘缓冲区为空
Dim dwVal As Long
Do
GetPortVal &H64, dwVal, 1
'这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中
'GetPortVal函数的用法是GetPortVal 端口号,存放读出数据的变量,读入的长度
Loop While (dwVal And &H2)
End Sub
上面的是一个根据KBC规范写的过程,它的作用是在向键盘端口写入数据前等待一段时间,后面将会用到。
然后再添加如下过程,这2个过程用来模拟按键:
Public Const KBC_KEY_CMD = &H64 '键盘命令端口
Public Const KBC_KEY_DATA = &H60 '键盘数据端口
Sub MyKeyDown(ByVal vKeyCoad As Long)
'这个用来模拟按下键,参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '发送数据前应该先等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
'SetPortVal函数用于向端口写入数据,它的用法是SetPortVal 端口号,欲写入的数据,写入数据的长度
KBCWait4IBE
SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键
End Sub
Sub MyKeyUp(ByVal vKeyCoad As Long)
'这个用来模拟释放键,参数vKeyCoad传入按键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键
End Sub
定义了上面的过程后,就可以用它来模拟键盘输入了。在窗体模块中添加一个定时器控件,然后加入以下代码:
Private Sub Form_Load()
If InitializeWinIo = False Then
'用InitializeWinIo函数加载驱动程序,如果成功会返回true,否则返回false
MsgBox "驱动程序加载失败!"
Unload Me
End If
Timer1.Interval = 3000
Timer1.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShutdownWinIo '程序结束时记得用ShutdownWinIo函数卸载驱动程序
End Sub
Private Sub Timer1_Timer()
Dim VK_A As Long = &H41
MyKeyDown VK_A
MyKeyUp VK_A '模拟按下并释放A键
End Sub
运行上面的程序,就会每隔3秒钟模拟按下一次A键,试试看,怎么样,是不是对所有程序都有效果了?
需要注意的问题:
要在VB的调试模式下使用WINIO,需要把那3个文件拷贝到VB的安装目录中。
键盘上有些键属于扩展键(比如键盘上的方向键就是扩展键),对于扩展键不应该用上面的MyKeyDown和MyKeyUp过程来模拟,可以使用下面的2个过程来准确模拟扩展键:
Sub MyKeyDownEx(ByVal vKeyCoad As Long) '模拟扩展键按下,参数vKeyCoad是扩展键的虚拟码
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, btScancode, 1 '写入按键信息,按下键
End Sub
Sub MyKeyUpEx(ByVal vKeyCoad As Long) '模拟扩展键弹起
Dim btScancode As Long
btScancode = MapVirtualKey(vKeyCoad, 0)
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, &HE0, 1 '写入扩展键标志信息
KBCWait4IBE '等待键盘缓冲区为空
SetPortVal KBC_KEY_CMD, &HD2, 1 '发送键盘写入命令
KBCWait4IBE
SetPortVal KBC_KEY_DATA, (btScancode Or &H80), 1 '写入按键信息,释放键
End Sub
还应该注意的是,如果要从扩展键转换到普通键,那么普通键的KeyDown事件应该发送两次。也就是说,如果我想模拟先按下一个扩展键,再按下一个普通键,那么就应该向端口发送两次该普通键被按下的信息。比如,我想模拟先按下左方向键,再按下空格键这个事件,由于左方向键是扩展键,空格键是普通键,那么流程就应该是这样的:
MyKeyDownEx VK_LEFT '按下左方向键
Sleep 200 '延时200毫秒
MyKeyUpEx VK_LEFT '释放左方向键
Sleep 500
MyKeyDown VK_SPACE '按下空格键,注意要发送两次
MyKeyDown VK_SPACE
Sleep 200
MyKeyUp VK_SPACE '释放空格键
以上是关于VB如何用WinIO打字?的主要内容,如果未能解决你的问题,请参考以下文章