V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
MajestySolor
V2EX  ›  问与答

求 ahk 大佬帮忙修改一个脚本🐶

  •  
  •   MajestySolor · 51 天前 · 836 次点击
    这是一个创建于 51 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下这个 ahk 脚本的作用是按下 win+c 后把当前激活的窗口根据所在显示器居中,我希望能把 win+c 这个快捷键换成 1 秒内按下三次左侧 shift 键,chatgpt 帮我改了半天还是错的,心好累🤣

    #Requires AutoHotkey v2.0
    
    
    #c::CenterWindow("A")
    
    CenterWindow(winTitle*) {
        hwnd := WinExist(winTitle*)
        WinGetPos ,, &W, &H, hwnd
        mon := GetNearestMonitorInfo(hwnd)
        WinMove mon.WALeft + mon.WAWidth // 2 - W // 2, mon.WATop + mon.WAHeight // 2 - H // 2,,, hwnd
    }
    
    GetNearestMonitorInfo(winTitle*) {
        static MONITOR_DEFAULTTONEAREST := 0x00000002
        hwnd := WinExist(winTitle*)
        hMonitor := DllCall("MonitorFromWindow", "ptr", hwnd, "uint", MONITOR_DEFAULTTONEAREST, "ptr")
        NumPut("uint", 104, MONITORINFOEX := Buffer(104))
        if (DllCall("user32\GetMonitorInfo", "ptr", hMonitor, "ptr", MONITORINFOEX)) {
            Return  { Handle   : hMonitor
                    , Name     : Name := StrGet(MONITORINFOEX.ptr + 40, 32)
                    , Number   : RegExReplace(Name, ".*(\d+)$", "$1")
                    , Left     : L  := NumGet(MONITORINFOEX,  4, "int")
                    , Top      : T  := NumGet(MONITORINFOEX,  8, "int")
                    , Right    : R  := NumGet(MONITORINFOEX, 12, "int")
                    , Bottom   : B  := NumGet(MONITORINFOEX, 16, "int")
                    , WALeft   : WL := NumGet(MONITORINFOEX, 20, "int")
                    , WATop    : WT := NumGet(MONITORINFOEX, 24, "int")
                    , WARight  : WR := NumGet(MONITORINFOEX, 28, "int")
                    , WABottom : WB := NumGet(MONITORINFOEX, 32, "int")
                    , Width    : Width  := R - L
                    , Height   : Height := B - T
                    , WAWidth  : WR - WL
                    , WAHeight : WB - WT
                    , Primary  : NumGet(MONITORINFOEX, 36, "uint")
                }
        }
        throw Error("GetMonitorInfo: " A_LastError, -1)
    }
    
    
    
    5 条回复    2024-08-01 21:45:42 +08:00
    Zaden
        1
    Zaden  
       51 天前
    可以参考以下代码,左 shift 键代码是 sc02A:
    ;■双击,,输入,
    ~sc033::
    Keywait, sc033, , t0.2
    if errorlevel = 1
    return
    else
    Keywait, sc033, d, t0.2
    if errorlevel = 0
    {
    send,{backspace 2}
    Sendinput % uStr(",")
    }
    return
    werwer
        2
    werwer  
       50 天前
    ; 全局变量
    KeyPressTimes := [] ; 存储按键时间戳的数组

    ; 监听左 Shift 键按下事件
    ~LShift::
    ; 获取当前时间戳
    CurrentTime := A_TickCount

    ; 将当前时间戳存储到数组中
    KeyPressTimes.Push(CurrentTime)

    ; 如果数组中有超过三个时间戳,移除最早的那个
    if (KeyPressTimes.Length() > 3) {
    KeyPressTimes.RemoveAt(1)
    }

    ; 检查时间间隔
    if (KeyPressTimes.Length() == 3) {
    FirstInterval := KeyPressTimes[2] - KeyPressTimes[1]
    SecondInterval := KeyPressTimes[3] - KeyPressTimes[2]
    TotalInterval := FirstInterval + SecondInterval

    if (TotalInterval < 1000) {
    MsgBox, 快速按下左 Shift 键三次,时间间隔:`n 第一次间隔: %FirstInterval% ms`n 第二次间隔: %SecondInterval% ms`n 总间隔: %TotalInterval% ms
    ; 在此处执行你希望的操作
    }

    ; 清空数组
    KeyPressTimes := []
    }
    return
    MajestySolor
        3
    MajestySolor  
    OP
       50 天前
    @werwer 你这也是 chatgpt 生成的啊,我已经用 chatgpt 折腾好久了,反复出错🤣

    werwer
        4
    werwer  
       50 天前
    @MajestySolor 你这个可以当游戏玩,比谁更接近于 1 秒
    MajestySolor
        5
    MajestySolor  
    OP
       50 天前
    已解决,这样写就行了🤣

    ```
    ~LShift up::
    {
    static presses := 0
    static firstPress := A_TickCount
    time := A_TickCount - firstPress
    if ++presses >= 4 || time > 1000 {
    presses := 1
    firstPress := A_TickCount
    }
    if (presses = 3) && time <= 1000
    CenterWindow("A")
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2749 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:48 · PVG 16:48 · LAX 01:48 · JFK 04:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.