找回密码
 加入
搜索
查看: 7186|回复: 14

[系统综合] [已解决] 用键盘模拟鼠标键,始终做不到完美

  [复制链接]
发表于 2012-6-19 14:27:10 | 显示全部楼层 |阅读模式
本帖最后由 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
发表于 2012-6-19 18:43:52 | 显示全部楼层
用 func 来代替部分代码进行调整
发表于 2012-6-19 19:24:36 | 显示全部楼层
本帖最后由 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[0]
                $my = $mousepos[1]
                While _IsPressed("57", $dll)
                        MouseMove($mx, $my - $i)
                        $i += 10
                WEnd
        ElseIf _IsPressed("53", $dll) Then;s
                $i = 10
                $mousepos = MouseGetPos()
                $mx = $mousepos[0]
                $my = $mousepos[1]
                While _IsPressed("53", $dll)
                        MouseMove($mx, $my + $i)
                        $i += 10
                WEnd
        ElseIf _IsPressed("41", $dll) Then;a
                $i = 10
                $mousepos = MouseGetPos()
                $mx = $mousepos[0]
                $my = $mousepos[1]
                While _IsPressed("41", $dll)
                        MouseMove($mx - $i, $my)
                        $i += 10
                WEnd
        ElseIf _IsPressed("44", $dll) Then;d
                $i = 10
                $mousepos = MouseGetPos()
                $mx = $mousepos[0]
                $my = $mousepos[1]
                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键时,模拟点击了鼠标的右键,但是网页也跳到页面底部了。要如何仅仅让它模拟鼠标点击,而不反馈给其他程序呢?
发表于 2012-6-19 19:53:27 | 显示全部楼层
原来这样可以。
#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[0]
                $my = $mousepos[1]
                While _IsPressed("57", $dll)
                        MouseMove($mx, $my - $i)
                        $i += 10
                WEnd
        ElseIf _IsPressed("53", $dll) Then;S,下
                $i = 10
                $mousepos = MouseGetPos()
                $mx = $mousepos[0]
                $my = $mousepos[1]
                While _IsPressed("53", $dll)
                        MouseMove($mx, $my + $i)
                        $i += 10
                WEnd
        ElseIf _IsPressed("41", $dll) Then;A,左
                $i = 10
                $mousepos = MouseGetPos()
                $mx = $mousepos[0]
                $my = $mousepos[1]
                While _IsPressed("41", $dll)
                        MouseMove($mx - $i, $my)
                        $i += 10
                WEnd
        ElseIf _IsPressed("44", $dll) Then;D,右
                $i = 10
                $mousepos = MouseGetPos()
                $mx = $mousepos[0]
                $my = $mousepos[1]
                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
发表于 2012-6-19 19:57:29 | 显示全部楼层
回复 1# fenghou


    用 _IsPressed 配合 HotKeySet 试试
发表于 2012-6-19 20:20:00 | 显示全部楼层
#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
 楼主| 发表于 2012-6-19 21:13:51 | 显示全部楼层
回复 6# afan

原来只要在HotKeySet激发的函数里临时取消热键就可以避免多重激发了,非常感谢!
 楼主| 发表于 2012-6-19 21:19:45 | 显示全部楼层
回复 4# zerobin

原来把热键用HotKeySet屏蔽掉就可以了,感谢!
发表于 2012-6-19 21:59:03 | 显示全部楼层
回复 8# fenghou

我也是看到你的提问帖子才去弄的。共同提高。
发表于 2012-6-19 22:07:36 | 显示全部楼层
回复 8# fenghou
$bMouseLeftPressed = False, $bKeyMouseLeftPressed
能否讲解下,上面是什么意思
 楼主| 发表于 2012-6-19 22:42:53 | 显示全部楼层
回复  fenghou 能否讲解下,上面是什么意思
zerobin 发表于 2012-6-19 22:07


就是两个变量的定义,第一个是鼠标左键是否被按下,第二个是鼠标左键对应的键盘热键是否被按下。
发表于 2012-6-19 22:45:48 | 显示全部楼层
回复 12# fenghou


    明白了。tks
发表于 2012-6-20 15:12:49 | 显示全部楼层
又学习到了,谢谢LS各位
发表于 2012-8-9 11:09:45 | 显示全部楼层
这样啊,学习了。
发表于 2015-9-11 10:51:10 | 显示全部楼层
看到就回复下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 20:16 , Processed in 0.084268 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表