我用VB编了一个模拟按键的程序,其实是在网上找的代码,那段代码是在驱动的级别上模拟按键的。
用了几次后,键盘上的数字键1键2键3键(大键盘的,其他的数字也有时候也不好按)不能按了,字母
键都没问题。我把键盘插到别的机器上,键盘可以正常使用,然后把别的键盘插在我的机器上,别的键盘
也可以使用。那么不是键盘问题,也不是接口问题。
当我按不动1 2 3的时候,我按下4键或其他的大键盘的数字键,就又可以按1 2 3 了。
请问下各位,这是怎么回事啊,驱动也没什么问题了,因为已经重装系统啦。
又或者是这个键盘不配这个主板了??
附上这个程序的代码:
Private Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Private Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Private Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Private Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Private Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Private Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Private Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Private Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Private Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Private Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
' ------------------------------------以上是WINIO函数声明-------------------------------------------
Private Const KBC_KEY_CMD = &H64 '键盘命令端口
Private Const KBC_KEY_DATA = &H60 '键盘数据端口
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Public VK_A 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
Private Sub Command1_Click()
Timer1.Interval = 1000
Timer1.Enabled = True
Command1.Enabled = False
Command2.Enabled = True
Label1.ForeColor = &H80FF&
Label1.FontSize = 12
Label1.Caption = "模拟按键开始。。。"
End Sub
Private Sub Command2_Click()
Command2.Enabled = False
Command1.Enabled = True
Timer1.Enabled = False
Label1.ForeColor = &HFF&
Label1.Caption = "模拟按键结束。。。"
Text1.Enabled = True
Text1.Text = ""
End Sub
Private Sub Command3_Click()
Dim a
Dim b
Dim i As Integer
a = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
b = Array("&H41", "&H42", "&H43", "&H44", "&H45", "&H46", "&H47", "&H48", "&H49", "&H4A", "&H4B", "&H4C", "&H4D", "&H4E", "&H4F", "&H50", "&H51", "&H52", "&H53", "&H54", "&H55", "&H56", "&H57", "&H58", "&H59", "&H5a", "&H60", "&H61", "&H62", "&H63", "&H64", "&H65", "&H66", "&H67", "&H68", "&H69")
For i = 0 To 35 Step 1
If Text1.Text = a(i) Then
VK_A = b(i)
End If
Next i
Text1.Enabled = False
End Sub
Private Sub Form_Load()
Command2.Enabled = False
If InitializeWinIo = False Then
'用InitializeWinIo函数加载驱动程序,如果成功会返回true,否则返回false
MsgBox "驱动程序加载失败!"
Unload Me
End If
Timer1.Enabled = False
End Sub
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_Unload(Cancel As Integer)
ShutdownWinIo '程序结束时记得用ShutdownWinIo函数卸载驱动程序
End Sub
Private Sub Timer1_Timer()
MyKeyDown VK_A
MyKeyUp VK_A
End Sub
这个是工程和可执行文件
[此贴子已经被作者于2007-10-5 0:03:50编辑过]