Skip to content

Instantly share code, notes, and snippets.

@AlexeyGy
Last active September 18, 2020 12:46
Show Gist options
  • Save AlexeyGy/19632eb5e7fe6422be0b8e104ef4120c to your computer and use it in GitHub Desktop.
Save AlexeyGy/19632eb5e7fe6422be0b8e104ef4120c to your computer and use it in GitHub Desktop.
// HOTKEYS to listen to
var HOTKEYS = map[int16]*Hotkey{
1: &Hotkey{4, ModAlt + ModCtrl, 'D'},
}
const (
ModAlt = 1 << iota
ModCtrl
ModShift
ModWin
)
//Hotkey ..
type Hotkey struct {
ID int // Unique id
Modifiers int // Mask of modifiers
KeyCode int // Key code, e.g. 'A'
}
// In order for fmt.Println to give us a human friendly display of the hotkey,
// we provide a custom String() function to the Hotkey struc
// such as "Hotkey[Id: 1, Alt+Ctrl+O]"
func (h *Hotkey) String() string {
mod := &bytes.Buffer{}
if h.Modifiers&ModAlt != 0 {
mod.WriteString("Alt+")
}
if h.Modifiers&ModCtrl != 0 {
mod.WriteString("Ctrl+")
}
if h.Modifiers&ModShift != 0 {
mod.WriteString("Shift+")
}
if h.Modifiers&ModWin != 0 {
mod.WriteString("Win+")
}
return fmt.Sprintf("Hotkey[Id: %d, %s%c]", h.ID, mod, h.KeyCode)
}
func registerHotkeys(user32 *syscall.DLL) {
reghotkey := user32.MustFindProc("RegisterHotKey") // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
// Register hotkeys:
for _, v := range HOTKEYS {
r1, _, err := reghotkey.Call(
0, uintptr(v.ID), uintptr(v.Modifiers), uintptr(v.KeyCode))
if r1 == 1 {
fmt.Println("Registered", v)
} else {
fmt.Println("Failed to register", v, ", error:", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment