按键音注册示例

; 来源:https://www.autohotkey.com/boards/viewtopic.php?f=76&t=38007
#Persistent
arr := { "~a": "C:\Windows\Media\Alarm03.wav"
     , "~s": "C:\Windows\Media\Alarm04.wav"
     , "~d": "C:\Windows\Media\Alarm05.wav"
     , "~f": "C:\Windows\Media\Alarm06.wav" }
PlaySound.Load(arr)
return

class PlaySound {
  Load(arr) {
    for k, v in arr {
      player := ComObjCreate("WMPlayer.OCX.7")
      , playlist := player.currentPlaylist
      , playlist.appendItem(player.newMedia(v))
      , handler := ObjBindMethod(this, "Play", player) 
      Hotkey, % k, % handler, On
    }
  }

  Play(player) {
    player.controls.play
  }
}

/*  ; 另一版
~a:: PlaySound("C:\Windows\Media\Alarm03.wav")
~b:: PlaySound("C:\Windows\Media\Alarm04.wav")
~c:: PlaySound("C:\Windows\Media\Alarm05.wav")
~d:: PlaySound("C:\Windows\Media\Alarm06.wav")

PlaySound(filePath)  {
  static players := []
  player := ComObjCreate("WMPlayer.OCX.7")
  ComObjConnect(player, WatchStatus)
  ( !WatchStatus.players && WatchStatus.players := players )
  Loop
    i := A_Index
  until !players.HasKey(i) && (players[i] := player) && player.url := filePath
}

class WatchStatus  {
  StatusChange(wmp)  {
    static Stopped := 1
    if (wmp.PlayState = Stopped)  {
      for k, v in this.players  {
        if (v = wmp)
          break
      }
      this.players.Delete(k)
    }
  }
}
*/

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。