Skip to content

Instantly share code, notes, and snippets.

@lmorchard
Last active January 29, 2019 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmorchard/399c8220141ec98c35ed64b06b634ecd to your computer and use it in GitHub Desktop.
Save lmorchard/399c8220141ec98c35ed64b06b634ecd to your computer and use it in GitHub Desktop.
Use AutoHotKey to move an OBS display capture source around to track the current window
#NoEnv
SetBatchLines, -1
; https://github.com/G33kDude/WebSocket.ahk
#Include ./WebSocket.ahk
sceneName := "Show desktop follow"
sourceName := "Desktop"
scale := 0.75
sourceWidth := 1455
sourceHeight := 1080
; TODO: This number mostly works to fix a bug with switching scale calculation
; for tall vs wide windows - but I don't know why. Figure out why.
winHeightFudge := 350
obs := new WebSocket("ws://127.0.0.1:4444/")
SetTimer, CheckMouseRegion, 250
CheckMouseRegion:
CoordMode, Mouse, Screen
SysGet mon, Monitor
WinGetPos, xpos, ypos, winWidth, winHeight, A
; Skip if window is off the main monitor
if (xpos >= monRight or ypos >= monBottom)
return
; Skip if none of the window parameters changed
if ((xpos = wasXpos)
and (ypos = wasYpos)
and (winWidth = wasWinWidth)
and (winHeight = wasWinHeight)) {
return
}
if (winWidth <= winHeight + winHeightFudge) {
scale := (sourceHeight / winHeight)
} else {
scale := (sourceWidth / winWidth)
}
sourceX := (sourceWidth / 2) - ((xpos + ((winWidth) / 2)) * scale)
sourceY := (sourceHeight / 2) - ((ypos + ((winHeight) / 2)) * scale)
; FIXME: Maybe the message-id here should be unique and not always "foo"?
message =
(
{
"message-id": "foo",
"request-type": "SetSceneItemProperties",
"scene-name": "%sceneName%",
"item": "%sourceName%",
"position": { "x": %sourceX%, "y": %sourceY% },
"scale": { "x": %scale%, "y": %scale% }
}
)
obs.send(message)
wasXpos := xpos
wasYpos := ypos
wasWinWidth := winWidth
wasWinHeight := winHeight
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment