hook

package module
v0.41.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: MIT Imports: 9 Imported by: 110

README

gohook

Build Status CircleCI Status Appveyor Go Report Card GoDoc

Requirements (Linux):

Robotgo-requirements-event

Install:

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

import "github.com/robotn/gohook"

Examples:

package main

import (
	"fmt"

	hook "github.com/robotn/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("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.40.0.123, Sierra Nevada!"

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

	KeyDown = 3
	KeyHold = 4
	KeyUp   = 5

	MouseUp    = 6
	MouseHold  = 7
	MouseDown  = 8
	MouseMove  = 9
	MouseDrag  = 10
	MouseWheel = 11

	FakeEvent = 12

	// Keychar could be v
	CharUndefined = 0xFFFF
	WheelUp       = -1
	WheelDown     = 1
)

Variables

View Source
var Keycode = keycode.Keycode

Keycode defines the robotgo hook key's code map

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

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 added in v0.40.0

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 added in v0.40.0

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 added in v0.40.0

func AddMousePos(x, y int16) bool

AddMousePos add listen mouse event pos hook

func End

func End()

End removes global event hook

func KeychartoRawcode

func KeychartoRawcode(kc string) uint16

KeychartoRawcode key char to rawcode

func Process

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

Process return go hook process

func RawcodetoKeychar

func RawcodetoKeychar(r uint16) string

RawcodetoKeychar rawcode to keychar

func Register

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

Register register gohook event

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 Event

type Event struct {
	Kind     uint8 `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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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