【问题标题】:AHK wait for more presses of hotkey to cycle through optionsAHK 等待更多热键按下以循环选择选项
【发布时间】:2020-04-18 04:28:55
【问题描述】:

我正在与 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
}

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

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

    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 热键,但如果您的脚本较大并且您还触发了其他热键,请考虑切换到上面显示的计时器方法。只需重置 press_count 变量,而不是退出。

    你也没有说如果它被按下超过四次会发生什么,我将把它留给你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 2011-06-09
      • 2018-12-01
      • 2017-04-03
      • 2018-06-25
      相关资源
      最近更新 更多