AutoHotkey 按住键运行脚本

AutoHotkey 按住键运行脚本

我需要脚本方面的帮助,我希望它只在我按住某个键时运行。以下是脚本:

;If you use this, you have to use absolute screen coordinates.
CoordMode, Mouse, Screen

;Suppose a 100x100 px bounding box for your game inventory.
;Eg., from (500, 500) to (600, 600)
w::
{
    ;Get current Mouse coords
    MouseGetPos, xCurrent ,yCurrent

    ;Calculate future Mouse coords
    xMoved := xCurrent
    yMoved := yCurrent - 35

    ;Check if the future mouse postion will be
    ;below the top border of your bounding box, 
    ;aka still inside it, after it has moved.
    ;If so, proceed and move the mouse,
    ;otherwise do nothing.
MouseGetPos, CoordXRec, CoordYRec
        MouseMove, xMoved, yMoved


if(yMoved < 503 && yMoved > 350 && yMoved > 360){
MouseMove 1846, 166
}
if(yMoved < 145){
MouseMove, %CoordXRec%, %CoordYRec%, 0
}
if(yMoved < 718 && yMoved < 720 && yMoved > 680){
MouseMove 1771, 671
}
return  
}
s::
{
    ;Get current Mouse coords
    MouseGetPos, xCurrent ,yCurrent

    ;Calculate future Mouse coords
    xMoved := xCurrent
    yMoved := yCurrent +35

    ;Check if the future mouse postion will be
    ;below the top border of your bounding box, 
    ;aka still inside it, after it has moved.
    ;If so, proceed and move the mouse,
    ;otherwise do nothing.

        MouseMove, xMoved, yMoved

if(yMoved > 285 && yMoved < 360){
MouseMove 1773, 526
}
if(yMoved > 697 && yMoved < 715){
MouseMove 1772, 736
}
return
}
a::
{
    ;Get current Mouse coords
    MouseGetPos, xCurrent ,yCurrent

    ;Calculate future Mouse coords
    xMoved := xCurrent -40
    yMoved := yCurrent 

    ;Check if the future mouse postion will be
    ;below the top border of your bounding box, 
    ;aka still inside it, after it has moved.
    ;If so, proceed and move the mouse,
    ;otherwise do nothing.
    if (xMoved > 1740) {
        MouseMove, xMoved, yMoved
    }
return  
}
d::
{
    ;Get current Mouse coords
    MouseGetPos, xCurrent ,yCurrent

    ;Calculate future Mouse coords
    xMoved := xCurrent +40
    yMoved := yCurrent 

    ;Check if the future mouse postion will be
    ;below the top border of your bounding box, 
    ;aka still inside it, after it has moved.
    ;If so, proceed and move the mouse,
    ;otherwise do nothing.
    if (xMoved < 1917) {
        MouseMove, xMoved, yMoved
    }
return  
}

基本上,您可以使用 WASD 控制鼠标,它还有一些其他功能,但我想让您必须按住某个键才能移动。谢谢!

答案1

如果您指的是按住执行该操作的键并在操作激活之前有一定的延迟(而不是按住其他键,如修饰键),您可以按照GetKeyState前面提到的方式做一些事情。

以下代码集可以作为函数插入或从每个热键定义中调用。您可以根据需要更改延迟设置,以适合您要执行的操作(或者它可能不会完全按照您的意愿执行,并且如果这不是您想要的,可能会对您来说很笨拙,但原始帖子非常模糊)。

将此代码插入每个热键定义的开头,它将要求您按住该热键一段时间才能激活,否则将定期发送击键。您还需要$在热键定义前面添加一个,这样该Send语句就不会连续两次重新触发热键定义并导致立即执行(因为如果用户只是短暂按下该键而不按住它,则此前驱函数将发送与热键相同的键)。

由于热键变量是通用的,您也可以将其放在单独的函数调用中,这样您就不必重复代码,然后只需为每个热键调用该函数即可。即使它不是您想要的,也许这会给您一些想法。

$a::
{
    ; make these global or static as desired so they don't have to be reassigned for each function call
    holdDelay:=300          ; time to wait before activation (or conversely, must release before this time to send a keystroke)
    repeatDelay:=700        ; max time to detect a repeat event--must be bigger than holdDelay or won't work

    ; if we just triggered this key last time and it's been less than the repeat delay,
    ; bypass the hold-down check and skip directly to desired function for moving the mouse
    If Not ((A_ThisHotkey=A_PriorHotkey) && (A_TimeSincePriorHotkey<repeatDelay)) {

        trimmedHotkey:=Trim(A_ThisHotkey, "$")  ; get rid of preceding $ so we can detect and send just the keystroke

        startTick:=A_TickCount                              ; init timer
        While GetKeyState(trimmedHotkey, "P")               ; Loop while key is pressed down, otherwise exit    
        && !(heldDown:=(A_TickCount-startTick)>holdDelay)   ; check for helddown/timeout (user held the key down)
            Sleep 10                                        ; wait a few ms...

        If !heldDown {                  ; if user didn't hold the key down, send it back as a keystroke

            SendInput % trimmedHotkey
            Return 
        }

    }

    ; Rest of the code goes here

相关内容