大家好, 本来我已再VB区发贴提问,但可能这里的朋友对硬件更熟悉, 故请教大家一下.
简单来说,我有一个小程序,在台式机上对64和60端口的操作,实现模拟PS/2鼠标的动作, 但同一程序放在笔记本电脑上使用却出问题了:
由于需要写一个3D游戏的自动操作脚本,首先使用了AutoHotKey, 但发现无论是sendEvent还是SendInput,鼠标能准确移动到指定位置, 但点击无效(即使自己亲手按下鼠标). 这是只需要把鼠标移动一点,就正常了.
因此,首先使用了WinIO写了一个小程序, 其实就是把鼠标往上移动1象素然后移回下面.
该程序在使用PS2鼠标的台式机上正常作用, 但一放到笔记本电脑上就不正常了:不是不动作,就是往上移动后往右移动(而不是往下)
我把笔记本电脑的USB鼠标拔掉也不行. 我觉得奇怪就是,笔记本电脑的触摸板/指点杆应该相当于PS2鼠标,为何就有这样的问题呢?
在"设备管理器"里面,看到"鼠标和其他指针设备"有"PS/2 compatible mouse", 驱动程序也是i8042prt.sys和mouclass.sys, 连版本也是和台式机一模一样.
请问大家有没有类似的经历,如何解决?
以下是程序代码,谢谢! (在台式机上运行,鼠标会往上然后往下移动一小格)
'模块1==============================================================
Option Explicit
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函数声明-----------------------------------------
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Const KBC_KEY_CMD = &H64 '键盘命令端口
Public Const KBC_KEY_DATA = &H60 '键盘数据端口
Public Sub KBCWait4IBE() '等待键盘缓冲区为空
Dim dwVal As Long
Do
GetPortVal &H64, dwVal, 1
'这句表示从&H64端口读取一个字节并把读出的数据放到变量dwVal中
'GetPortVal函数的用法是GetPortVal 端口号,存放读出数据的变量,读入的长度
Loop While (dwVal And &H2)
End Sub
'Form1=================================================================================
Sub MyMouseKey(MouseFun As Long, MyMouseX As Long, MyMouseY As Long, MyMouseZ As Long)
' 左键按下(MouseFun=9)。MyMouseX、MyMouseY、MyMouseZ 为0
' 右键按下(MouseFun=10)。MyMouseX、MyMouseY、MyMouseZ 为0
' 中键按下(MouseFun=12)。MyMouseX、MyMouseY、MyMouseZ 为0
' 任意键放开(MouseFun=8)。MyMouseX、MyMouseY、MyMouseZ 为0
' ------------------------------------
' 鼠标上移(MouseFun=8)。MyMouseY为移动距离,最大为255,最小为1。MyMouseX、MyMouseZ 为0
' 鼠标下移(MouseFun=40)。MyMouseY为移动距离,最大为1,最小为255。MyMouseX、MyMouseZ 为0
' 鼠标左移(MouseFun=24)。MyMouseX为移动距离,最大为1,最小为255。MyMouseY、MyMouseZ 为0
' 鼠标右移(MouseFun=8)。MyMouseX为移动距离,最大为255,最小为1。MyMouseY、MyMouseZ 为0
' ------------------------------------
KBCWait4IBE '等待缓冲区为空
SetPortVal 100, 211, 1 '发送鼠标写入命令
KBCWait4IBE '等待缓冲区为空
SetPortVal 96, MouseFun, 1 '发送鼠标动作命令
'-------------------------------------
KBCWait4IBE '等待缓冲区为空
SetPortVal 100, 211, 1 '发送鼠标写入命令
KBCWait4IBE '等待缓冲区为空
SetPortVal 96, MyMouseX, 1 '发送鼠标动作命令
'-------------------------------------
KBCWait4IBE '等待缓冲区为空
SetPortVal 100, 211, 1 '发送鼠标写入命令
KBCWait4IBE '等待缓冲区为空
SetPortVal 96, MyMouseY, 1 '发送鼠标动作命令
'-------------------------------------
KBCWait4IBE '等待缓冲区为空
SetPortVal 100, 211, 1 '发送鼠标写入命令
KBCWait4IBE '等待缓冲区为空
SetPortVal 96, MyMouseZ, 1 '发送鼠标动作命令
End Sub
Private Sub Command1_Click()
MyMouseKey 8, 0, 50, 0 '上移5象素
End Sub
Private Sub Form_Load()
If InitializeWinIo = False Then
'用InitializeWinIo函数加载驱动程序,如果成功会返回true,否则返回false
MsgBox "驱动程序加载失败!"
Unload Me
End If
MyMouseKey 8, 0, 1, 0 '上移-象素
Sleep 300
MyMouseKey 40, 0, (255 Xor 1), 0 '下移-象素
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShutdownWinIo '程序结束时记得用ShutdownWinIo函数卸载驱动程序
End Sub
'完毕============================================================================
笔记本的触摸板/指点杆比较特殊,与PS/2鼠标不同,具体情况我也不清楚。
你的代码没错,但有的鼠标是发两个命令,有的是发一个命令,还有的是发三个命令。。
就是在MouseFun=9 这里要调用两次,
同样 MouseFun=9 也要两次,
这里有点不同
具体情况 你的 得自己试试啦
如果你非常想要了解的话,请到google里找到那份 叫 ps/2 协议的pdf 中英对译的那份。。里面说得很清楚
底层的东西啦
这个,应该是触摸板和ps2,有区别了,建议看下ps2协议
如果没时间啃,只能如楼上所说,多加几个测试看看