goinput

package module
v0.0.0-...-02364a5 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Go-Input

A Go package for handling user input from various sources (keyboard, mouse, console).

Features

  • Keyboard input monitoring
  • Mouse input monitoring
  • Console-based user input for strings, integers, floats, booleans, and choice selections
  • Multiple polling rate options (Eco, Normal, Game) for different application needs

Installation

go get github.com/AndrewDonelson/go-input

Usage

Basic Console Input
package main

import (
    "fmt"
    "github.com/AndrewDonelson/go-input/userinput"
)

func main() {
    ui := userinput.New()
    
    name, err := ui.ReadString("Enter your name")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    
    age, err := ui.ReadInt("Enter your age")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    
    fmt.Printf("Hello, %s! You are %d years old.\n", name, age)
}
Using Keyboard and Mouse Monitoring
package main

import (
    "fmt"
    "time"
    "github.com/AndrewDonelson/go-input"
    "github.com/AndrewDonelson/go-input/keyboard"
    "github.com/AndrewDonelson/go-input/mouse"
)

func main() {
    kb := goinput.NewKeyboard()
    m := goinput.NewMouse()
    
    // Set up a key state
    kb.SetState(keyboard.A, keyboard.Down)
    
    // Check a key state
    if kb.Down(keyboard.A) {
        fmt.Println("Key A is down")
    }
    
    // Set up a mouse button state
    m.SetState(mouse.Left, mouse.Down)
    
    // Check a mouse button state
    if m.Down(mouse.Left) {
        fmt.Println("Left mouse button is down")
    }
}

License

BSD-style - See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultKeyboard allows immediate access to default keyboard input
	DefaultKeyboard = NewKeyboard()

	// DefaultMouse allows immediate access to default mouse input
	DefaultMouse = NewMouse()

	// DefaultAsk provides a default instance of the questions.Ask struct
	DefaultAsk = questions.New(DefaultKeyboard, DefaultMouse)
)

Functions

func NewKeyboard

func NewKeyboard() *keyboard.Watcher

NewKeyboard returns a new keyboard watcher that can be used to monitor keyboard input. The returned watcher is configured with the default polling mode of Eco.

func NewMouse

func NewMouse() *mouse.Watcher

NewMouse returns a new mouse watcher that can be used to monitor mouse input. The returned watcher is configured with the default polling mode of Eco.

Types

type InputState

type InputState struct {
	Keyboard keyboard.State
	Mouse    mouse.State
}

InputState holds the current state of the keyboard and mouse input.

type Polling

type Polling uint8

Polling represents the update speed for detecting input.

const (
	Eco    Polling = iota // 10x/sec
	Normal                // 30x/sec
	Game                  // 60x/sec
)

Polling mode constants, Eco will check the input ten times per second. Normal will check thirty times per seconds and Game will update at sixty times per second. Default is Eco which is suggested for standard console or user input that does not require a high update interval.

Directories

Path Synopsis
cmd
goinput command
internal
common
Package common provides shared utilities and types for internal package use.
Package common provides shared utilities and types for internal package use.
Package keyboard implements various keyboard related data types.
Package keyboard implements various keyboard related data types.
Package mouse implements various mouse related data types.
Package mouse implements various mouse related data types.

Jump to

Keyboard shortcuts

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