AutoHotkey中的脚本以同时按下Windows按钮和向左箭头

索海尔

我想在autohotkey中编写一个脚本,以便每次我在PC上打开字典应用程序时,同时按下Windows+LeftArrow键,结果,它会捕捉监视器左侧的窗口。

我尝试了这个:

#IfWinActive Oxford Advanced Learner's Dictionary
Send, #{Left}
return

也是这个:

#IfWinActive Oxford Advanced Learner's Dictionary
Send, {LWinDown}{Left}{LWinup}
return

但是对于他们中的任何一个,我打开应用程序时都注意到了。

编辑:

正如@Charlie Armstrong所建议的那样,真正的问题是:每次启动某个程序时,如何使代码块运行?因此#IfWinActive可能对您没有用。

乔·DF

一种方法是定期检查是否创建了新的进程/窗口,并检查是否是我们要与之交互的进程/窗口。

第一个示例基于创建/销毁进程时的COM通知。

; help for question: https://stackoverflow.com/q/66394326/883015
; by joedf (16:04 2021/02/28)

MyWatchedWindowTitle := "Oxford Advanced Learner's Dictionary"
NewProcess_CheckInterval := 1 ; in seconds
SetTitleMatchMode, 2 ;this might not be needed, makes the check for "contains" instead of "same" winTitle
hWnds := []
gosub, initialize_NewProcessNotification
return

; Called when a new process is detected
On_NewProcess(proc) {
    global hWnds
    global MyWatchedWindowTitle
    
    ; get the window handle, if possible
    if (hwnd:=WinExist("ahk_pid " proc.ProcessID)) {
        WinGetTitle, wTitle, ahk_id %hwnd%
        
        ; check if there is a visible window
        if (wTitle)
        {
            ; if so, check if it's a window we want to interact with
            if (InStr(wTitle,MyWatchedWindowTitle))
            {
                ; check if we've interacted with this specific window before
                if (!ArrayContains(hWnds, hwnd)) {
                    ; we havent, so we do something with it
                    hWnds.push(hwnd) ; keep in memory that we have interacted with this window ID before.
                    DoSomething(hwnd) ; the keys we want to send to it
                }
            }
        }
    }
}

DoSomething(hwnd) {
    ; size and move window to the left
    SysGet, MonitorWorkArea, MonitorWorkArea
    posY := 0
    posX  := 0
    width := A_ScreenWidth // 2
    height := MonitorWorkAreaBottom
    WinMove, ahk_id %hwnd% ,,%posX%,%posY%,%width%,%height%
    
    ; multi-montitor support, more examples, and more complete snapping functions can be found here:
    ; https://gist.github.com/AWMooreCO/1ef708055a11862ca9dc
}

ArrayContains(haystack, needle) {
    for k, v in haystack
    {
        if (v == needle)
            return true
    }
    return false
}



initialize_NewProcessNotification:
;////////////////////////////// New Process notificaton ////////////////////////
; from Lexikos' example
; https://autohotkey.com/board/topic/56984-new-process-notifier/#entry358038

; Get WMI service object.
winmgmts := ComObjGet("winmgmts:")

; Create sink objects for receiving event noficiations.
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

; Set event polling interval, in seconds.
interval := NewProcess_CheckInterval

; Register for process creation notifications:
winmgmts.ExecNotificationQueryAsync(createSink
    , "Select * from __InstanceCreationEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Register for process deletion notifications:
winmgmts.ExecNotificationQueryAsync(deleteSink
    , "Select * from __InstanceDeletionEvent"
    . " within " interval
    . " where TargetInstance isa 'Win32_Process'")

; Don't exit automatically.
#Persistent
return

; Called when a new process is detected:
ProcessCreate_OnObjectReady(obj) {
    proc := obj.TargetInstance
    /*
    TrayTip New Process Detected, % "
    (LTrim
        ID:`t" proc.ProcessID "
        Parent:`t" proc.ParentProcessID "
        Name:`t" proc.Name "
        Path:`t" proc.ExecutablePath "
        
        Command line (requires XP or later):
        
        " proc.CommandLine
    )
    */
    On_NewProcess(proc)
}

; Called when a process terminates:
ProcessDelete_OnObjectReady(prm) {
    /*
    obj := COM_DispGetParam(prm, 0, 9)
    proc := COM_Invoke(obj, "TargetInstance")
    COM_Release(obj)
    TrayTip Process Terminated, % "
    (LTrim
        ID:`t" COM_Invoke(proc, "Handle") "
        Name:`t" COM_Invoke(proc, "Name")
    )
    COM_Release(proc)
    */
}

第二个示例可能更简单一些,它定期检查与searchd匹配的新窗口WinTitle

; help for question: https://stackoverflow.com/q/66394326/883015
; by joedf (16:17 2021/02/28)

#Persistent

MyWatchedWindowTitle := "Oxford Advanced Learner's Dictionary"
SetTitleMatchMode, 2 ;this might not be needed, makes the check for "contains" instead of "same" winTitle
SetTimer, checkForNewWindow, 1000 ;ms
hWnds := []
return

checkForNewWindow() {
    global hWnds
    global MyWatchedWindowTitle
    
    ; first check if there is at least one window that matches our winTitle
    if (hwnd:=WinExist(MyWatchedWindowTitle)) {
        ; get all window matches
        WinGet, wArray, List , %MyWatchedWindowTitle%
        
        ; loop through all windows that matched
        loop % wArray
        {
            hWnd := wArray%A_Index%

            ; check if we've interacted with this specific window before
            if (!ArrayContains(hWnds, hwnd)) {
                ; we havent, so we do something with it
                hWnds.push(hwnd) ; keep in memory that we have interacted with this window ID before.
                DoSomething(hwnd) ; the keys we want to send to it
            }
        }
    }
}

DoSomething(hwnd) {
    ; size and move window to the left
    SysGet, MonitorWorkArea, MonitorWorkArea
    posY := 0
    posX  := 0
    width := A_ScreenWidth // 2
    height := MonitorWorkAreaBottom
    WinMove, ahk_id %hwnd% ,,%posX%,%posY%,%width%,%height%
    
    ; multi-montitor support, more examples, and more complete snapping functions can be found here:
    ; https://gist.github.com/AWMooreCO/1ef708055a11862ca9dc
}

ArrayContains(haystack, needle) {
    for k, v in haystack
    {
        if (v == needle)
            return true
    }
    return false
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AutoHotkey中的脚本以同时按下Windows按钮和向左箭头

来自分类Dev

AutoHotkey按下按钮可将光标向左移动

来自分类Dev

同时按下按钮

来自分类Dev

如何从Bash / PowerShell脚本中的Windows 10显示设置模拟MONITOR DETECT按钮按下

来自分类Dev

vb.net在文本框中同时按下获取箭头键

来自分类Dev

在Google Apps脚本中获取按下按钮的ID或标签

来自分类Dev

onActivityResult不是在屏幕上按下向后箭头时调用,而是仅在android中按下向后按钮时调用?

来自分类Dev

未同时按下多个按钮

来自分类Dev

无法同时按下某些键盘按钮

来自分类Dev

recyclerView 列表允许同时按下按钮

来自分类Dev

同时按下两个按钮

来自分类Dev

按住Shift键的同时按下箭头键

来自分类Dev

编写批处理脚本以按“ Tab”和“ Enter”键

来自分类Dev

如何让脚本等待按钮按下?

来自分类Dev

按下按钮停止Shell脚本

来自分类Dev

在算法中反向(向左)箭头?

来自分类Dev

如何在UWP ToggleSwitch中按向左或向右箭头键(从键盘)取消Toggle

来自分类Dev

按下鼠标按钮时按下键盘键的脚本

来自分类Dev

按下向上和向下箭头选择锚标签

来自分类Dev

在Sublime中,界面左上角的向左/向右箭头按钮有什么作用?

来自分类Dev

android:按钮的按下状态和非按下状态

来自分类Dev

如何防止用户同时按下两个按钮?

来自分类Dev

检查是否同时按下两个按钮

来自分类Dev

同时按下2个按钮时执行操作

来自分类Dev

如何防止用户同时按下两个按钮?

来自分类Dev

为什么按下向左或向右箭头键时第四个字符不移动?

来自分类Dev

Windows Phone按钮按下效果

来自分类Dev

GtkEntry和按钮按下事件

来自分类Dev

怎么做:按下按钮后-当前视图将向左交换,新视图将出现

Related 相关文章

  1. 1

    AutoHotkey中的脚本以同时按下Windows按钮和向左箭头

  2. 2

    AutoHotkey按下按钮可将光标向左移动

  3. 3

    同时按下按钮

  4. 4

    如何从Bash / PowerShell脚本中的Windows 10显示设置模拟MONITOR DETECT按钮按下

  5. 5

    vb.net在文本框中同时按下获取箭头键

  6. 6

    在Google Apps脚本中获取按下按钮的ID或标签

  7. 7

    onActivityResult不是在屏幕上按下向后箭头时调用,而是仅在android中按下向后按钮时调用?

  8. 8

    未同时按下多个按钮

  9. 9

    无法同时按下某些键盘按钮

  10. 10

    recyclerView 列表允许同时按下按钮

  11. 11

    同时按下两个按钮

  12. 12

    按住Shift键的同时按下箭头键

  13. 13

    编写批处理脚本以按“ Tab”和“ Enter”键

  14. 14

    如何让脚本等待按钮按下?

  15. 15

    按下按钮停止Shell脚本

  16. 16

    在算法中反向(向左)箭头?

  17. 17

    如何在UWP ToggleSwitch中按向左或向右箭头键(从键盘)取消Toggle

  18. 18

    按下鼠标按钮时按下键盘键的脚本

  19. 19

    按下向上和向下箭头选择锚标签

  20. 20

    在Sublime中,界面左上角的向左/向右箭头按钮有什么作用?

  21. 21

    android:按钮的按下状态和非按下状态

  22. 22

    如何防止用户同时按下两个按钮?

  23. 23

    检查是否同时按下两个按钮

  24. 24

    同时按下2个按钮时执行操作

  25. 25

    如何防止用户同时按下两个按钮?

  26. 26

    为什么按下向左或向右箭头键时第四个字符不移动?

  27. 27

    Windows Phone按钮按下效果

  28. 28

    GtkEntry和按钮按下事件

  29. 29

    怎么做:按下按钮后-当前视图将向左交换,新视图将出现

热门标签

归档