首页 > 解决方案 > AutoHotKey 播放/暂停 Youtube

问题描述

我正在尝试重新映射我的 Surface Pen 按钮单击以在 Chrome 中暂停/取消暂停 youtube。如果 Chrome 没有聚焦,或者 Chrome 聚焦并且 youtube 是活动选项卡,我可以让它工作,但如果 Chrome 聚焦但 youtube 不是活动选项卡,它就不起作用。

这就是我所拥有的:

    #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
    #Warn  ; Enable warnings to assist with detecting common errors.
    SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

    SetTitleMatchMode 2

    #IfWinNotActive, ahk_exe chrome.exe
    #F20::
        ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
        ControlFocus,,ahk_id %controlID%
        tabCount := 0
        Loop {
            IfWinExist, YouTube
                break
            ControlSend, , ^{PgUp} , Google Chrome
            sleep 150
            tabCount := tabCount + 1
            if tabCount = 10
                break
        }
        if tabCount < 10
            ControlSend, , k , Google Chrome
        Loop, %tabCount% {
            ControlSend, , ^{PgDn} , Google Chrome
            sleep 150
        }
        return
    #IfWinNotActive

    #IfWinActive, ahk_exe chrome.exe
    #F20::
        tabCount := 0
        Loop {
            IfWinExist, YouTube
                break
            ControlSend, , ^{PgUp} , Google Chrome
            sleep 150
            tabCount := tabCount + 1
            if tabCount = 10
                break
        }
        if tabCount < 10
            ControlSend, , k , Google Chrome
        Loop, %tabCount% {
            ControlSend, , ^{PgDn} , Google Chrome
            sleep 150
        }
        return
    #IfWinActive

标签: autohotkey

解决方案


事实证明这已经足够了:

    #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
    #Warn  ; Enable warnings to assist with detecting common errors.
    SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

    #F20::Media_Play_Pause

推荐阅读