hook

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2025 License: MIT Imports: 9 Imported by: 0

README

gohook

This is a fork of gohook that is updated to use Go 1.25.0 and has been rewritten to completely to fix the issues with mouse events.

Requirements (Linux):

Robotgo-requirements-event

Install:

With Go module support (Go 1.11+), just import:

import "github.com/ms-dosx86/gohook"

Examples:

package main

import (
	"fmt"

	hook "github.com/ms-dosx86/gohook"
)

func main() {
	add()

	low()
}

func add() {
	fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
	hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
		fmt.Println("ctrl-shift-q")
		hook.End()
	})

	fmt.Println("--- Please press w---")
	hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
		fmt.Println("keyDown: ", "w")
	})

	hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) {
		fmt.Println("keyUp: ", "w")
	})

	s := hook.Start()
	<-hook.Process(s)
}

func low() {
	evChan := hook.Start()
	defer hook.End()

	for ev := range evChan {
		fmt.Println("hook: ", ev)
	}
}

Based on libuiohook.

Documentation

Index

Constants

View Source
const (
	// Version get the gohook version
	Version = "v0.41.0"

	// HookEnabled honk enable status
	HookEnabled  = 1 // iota
	HookDisabled = 2

	KeyDown = 4 // 3
	KeyHold = 3 // 4
	KeyUp   = 5 // 5

	MouseDown = 7 // 6
	MouseHold = 8 // 7
	MouseUp   = 6 // 8

	MouseMove  = 9
	MouseDrag  = 10
	MouseWheel = 11

	FakeEvent = 12

	// Keychar could be v
	CharUndefined = 0xFFFF
	WheelUp       = -1
	WheelDown     = 1
	Debug         = DebugLevel(13)
	Silent        = DebugLevel(14)
)

Variables

View Source
var Keycode = WindowsVKCodes
View Source
var MouseMap = keycode.MouseMap

MouseMap defines the robotgo hook mouse's code map

View Source
var Special = keycode.Special

Special defines the special key map

View Source
var WindowsVKCodeToName = make(map[uint16]string)

Reverse mapping from VK code to key name

View Source
var WindowsVKCodes = map[string]uint16{}/* 112 elements not displayed */

Windows Virtual Key Codes (VK codes) - what your system actually sends Source: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Functions

func AddEvent

func AddEvent(key string) bool

AddEvent add event listener,

parameters for the string type, the keyboard corresponding key parameters,

mouse arguments: mleft, center, mright, wheelDown, wheelUp, wheelLeft, wheelRight.

Use "hook.AddEvents()" or "gohook" add asynchronous event listener

func AddEvents

func AddEvents(key string, arr ...string) bool

AddEvents add global event hook

hook.AddEvents("q") hook.AddEvents("q", "ctrl") hook.AddEvents("q", "ctrl", "shift")

func AddMouse

func AddMouse(btn string, x ...int16) bool

AddMouse add mouse event hook

mouse arguments: left, center, right, wheelDown, wheelUp, wheelLeft, wheelRight.

hook.AddMouse("left") hook.AddMouse("left", 100, 100)

func AddMousePos

func AddMousePos(x, y int16) bool

AddMousePos add listen mouse event pos hook

func End

func End()

End removes global event hook

func GetWindowsVKKeyCode added in v0.2.0

func GetWindowsVKKeyCode(name string) (uint16, bool)

GetKeyCode returns the VK code for a given key name

func GetWindowsVKKeyName added in v0.2.0

func GetWindowsVKKeyName(code uint16) string

GetKeyName returns the key name for a given VK code

func Process

func Process(evChan <-chan Event) (out chan bool)

Process return go hook process

func Register

func Register(when Kind, cmds []string, cb func(Event)) error

Register gohook event

func SetLogLevel added in v0.0.3

func SetLogLevel(level DebugLevel)

func Start

func Start() chan Event

Start adds global event hook to OS returns event channel

func StopEvent

func StopEvent()

StopEvent stop the block event listener

Types

type Code

type Code uint16

type DebugLevel

type DebugLevel uint8

type Event

type Event struct {
	Kind     Kind `json:"id"`
	When     time.Time
	Mask     uint16 `json:"mask"`
	Reserved uint16 `json:"reserved"`

	Keycode uint16 `json:"keycode"`
	Rawcode uint16 `json:"rawcode"`
	Keychar rune   `json:"keychar"`

	Button uint16 `json:"button"`
	Clicks uint16 `json:"clicks"`

	X int16 `json:"x"`
	Y int16 `json:"y"`

	Amount    uint16 `json:"amount"`
	Rotation  int32  `json:"rotation"`
	Direction uint8  `json:"direction"`
}

Event Holds a system event

If it's a Keyboard event the relevant fields are: Mask, Keycode, Rawcode, and Keychar, Keychar is probably what you want.

If it's a Mouse event the relevant fields are: Button, Clicks, X, Y, Amount, Rotation and Direction

func (Event) String

func (e Event) String() string

String return formatted hook kind string

type Kind

type Kind uint8

Directories

Path Synopsis
event command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL