### Start gohook Event Listener in Go Source: https://context7.com/robotn/gohook/llms.txt Starts the global event hook and returns a channel that receives all keyboard and mouse events. This function is the foundation for event processing and callback registration. It requires the 'github.com/robotn/gohook' package. The function returns a channel of hook.Event, and it's recommended to call hook.End() using defer. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Start the event listener evChan := hook.Start() defer hook.End() fmt.Println("Listening for events... Press 'q' to quit") for ev := range evChan { // Each event contains Kind, Keycode, Rawcode, Keychar for keyboard // or Button, X, Y, Clicks for mouse events fmt.Println("Event:", ev) // Exit when 'q' is pressed if ev.Keychar == 'q' { break } } } ``` -------------------------------- ### Install gohook using Go Modules Source: https://github.com/robotn/gohook/blob/master/README.md This snippet demonstrates how to import the gohook library into a Go project using Go modules, which is the standard for Go 1.11 and later. Ensure you have Go module support enabled in your project. ```Go import "github.com/robotn/gohook" ``` -------------------------------- ### Registering and Processing Keyboard and Mouse Events in Go Source: https://github.com/robotn/gohook/blob/master/README.md This Go code snippet illustrates how to use the gohook library to register event listeners for keyboard and mouse actions. It shows how to set up a listener for a specific key combination (Ctrl+Shift+Q) to terminate the hook, and also how to capture individual key down and key up events for the 'w' key. The `hook.Start()` function initiates the event loop, and `<-hook.Process(s)` waits for events to be processed. ```Go 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("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) } } ``` -------------------------------- ### Handle Keyboard and Mouse Events with Event Struct (Go) Source: https://context7.com/robotn/gohook/llms.txt Demonstrates how to capture and process keyboard and mouse events using the `Event` struct. The `hook.Start()` function initiates event capturing, returning a channel from which `Event` objects can be read. The `Event.Kind` field determines the type of event, allowing for specific handling of key presses, mouse clicks, movements, and wheel actions. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { evChan := hook.Start() defer hook.End() for ev := range evChan { switch ev.Kind { case hook.KeyDown, hook.KeyHold, hook.KeyUp: // Keyboard event fields fmt.Printf("Keyboard Event:\n") fmt.Printf(" Kind: %d, Keycode: %d, Rawcode: %d, Keychar: %c\n", ev.Kind, ev.Keycode, ev.Rawcode, ev.Keychar) case hook.MouseDown, hook.MouseUp, hook.MouseHold: // Mouse click event fields fmt.Printf("Mouse Click Event:\n") fmt.Printf(" Kind: %d, Button: %d, X: %d, Y: %d, Clicks: %d\n", ev.Kind, ev.Button, ev.X, ev.Y, ev.Clicks) case hook.MouseMove, hook.MouseDrag: // Mouse movement event fields fmt.Printf("Mouse Move Event: X: %d, Y: %d\n", ev.X, ev.Y) case hook.MouseWheel: // Mouse wheel event fields fmt.Printf("Mouse Wheel Event:\n") fmt.Printf(" Amount: %d, Rotation: %d, Direction: %d\n", ev.Amount, ev.Rotation, ev.Direction) } if ev.Keychar == 'q' { break } } } ``` -------------------------------- ### Utilize Event Constants and Maps for Input Handling (Go) Source: https://context7.com/robotn/gohook/llms.txt This code snippet demonstrates the usage of constants and maps provided by the gohook library for identifying event types and key/mouse button identifiers. It shows how to access event kind constants (e.g., `hook.KeyDown`), mouse wheel directions, and mappings for mouse buttons and keyboard keys. These constants and maps are crucial for filtering and interpreting captured input events. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Print version fmt.Println("gohook version:", hook.Version) // Event Kind constants fmt.Println("\nEvent Types:") fmt.Printf(" KeyDown: %d\n", hook.KeyDown) fmt.Printf(" KeyHold: %d\n", hook.KeyHold) fmt.Printf(" KeyUp: %d\n", hook.KeyUp) fmt.Printf(" MouseDown: %d\n", hook.MouseDown) fmt.Printf(" MouseHold: %d\n", hook.MouseHold) fmt.Printf(" MouseUp: %d\n", hook.MouseUp) fmt.Printf(" MouseMove: %d\n", hook.MouseMove) fmt.Printf(" MouseDrag: %d\n", hook.MouseDrag) fmt.Printf(" MouseWheel: %d\n", hook.MouseWheel) // Mouse wheel constants fmt.Println("\nWheel Direction:") fmt.Printf(" WheelUp: %d\n", hook.WheelUp) fmt.Printf(" WheelDown: %d\n", hook.WheelDown) // Mouse button map fmt.Println("\nMouse Buttons:") fmt.Printf(" left: %d\n", hook.MouseMap["left"]) fmt.Printf(" right: %d\n", hook.MouseMap["right"]) fmt.Printf(" center: %d\n", hook.MouseMap["center"]) } ``` ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Using Keycode map for keyboard keys fmt.Println("Keyboard Keycodes:") fmt.Printf(" ctrl: %d\n", hook.Keycode["ctrl"]) fmt.Printf(" shift: %d\n", hook.Keycode["shift"]) fmt.Printf(" alt: %d\n", hook.Keycode["alt"]) fmt.Printf(" enter: %d\n", hook.Keycode["enter"]) fmt.Printf(" escape: %d\n", hook.Keycode["escape"]) fmt.Printf(" space: %d\n", hook.Keycode["space"]) fmt.Printf(" a: %d\n", hook.Keycode["a"]) fmt.Printf(" f1: %d\n", hook.Keycode["f1"]) // Using MouseMap for mouse buttons fmt.Println("\nMouse Button Codes:") fmt.Printf(" left: %d\n", hook.MouseMap["left"]) fmt.Printf(" right: %d\n", hook.MouseMap["right"]) fmt.Printf(" center: %d\n", hook.MouseMap["center"]) fmt.Printf(" wheelUp: %d\n", hook.MouseMap["wheelUp"]) fmt.Printf(" wheelDown: %d\n", hook.MouseMap["wheelDown"]) // Use in event filtering evChan := hook.Start() defer hook.End() for ev := range evChan { if ev.Kind == hook.KeyDown && ev.Keycode == hook.Keycode["escape"] { fmt.Println("Escape pressed - exiting") break } if ev.Kind == hook.MouseDown && ev.Button == hook.MouseMap["left"] { fmt.Printf("Left click at (%d, %d)\n", ev.X, ev.Y) } } } ``` -------------------------------- ### Register Callbacks for Events with gohook in Go Source: https://context7.com/robotn/gohook/llms.txt Registers callback functions for specific keyboard or mouse events, optionally with key combinations. It supports various event types like KeyDown, KeyUp, MouseDown, MouseMove, etc. The 'github.com/robotn/gohook' package is necessary. Callbacks can be triggered by single keys, key combinations, or mouse actions. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Register Ctrl+Shift+Q to exit fmt.Println("Press Ctrl+Shift+Q to stop") hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) { fmt.Println("Ctrl+Shift+Q pressed - exiting") hook.End() }) // Register single key 'w' for KeyDown hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) { fmt.Println("KeyDown: w") }) // Register single key 'w' for KeyUp hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) { fmt.Println("KeyUp: w") }) // Register mouse left button click hook.Register(hook.MouseDown, []string{}, func(e hook.Event) { if e.Button == hook.MouseMap["left"] { fmt.Printf("Left click at (%d, %d)\n", e.X, e.Y) } else if e.Button == hook.MouseMap["right"] { fmt.Printf("Right click at (%d, %d)\n", e.X, e.Y) } }) // Start event loop and process registered callbacks s := hook.Start() <-hook.Process(s) } ``` -------------------------------- ### AddEvents: Listen for Key Combinations Source: https://context7.com/robotn/gohook/llms.txt Adds a blocking event listener for key combinations. Execution pauses until all specified modifier keys are held down and the main key is released. Supports combinations like Ctrl+Q or Ctrl+Shift+Q. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Wait for single key fmt.Println("Press 'q' to continue...") hook.AddEvents("q") fmt.Println("'q' was pressed") // Wait for Ctrl+Q fmt.Println("Press Ctrl+Q to continue...") hook.AddEvents("q", "ctrl") fmt.Println("Ctrl+Q was pressed") // Wait for Ctrl+Shift+Q fmt.Println("Press Ctrl+Shift+Q to continue...") hook.AddEvents("q", "ctrl", "shift") fmt.Println("Ctrl+Shift+Q was pressed") fmt.Println("All key combinations detected!") } ``` -------------------------------- ### RawcodetoKeychar: Convert Raw Keyboard Codes to Key Names Source: https://context7.com/robotn/gohook/llms.txt Converts a raw keyboard event code into its corresponding human-readable key character string. This function is platform-aware, handling differences between operating systems like macOS and others. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { evChan := hook.Start() defer hook.End() for ev := range evChan { if ev.Kind == hook.KeyDown { // Convert rawcode to readable key name keyName := hook.RawcodetoKeychar(ev.Rawcode) fmt.Printf("Key pressed: %s (rawcode: %d)\n", keyName, ev.Rawcode) } if ev.Keychar == 'q' { break } } } // Output: // Key pressed: a (rawcode: 65) // Key pressed: enter (rawcode: 13) // Key pressed: shift (rawcode: 16) ``` -------------------------------- ### AddMousePos: Listen for Mouse Movement to Specific Position Source: https://context7.com/robotn/gohook/llms.txt Adds a blocking event listener that waits until the mouse cursor moves to a specified screen position. Execution pauses until the mouse reaches the target coordinates. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { fmt.Println("Move mouse to position (100, 100)...") hook.AddMousePos(100, 100) fmt.Println("Mouse reached position (100, 100)") fmt.Println("Move mouse to position (500, 300)...") hook.AddMousePos(500, 300) fmt.Println("Mouse reached position (500, 300)") } ``` -------------------------------- ### AddMouse: Listen for Mouse Button Clicks Source: https://context7.com/robotn/gohook/llms.txt Adds a blocking event listener for mouse button clicks. It can wait for any click of a specified button (e.g., 'left', 'right', 'center') or a click at specific screen coordinates. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Wait for any left mouse click fmt.Println("Click left mouse button anywhere...") hook.AddMouse("left") fmt.Println("Left button clicked") // Wait for left click at specific position (100, 100) fmt.Println("Move to position (100, 100) and click left button...") hook.AddMouse("left", 100, 100) fmt.Println("Left button clicked at (100, 100)") // Wait for right mouse click fmt.Println("Click right mouse button...") hook.AddMouse("right") fmt.Println("Right button clicked") // Wait for center (middle) button fmt.Println("Click middle mouse button...") hook.AddMouse("center") fmt.Println("Middle button clicked") } ``` -------------------------------- ### Convert Key Character to Raw Keyboard Code (Go) Source: https://context7.com/robotn/gohook/llms.txt Converts a human-readable key character string (e.g., 'escape', 'enter') into its corresponding raw keyboard code. This function is platform-aware, handling differences between operating systems like macOS and others. It's useful for mapping user input to system-level key events. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Get rawcodes for specific keys escapeCode := hook.KeychartoRawcode("escape") enterCode := hook.KeychartoRawcode("enter") spaceCode := hook.KeychartoRawcode("spacebar") f1Code := hook.KeychartoRawcode("f1") fmt.Printf("escape rawcode: %d\n", escapeCode) fmt.Printf("enter rawcode: %d\n", enterCode) fmt.Printf("spacebar rawcode: %d\n", spaceCode) fmt.Printf("f1 rawcode: %d\n", f1Code) } // Output (Linux/Windows): // escape rawcode: 27 // enter rawcode: 13 // spacebar rawcode: 32 // f1 rawcode: 112 ``` -------------------------------- ### Process gohook Events and Trigger Callbacks in Go Source: https://context7.com/robotn/gohook/llms.txt Processes events from the event channel returned by hook.Start() and triggers any registered callbacks. This function blocks until hook.End() is called, returning a channel that signals when the event loop has finished. It requires the 'github.com/robotn/gohook' package and should be used after registering callbacks. ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Register callbacks before starting hook.Register(hook.KeyDown, []string{"escape"}, func(e hook.Event) { fmt.Println("Escape pressed - stopping") hook.End() }) hook.Register(hook.KeyDown, []string{"a", "ctrl"}, func(e hook.Event) { fmt.Println("Ctrl+A pressed") }) // Start the event stream s := hook.Start() // Process blocks until End() is called done := hook.Process(s) fmt.Println("Waiting for events...") <-done fmt.Println("Done processing") } ``` -------------------------------- ### AddEvent: Listen for Single Keyboard or Mouse Events Source: https://context7.com/robotn/gohook/llms.txt Adds a blocking event listener that waits for a specific key press or mouse button click. Execution pauses until the specified event occurs. Supports various keyboard keys (e.g., 'v', 'f1') and mouse buttons (e.g., 'mleft', 'mright'). ```go package main import ( "fmt" hook "github.com/robotn/gohook" ) func main() { // Wait for 'v' key press (blocking) fmt.Println("Press 'v' to continue...") if hook.AddEvent("v") { fmt.Println("You pressed 'v'") } // Wait for function key fmt.Println("Press F1 to continue...") if hook.AddEvent("f1") { fmt.Println("You pressed F1") } // Wait for left mouse button fmt.Println("Click left mouse button to continue...") if hook.AddEvent("mleft") { fmt.Println("Left mouse button clicked") } // Wait for right mouse button fmt.Println("Click right mouse button to continue...") if hook.AddEvent("mright") { fmt.Println("Right mouse button clicked") } } ``` -------------------------------- ### Stop gohook Global Event Hook in Go Source: https://context7.com/robotn/gohook/llms.txt Stops the global event hook, cleans up resources, and closes the event channel. This function should be called when event listening is no longer needed, typically using defer after hook.Start(). It ensures that the system resources used by the hook are released properly. The 'github.com/robotn/gohook' package is required. ```go package main import ( "fmt" "time" hook "github.com/robotn/gohook" ) func main() { evChan := hook.Start() // Process events for 5 seconds go func() { for ev := range evChan { fmt.Println("Event:", ev) } fmt.Println("Event channel closed") }() time.Sleep(5 * time.Second) // Stop the hook and clean up hook.End() fmt.Println("Hook ended") } ``` -------------------------------- ### StopEvent: Cancel Blocking Event Listeners Source: https://context7.com/robotn/gohook/llms.txt Stops an active blocking event listener initiated by functions like AddEvent. This is useful for canceling a wait operation, potentially from another goroutine, after a timeout or other condition. ```go package main import ( "fmt" "time" hook "github.com/robotn/gohook" ) func main() { // Start a goroutine that will cancel after 5 seconds go func() { time.Sleep(5 * time.Second) fmt.Println("Timeout - stopping event listener") hook.StopEvent() }() fmt.Println("Press 'q' within 5 seconds...") if hook.AddEvent("q") { fmt.Println("You pressed 'q' in time") } else { fmt.Println("Event was cancelled") } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.