Skip to content

Instantly share code, notes, and snippets.

@coreequip
Last active November 20, 2019 15:31
Show Gist options
  • Save coreequip/b0330299fe61cf9038ed4f581f6f21c6 to your computer and use it in GitHub Desktop.
Save coreequip/b0330299fe61cf9038ed4f581f6f21c6 to your computer and use it in GitHub Desktop.
Listens to the global hotkey MEDIA_PLAY_PAUSE and toggles mumble's self mute.
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
"unsafe"
)
type (
HANDLE uintptr
HWND HANDLE
)
var (
user32dll = syscall.NewLazyDLL("user32.dll")
procGetMessage = user32dll.NewProc("GetMessageW")
procRegisterHotKey = user32dll.NewProc("RegisterHotKey")
)
type MSG struct {
Hwnd HWND
Message uint32
WParam uintptr
LParam uintptr
Time uint32
Pt struct{ X, Y int64 }
}
func registerHotKey(hWnd HWND, id int, fsModifiers uint, vk uint) bool {
// modifiers: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey#parameters
// kecodes: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
ret, _, _ := procRegisterHotKey.Call(
uintptr(hWnd),
uintptr(id),
uintptr(fsModifiers),
uintptr(vk),
)
return ret != 0
}
func getMessage(msg *MSG, hwnd HWND, msgFilterMin, msgFilterMax uint32) int {
ret, _, _ := procGetMessage.Call(
uintptr(unsafe.Pointer(msg)),
uintptr(hwnd),
uintptr(msgFilterMin),
uintptr(msgFilterMax))
return int(ret)
}
func main() {
// default Mumble binary
mumblePath := "c:\\Program Files\\Mumble\\mumble.exe"
// can be overwritten by first command line argument
if len(os.Args) > 1 {
mumblePath = os.Args[1]
}
if _, err := os.Stat(mumblePath); os.IsNotExist(err) {
fmt.Printf("Path to mumble doesn't exists: %s", mumblePath)
return
}
var msg MSG
regOk := registerHotKey(0, 42030, 0x0, 0xB3)
if !regOk {
fmt.Println("Cant register hotkey!")
return
}
fmt.Printf("Okay, using Mumble at: %s\n", mumblePath)
for {
ok := getMessage(&msg, 0, 0, 0)
if ok != 1 {
time.Sleep(time.Millisecond * 50)
continue
}
switch msg.WParam {
case 42030:
fmt.Println("Toggle.")
cmdInstance := exec.Command("cmd", "/c", mumblePath, "rpc", "togglemute")
cmdInstance.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := cmdInstance.Run()
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment