Documentation ¶
Index ¶
- type Bot
- func (b *Bot) CVMatchMode() string
- func (b *Bot) CaptureWindow() *image.Image
- func (b *Bot) Click(btn MouseButton, doubleClick bool)
- func (b *Bot) DetectImage(in *image.Image, tmpl *image.Image) (float32, float32, *image.Point, *image.Point, error)
- func (b *Bot) GetPixelColor(x, y int) string
- func (b *Bot) IsKeyDown(key string) bool
- func (b *Bot) KeyTap(key string)
- func (b *Bot) KeysDown() []string
- func (b *Bot) MilliSleep(ms int)
- func (b *Bot) MousePosition() (int, int)
- func (b *Bot) MousePress(btn MouseButton)
- func (b *Bot) MouseRelease(btn MouseButton)
- func (b *Bot) MoveCursor(x, y int)
- func (b *Bot) MoveCursorClick(x, y int, btn MouseButton, doubleClick bool)
- func (b *Bot) MoveCursorRelative(x, y int)
- func (b *Bot) MoveCursorSmoothClick(x, y int, btn MouseButton, doubleClick bool)
- func (b *Bot) OpenImage(path string) (*image.Image, error)
- func (b *Bot) PressKey(key string)
- func (b *Bot) ProcessName() string
- func (b *Bot) RandomInt(min, max int) int
- func (b *Bot) ReleaseKey(key string)
- func (b *Bot) SetCVMatchMode(matchMode gocv.TemplateMatchMode)
- func (b *Bot) SetCursor(x, y int)
- func (b *Bot) ShowDetectedImage(windowTitle string, tmpl *image.Image) error
- func (b *Bot) Sleep(s int)
- func (b *Bot) UpdateWindow() error
- func (b *Bot) Window() *window
- type KeyState
- type MouseButton
- type WindowPidGreaterThenOneError
- type WindowPidNotFoundError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
func NewBot ¶
NewBot create a new bot instance.
Example ¶
package main import ( "fmt" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // return the process name the bot is attached to. fmt.Println(b.ProcessName()) }
Output: gamebot.test.exe
func (*Bot) CVMatchMode ¶
(b *Bot) SetCVMatchMode returns the opencv template matching mode. Reference: [OpenCV Documentation](https://docs.opencv.org/4.6.0/df/dfb/group__imgproc__object.html) for more information.
func (*Bot) CaptureWindow ¶
(b *Bot) CaptureWindow can be used to capture an image of the bot's set window.
func (*Bot) Click ¶
func (b *Bot) Click(btn MouseButton, doubleClick bool)
(b *Bot) Click click the specified mouse button at the current location of the cursor. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for keycodes.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Single click the right mouse button b.Click(gamebot.Right, false) }
Output:
func (*Bot) DetectImage ¶
func (b *Bot) DetectImage(in *image.Image, tmpl *image.Image) (float32, float32, *image.Point, *image.Point, error)
(b *Bot) DetectImage scan the bot's set window to detect images within the window. This function returns the minValue, maxValue, minLocation and maxLocation of the matched image. If an error occurs and error is returned.
The `in` parameter represents the larger image where `tmpl` is the template image to search for.
This function utilized opencv's TmCcoeffNormed algorithm by default. To change the algorithm use the `(b *Bot) SetCVMatchMode()`.
func (*Bot) GetPixelColor ¶
(b *Bot) GetPixelColor return the color of the pixel at the x, y coordinates of the screen.
func (*Bot) IsKeyDown ¶
(b *Bot) IsKeyDown returns true is the specified key is in a down state.
Note that mouse keys are prefixed with the string mouse e.g. mouseleft, mouseright, mousecenter
Example ¶
package main import ( "fmt" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Press the 'w' key to a down state b.PressKey("w") // Check if a key is pushed down. if b.IsKeyDown("w") { fmt.Println("w key is down") } b.ReleaseKey("w") }
Output: w key is down
func (*Bot) KeyTap ¶
(b *Bot) KeyTap will press and release a key. The `args` parameter represents special characters that may need to be pressed alongside the primary key, e.g shift. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for full list of keycodes.
func (*Bot) KeysDown ¶
(b *Bot) KeysDown returns a slice of keys currently in the down position. If there are no keys in a down state this function returns an empty slice.
Example ¶
package main import ( "fmt" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } b.PressKey("w") b.PressKey("a") b.PressKey("s") b.PressKey("d") // Get a slice of keys in down position depressedKeys := b.KeysDown() for _, v := range depressedKeys { fmt.Println(v) } b.ReleaseKey("w") }
Output: w a s d
func (*Bot) MilliSleep ¶
(b *Bot) MilliSleep will pause program operation for the specified number of milliseconds.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Bot will pause operation for 100 milliseconds b.MilliSleep(100) }
Output:
func (*Bot) MousePosition ¶
(b *Bot) MousePosition returns the mouse's current x, y coordinates.
Example ¶
package main import ( "fmt" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } mpx, mpy := b.MousePosition() fmt.Println(mpx, mpy) // ouptut: 0 0 }
Output:
func (*Bot) MousePress ¶
func (b *Bot) MousePress(btn MouseButton)
(b *Bot) MousePress puts the specified mouse button in a down state. To release the button use `(b *Bot) MousePress`.
Example ¶
package main import ( "fmt" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } b.MousePress(gamebot.Left) if b.IsKeyDown("mouseleft") { fmt.Println("left mouse key is down") } b.MouseRelease(gamebot.Left) }
Output: left mouse key is down
func (*Bot) MouseRelease ¶
func (b *Bot) MouseRelease(btn MouseButton)
(b *Bot) MouseRelease puts the specified mouse button in an up state.
Example ¶
package main import ( "fmt" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } b.MousePress(gamebot.Left) if b.IsKeyDown("mouseleft") { fmt.Println("left mouse key is down") } b.MouseRelease(gamebot.Left) }
Output: left mouse key is down
func (*Bot) MoveCursor ¶
(b *Bot) MoveCursor simulates moving the cursor from it's current position to the x, y location on the screen. This simulates human-like movement. If you want to move x, y number of pixels from the current mouses position see `(b *Bot) MoveCursorRelative`. (x: 0, y: 0) represents the top left-hand corner of the screen.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Drag the cursor position to the top-left hand corner b.MoveCursor(0, 0) }
Output:
func (*Bot) MoveCursorClick ¶
func (b *Bot) MoveCursorClick(x, y int, btn MouseButton, doubleClick bool)
(b *Bot) MoveClick puts the cursor at the specified x, y position then clicks the specified mouse button. This movement is nearly instant and does not simulate human-like movement. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for keycodes.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Instantly set the location of the cursor to 100, 100 // and click the left mouse button once. b.MoveCursorClick(100, 100, gamebot.Left, false) // Instantly set the location of the cursor to 100, 100 // and double click the left mouse b.MoveCursorClick(100, 100, gamebot.Left, true) }
Output:
func (*Bot) MoveCursorRelative ¶
(b *Bot) MoveCursorRelative simulates moving the cursor from it's current position by x and y number of pixels. This simulates human-like movement. x represents left and right movement while y represents up and down on the screen.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Drag the cursor position from the current position // 10 pixels left and 10 pixels down. b.MoveCursor(-10, 10) }
Output:
func (*Bot) MoveCursorSmoothClick ¶
func (b *Bot) MoveCursorSmoothClick(x, y int, btn MouseButton, doubleClick bool)
(b *Bot) MoveCursorSmoothClick puts the cursor at the specified x, y position then clicks the specified mouse button. This movement simulates human-like movement. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for keycodes.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Drag the location of the cursor to 100, 100 // and click the left mouse button once. b.MoveCursorClick(100, 100, gamebot.Left, false) // Drag the location of the cursor to 100, 100 // and double click the left mouse b.MoveCursorClick(100, 100, gamebot.Left, true) }
Output:
func (*Bot) OpenImage ¶
(b *Bot) OpenImage will provide an image given a path. An error is returned if there is a problem reading the file.
func (*Bot) PressKey ¶
(b *Bot) PressKey toggles a key on the keyboard. This will put the key in a down state until ReleaseKey is called. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for full list of keycodes.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Press the 'w' key to a down state b.PressKey("w") // Release the 'w' key b.ReleaseKey("w") }
Output:
func (*Bot) ProcessName ¶
(b *Bot) ProcessName() return the bot's currently configured processName.
func (*Bot) RandomInt ¶
(b *Bot) RandomInt generate random integers within a range of min and max values (inclusive).
Example ¶
package main import ( "fmt" "math/rand" "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } rand.Seed(0) // get a random integer between 5 and 10 (he min and max values are inclusive. rn := b.RandomInt(5, 10) fmt.Println(rn) }
Output: 5
func (*Bot) ReleaseKey ¶
(b *Bot) ReleaseKey toggles a key on the keyboard. This will put the key in a down state until ReleaseKey is called. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for full list of keycodes.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Press the 'w' key to a down state b.PressKey("w") // Release the 'w' key b.ReleaseKey("w") }
Output:
func (*Bot) SetCVMatchMode ¶
func (b *Bot) SetCVMatchMode(matchMode gocv.TemplateMatchMode)
(b *Bot) SetCVMatchMode set the opencv template matching mode. Reference: [OpenCV Documentation](https://docs.opencv.org/4.6.0/df/dfb/group__imgproc__object.html) for more information.
func (*Bot) SetCursor ¶
(b *Bot) SetCursor puts the cursor at the specified x, y position. This movement is nearly instant and does not simulate human-like movement.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Instantly set the location of the cursor to 100, 100 b.SetCursor(100, 100) }
Output:
func (*Bot) ShowDetectedImage ¶
(b *Bot) ShowDetectedImage this function opens a window creating a rectangle around the min and max areas of the detected image. Pressing the 'q' key will close this window. This function is mostly used for debugging your bot.
The `tmpl` is the template image to search for within the game window. This function will print the minValue, maxValue, MinLocation, and MaxLocation.
func (*Bot) Sleep ¶
(b *Bot) Sleep will pause program operation for the specified number of seconds.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } // Bot will pause operation for 1 seconds b.Sleep(1) }
Output:
func (*Bot) UpdateWindow ¶
(b *Bot) UpdateWindow sets the bot's window configuration in the event that the window changed size or position.
Example ¶
package main import ( "os" "path/filepath" "github.com/KalebHawkins/gamebot" ) func main() { procName := filepath.Base(os.Args[0]) b, err := gamebot.NewBot(procName) if err != nil { panic(err) } didChange, err := b.Window().Changed() if err != nil { panic(err) } if didChange { b.UpdateWindow() } }
Output:
type MouseButton ¶
type MouseButton string
const ( Left MouseButton = "left" Right MouseButton = "center" Center MouseButton = "right" WheelDown MouseButton = "wheelDown" WheelUp MouseButton = "wheelUp" WheelLeft MouseButton = "wheelLeft" WheelRight MouseButton = "wheelRight" )
type WindowPidGreaterThenOneError ¶
type WindowPidGreaterThenOneError struct {
Applciation string
}
WindowPidGreaterThenOneError is returned when nPids > 1.
func NewWindowPidGreaterThenOneError ¶
func NewWindowPidGreaterThenOneError(processName string) *WindowPidGreaterThenOneError
NewWindowPidGreaterThenOneError is returned when nPids > 1.
func (*WindowPidGreaterThenOneError) Error ¶
func (e *WindowPidGreaterThenOneError) Error() string
func (*WindowPidGreaterThenOneError) Is ¶
func (e *WindowPidGreaterThenOneError) Is(tgt error) bool
type WindowPidNotFoundError ¶
type WindowPidNotFoundError struct {
Applciation string
}
WindowPidNotFoundError is returned when the window PID cannot be found.
func NewWindowPidNotFound ¶
func NewWindowPidNotFound(processName string) *WindowPidNotFoundError
NewWindowPidNotFound is returned if a window PID cannot be found.
func (*WindowPidNotFoundError) Error ¶
func (e *WindowPidNotFoundError) Error() string
func (*WindowPidNotFoundError) Is ¶
func (e *WindowPidNotFoundError) Is(tgt error) bool