AHK等待更多的热键按下以循环选择

时间:2019-12-28 18:19:37

标签: autohotkey

我正在与AHK一起使用COM来控制Excel。当前脚本的想法是让脚本在每次单击热键时都冻结冻结Excel工作表的不同选项。在这种情况下,它是^ f ::(CTRL + f)

我的想法是有一个5秒的计时器,并等待同时按下,每次都运行冻结功能。 5秒钟后,脚本终止。

我觉得这是使用WaitKey的相当简单的实现,但是我没有运气。我不太了解如何使用ErrorLevel并通过函数实现它。

任何帮助将不胜感激。我一直在查看文档和其他帖子,但无法弄清楚。我在下面写了一些伪代码来对您进行排序,以了解您要执行的操作。

press_cycle :=0

;; while KeyWait timer hasn't run out:
;;     wait for additional presses of hotkey
;;     if press 
;;         reset 5 sec timer
;;         run cycle_freezing() again 

cycle_freezing(){
    if (press_cycle = 3){
        MsgBox, "unfreezing"
        xl.Application.ActiveWindow.FreezePanes := False
        press_cycle := 0
    }
    if (press_cycle = 2){ ;; freeze header and selected column
        MsgBox, "freezing header and selected"
        ;XL_Freeze(xl,Row:=header_row,Col:=column)
        press_cycle := 3
    }
    if (press_cycle = 1){ ;; freeze header and first column
        MsgBox, "freezing header and first col"
        ;XL_Freeze(xl,Row:=header_row,Col:="B")
        press_cycle := 2
    }
    if (press_cycle = 0){ ;; freeze header
        MsgBox, "freezing header"
        XL_Freeze(xl,Row:=header_row,Col:="A")
        press_cycle := 1
    }
}
;***********************Freeze Panes********************************.
;~ XL_Freeze(XL,Row:="1",Col:="B") ;Col A will not include cols which is default so leave out if unwanted
;***********************Freeze Panes in Excel********************************.
XL_Freeze(xl,Row="",Col=""){
    xl.Application.ActiveWindow.FreezePanes := False ;unfreeze in case already frozen
    IfEqual,row,,return ;if no row value passed row;  turn off freeze panes
    xl.Application.ActiveSheet.Range(Col . Row+1).Select ;; Helps it work more intuitivly 
                                                         ;; 1 includes 1 not start at zero
    xl.Application.ActiveWindow.FreezePanes := True
}

1 个答案:

答案 0 :(得分:2)

这是您要的内容,但是我不确定终止脚本实际上是要终止脚本吗?对我来说听起来不明智。

press_count := 0
^f::
    if(!press_count) ;zero evaluates to false
        ToolTip, % "Action that should happen after the 1st press"
    else if (press_count = 1)
        ToolTip, % "Action that should happen after the 2nd press"
    else if (press_count = 2)
        ToolTip, % "Action that should happen after the 3rd press"
    else
        ToolTip, % "Action that should happen after the 4th press"

    if(!press_count) ;if we're on the very first keypress
        SetTimer, ExitTimerCallback, -5000 ;set the timer, -5000 means it'll run ONCE after 5000ms
    else
        SetTimer, ExitTimerCallback ;otherwise reset the timer's period
    press_count++ ;increase by one
return

ExitTimerCallback:
    ExitApp
return

这是一个对我来说更有意义的版本,这可能是您要问的意思?

^f::
    if(A_TimeSincePriorHotkey < 5000)
        press_count++ ;increase by one if we're inside that 5sec window
    else
        press_count := 1 ;reset otherwise
    if(press_count = 1)
        ToolTip, % "Action that should happen after the 1st press"
    else if (press_count = 2)
        ToolTip, % "Action that should happen after the 2nd press"
    else if (press_count = 3)
        ToolTip, % "Action that should happen after the 3rd press"
    else
        ToolTip, % "Action that should happen after the 4th press"
return

我想给A_TimeSincePriorHotkey变量一些识别。
但是请务必记住,该变量不仅适用于该^f热键,如果您的脚本较大并且您还触发了其他热键,请考虑切换到我上面显示的timer方法。重设press_count变量而不是退出。

您也没有说如果按下四次以上该怎么办,我将由您自己决定。