Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Created November 11, 2023 04:27
Show Gist options
  • Save sangheestyle/2cf006e4e201e6c4f3314bc5b23acb45 to your computer and use it in GitHub Desktop.
Save sangheestyle/2cf006e4e201e6c4f3314bc5b23acb45 to your computer and use it in GitHub Desktop.
Click given image on screen.
package main
import (
"fmt"
"log"
"os"
"github.com/go-vgo/robotgo"
"github.com/vcaesar/bitmap"
)
func main() {
fmt.Println(">>> start")
name := "./needle.png"
// Check if the file exists
if _, err := os.Stat(name); os.IsNotExist(err) {
log.Fatalf("File does not exist: %s", name)
}
bit := bitmap.Open(name)
if bit == nil {
log.Fatalf("Failed to open bitmap file or file is not a valid bitmap")
}
defer robotgo.FreeBitmap(bit)
fmt.Print(">>> find bitmap: ")
// Find the bitmap on the screen
fx, fy := bitmap.Find(bit)
// Check if the bitmap is found
if fx == -1 || fy == -1 {
fmt.Println("Bitmap not found.")
} else {
fmt.Printf("FindBitmap at position (%d, %d)\n", fx, fy)
// Move the mouse to the found position and click
robotgo.MoveSmooth(fx, fy)
robotgo.Click()
}
}
use autopilot::bitmap::{capture_screen, Bitmap};
use autopilot::mouse::{click, move_to, Button};
use image::open;
use std::{thread, time};
fn main() {
let screen = capture_screen().expect("Failed to capture screen");
let needle_path = "./needle.png"; // Replace with your image path
let needle_image = open(needle_path).expect("Failed to open needle image");
let needle_bitmap = Bitmap::new(needle_image, None);
if let Some(point) = screen.find_bitmap(&needle_bitmap, Some(0.0), None, None) {
println!("Bitmap found at {:?}", point);
move_to(point).expect("Failed to move mouse");
// Adding a delay before the click
thread::sleep(time::Duration::from_millis(500));
click(Button::Left, Some(1)); // Clicks left button once
} else {
println!("Bitmap not found");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment