[已解决] 用键盘模拟鼠标键,始终做不到完美
本帖最后由 fenghou 于 2012-6-19 21:15 编辑我想用键盘键模拟鼠标键,要做到键盘键按下鼠标就按下,键盘键弹起鼠标键就弹起。
一开始我想用HotKeySet实现,可惜这个函数判断按键时把按下和弹起混在一起了,没办法分别控制。
于是我只能用_IsPressed,如下,但这个函数的问题是键盘键和鼠标键的按下会同时发送到目标对象,而不是屏蔽掉键盘键。(不屏蔽掉的话非常不方便,比如全选一段文本时,那段文本会被键盘键覆盖掉。)
请问有什么方法能完美实现用键盘模拟鼠标键的功能?#include <Misc.au3>
Global $hDLL = DllOpen("user32.dll")
Global $sKeyMouseLeft = "47" ; G key
Local $bMouseLeftPressed = False
While True
Local $bKeyMouseLeftPressed = _IsPressed($sKeyMouseLeft, $hDLL)
If $bKeyMouseLeftPressed And Not $bMouseLeftPressed Then
MouseDown("left")
$bMouseLeftPressed = True
ElseIf Not $bKeyMouseLeftPressed And $bMouseLeftPressed Then
MouseUp("left")
$bMouseLeftPressed = False
EndIf
Sleep(100)
WEnd
Exit 用 func 来代替部分代码进行调整 本帖最后由 zerobin 于 2012-6-19 19:29 编辑
回复 2# lchl0588
我也试做了下。解决不了。您说的是怎样弄的?麻烦指点下
如:
按下键盘上的 w s a d 分别控制鼠标的 上 下 左 右 移动。
按下home键 对应鼠标左键点击
按下end键 对应鼠标右键点击#include <Misc.au3>
Local $dll = DllOpen("user32.dll")
While 1
Sleep(50)
If _IsPressed("57", $dll) Then;w
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("57", $dll)
MouseMove($mx, $my - $i)
$i += 10
WEnd
ElseIf _IsPressed("53", $dll) Then;s
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("53", $dll)
MouseMove($mx, $my + $i)
$i += 10
WEnd
ElseIf _IsPressed("41", $dll) Then;a
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("41", $dll)
MouseMove($mx - $i, $my)
$i += 10
WEnd
ElseIf _IsPressed("44", $dll) Then;d
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("44", $dll)
MouseMove($mx + $i, $my)
$i += 10
WEnd
ElseIf _IsPressed("24", $dll) Then;home
MouseClick("left")
ElseIf _IsPressed("23", $dll) Then;end
MouseClick("right")
EndIf
WEnd
DllClose($dll)
就像楼主所说的。我现在在浏览网页,当我按下end键时,模拟点击了鼠标的右键,但是网页也跳到页面底部了。要如何仅仅让它模拟鼠标点击,而不反馈给其他程序呢? 原来这样可以。#include <Misc.au3>
HotKeySet("{home}","UUU")
HotKeySet("{end}","UUU")
HotKeySet("{w}","UUU")
HotKeySet("{s}","UUU")
HotKeySet("{a}","UUU")
HotKeySet("{d}","UUU")
Local $dll = DllOpen("user32.dll")
While 1
Sleep(50)
If _IsPressed("57", $dll) Then;W,上
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("57", $dll)
MouseMove($mx, $my - $i)
$i += 10
WEnd
ElseIf _IsPressed("53", $dll) Then;S,下
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("53", $dll)
MouseMove($mx, $my + $i)
$i += 10
WEnd
ElseIf _IsPressed("41", $dll) Then;A,左
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("41", $dll)
MouseMove($mx - $i, $my)
$i += 10
WEnd
ElseIf _IsPressed("44", $dll) Then;D,右
$i = 10
$mousepos = MouseGetPos()
$mx = $mousepos
$my = $mousepos
While _IsPressed("44", $dll)
MouseMove($mx + $i, $my)
$i += 10
WEnd
ElseIf _IsPressed("24", $dll) Then;HOME,点击左键
MouseClick("left")
ElseIf _IsPressed("23", $dll) Then;END,点击右键
MouseClick("right")
EndIf
WEnd
DllClose($dll)
Func uuu()
EndFunc
回复 1# fenghou
用 _IsPressed 配合 HotKeySet 试试 #include <Misc.au3>
HotKeySet('^q', '_Exit')
HotKeySet('g', '_key_G')
Global $hDLL = DllOpen('user32.dll')
Global $sKeyMouseLeft = '47' ; G key
Local $bMouseLeftPressed = False, $bKeyMouseLeftPressed
While 1
Sleep(1000)
WEnd
Func _key_G()
HotKeySet('g', '')
Do
$bKeyMouseLeftPressed = _IsPressed($sKeyMouseLeft, $hDLL)
If $bKeyMouseLeftPressed And Not $bMouseLeftPressed Then
MouseDown('left')
$bMouseLeftPressed = True
EndIf
Sleep(5)
Until Not $bKeyMouseLeftPressed
MouseUp('left')
$bMouseLeftPressed = False
HotKeySet('g', '_key_G')
EndFunc ;==>_key_G
Func _Exit()
DllClose($hDLL)
Exit
EndFunc ;==>_Exit
回复 6# afan
原来只要在HotKeySet激发的函数里临时取消热键就可以避免多重激发了,非常感谢! 回复 4# zerobin
原来把热键用HotKeySet屏蔽掉就可以了,感谢! 回复 8# fenghou
我也是看到你的提问帖子才去弄的。共同提高。 回复 8# fenghou $bMouseLeftPressed = False, $bKeyMouseLeftPressed能否讲解下,上面是什么意思 回复fenghou 能否讲解下,上面是什么意思
zerobin 发表于 2012-6-19 22:07 http://www.autoitx.com/images/common/back.gif
就是两个变量的定义,第一个是鼠标左键是否被按下,第二个是鼠标左键对应的键盘热键是否被按下。 回复 12# fenghou
明白了。tks 又学习到了,谢谢LS各位 这样啊,学习了。 看到就回复下
页:
[1]