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

51 天前
 MajestySolor

以下这个 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)
}


836 次点击
所在节点    问与答
5 条回复
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
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
50 天前
@werwer 你这也是 chatgpt 生成的啊,我已经用 chatgpt 折腾好久了,反复出错🤣

werwer
50 天前
@MajestySolor 你这个可以当游戏玩,比谁更接近于 1 秒
MajestySolor
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")
}
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1061591

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX