robotgo/example/main.go

193 lines
6 KiB
Go
Raw Permalink Normal View History

2017-02-09 16:19:10 +00:00
// Copyright 2016-2017 The go-vgo Project Developers. See the COPYRIGHT
2017-01-02 11:07:45 +00:00
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2016-11-24 10:55:21 +00:00
package main
import (
2016-12-10 15:31:47 +00:00
"fmt"
2016-12-09 15:07:18 +00:00
2016-11-24 10:55:21 +00:00
"github.com/go-vgo/robotgo"
// "go-vgo/robotgo"
)
func main() {
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
// Control the keyboard
////////////////////////////////////////////////////////////////////////////////
2017-03-24 15:31:17 +00:00
robotgo.TypeString("Hello World") // Importing "Hello World"
robotgo.KeyTap("enter") // Press "enter"
2016-11-24 10:55:21 +00:00
robotgo.KeyTap("a", "control")
2017-03-04 16:17:33 +00:00
robotgo.KeyTap("h", "command") // Hide window
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Press "i", "alt", "command" Key combination
2017-01-24 15:16:12 +00:00
robotgo.KeyTap("i", "alt", "command")
2016-11-24 10:55:21 +00:00
arr := []string{"alt", "command"}
robotgo.KeyTap("i", arr)
2017-03-04 16:17:33 +00:00
robotgo.KeyTap("w", "command") // close window
robotgo.KeyTap("m", "command") // minimize window
2016-11-24 10:55:21 +00:00
robotgo.KeyTap("f1", "control")
robotgo.KeyTap("a", "control")
2017-03-24 15:31:17 +00:00
2016-11-24 10:55:21 +00:00
robotgo.KeyToggle("a", "down")
robotgo.KeyToggle("a", "down", "alt")
robotgo.KeyToggle("a", "down", "alt", "command")
robotgo.KeyToggle("enter", "down")
2017-03-24 15:31:17 +00:00
2016-11-24 10:55:21 +00:00
robotgo.TypeString("en")
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
// Control the mouse
////////////////////////////////////////////////////////////////////////////////
2017-03-24 15:31:17 +00:00
robotgo.MoveMouse(100, 200) // Move the mouse to 100, 200
robotgo.MouseClick() // Click the left mouse button
robotgo.MouseClick("right", false) // Click the right mouse button
robotgo.MouseClick("left", true) // double click the left mouse button
2017-03-04 16:17:33 +00:00
robotgo.ScrollMouse(10, "up") // Scrolls the mouse either up
robotgo.MouseToggle("down", "right") // Toggles right mouse button
2017-03-24 15:31:17 +00:00
robotgo.MoveMouseSmooth(100, 200) // Smooth move the mouse to 100, 200
2016-11-24 10:55:21 +00:00
robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0)
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
x, y := robotgo.GetMousePos() // Gets the mouse coordinates
2016-12-10 15:31:47 +00:00
fmt.Println("pos:", x, y)
2016-11-24 10:55:21 +00:00
if x == 456 && y == 586 {
2016-12-10 15:31:47 +00:00
fmt.Println("mouse...", "586")
2016-11-24 10:55:21 +00:00
}
robotgo.MouseToggle("up")
robotgo.MoveMouse(x, y)
robotgo.MoveMouse(100, 200)
for i := 0; i < 1080; i += 1000 {
2016-12-10 15:31:47 +00:00
fmt.Println(i)
2016-11-24 10:55:21 +00:00
robotgo.MoveMouse(800, i)
}
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
// Read the screen
////////////////////////////////////////////////////////////////////////////////
2016-12-10 15:31:47 +00:00
gbitMap := robotgo.BCaptureScreen()
2017-01-16 09:23:32 +00:00
fmt.Println("BCaptureScreen...", gbitMap.Width)
// fmt.Println("...", gbitmap.Width, gbitmap.BytesPerPixel)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Gets the screen width and height
2017-01-24 15:16:12 +00:00
sx, sy := robotgo.GetScreenSize()
2016-12-10 15:31:47 +00:00
fmt.Println("...", sx, sy)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Gets the pixel color at 100, 200.
2017-01-24 15:16:12 +00:00
color := robotgo.GetPixelColor(100, 200)
2016-12-10 15:31:47 +00:00
fmt.Println("color----", color, "-----------------")
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Gets the pixel color at 10, 20.
2017-01-24 15:16:12 +00:00
color2 := robotgo.GetPixelColor(10, 20)
2016-12-10 15:31:47 +00:00
fmt.Println("color---", color2)
2016-11-24 10:55:21 +00:00
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
2016-11-24 10:55:21 +00:00
// Bitmap
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
// Gets all of the screen
2017-01-24 15:16:12 +00:00
abitMap := robotgo.CaptureScreen()
2017-01-16 09:23:32 +00:00
fmt.Println("abitMap...", abitMap)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Gets part of the screen
2017-01-24 15:16:12 +00:00
bitmap := robotgo.CaptureScreen(100, 200, 30, 40)
2016-12-10 15:31:47 +00:00
fmt.Println("CaptureScreen...", bitmap)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Searches for needle in bitmap
2017-01-24 15:16:12 +00:00
fx, fy := robotgo.FindBitmap(bitmap)
2016-12-10 15:31:47 +00:00
fmt.Println("FindBitmap------", fx, fy)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Returns new bitmap object created from a portion of another
2017-01-24 15:16:12 +00:00
bitpos := robotgo.GetPortion(bitmap, 10, 10, 11, 10)
2016-12-10 15:31:47 +00:00
fmt.Println(bitpos)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Creates bitmap from string by bitmap
2016-12-09 15:07:18 +00:00
bitstr := robotgo.TostringBitmap(bitmap)
2017-01-16 09:23:32 +00:00
fmt.Println("bitstr...", bitstr)
2016-11-24 10:55:21 +00:00
2017-01-16 09:23:32 +00:00
// sbitmap := robotgo.BitmapFromstring(bitstr, 2)
// fmt.Println("...", sbitmap)
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Saves image to absolute filepath in the given format
2017-01-24 15:16:12 +00:00
robotgo.SaveBitmap(bitmap, "test.png")
2016-12-09 15:07:18 +00:00
robotgo.SaveBitmap(bitmap, "test31.tif", 1)
2017-03-04 16:17:33 +00:00
robotgo.Convert("test.png", "test.tif") // Convert image
2016-11-24 10:55:21 +00:00
2017-01-16 09:23:32 +00:00
openbit := robotgo.OpenBitmap("test.tif") // open image bitmap
fmt.Println("openBitmap...", openbit)
2016-11-24 10:55:21 +00:00
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
// Global event listener
////////////////////////////////////////////////////////////////////////////////
2017-03-24 15:31:17 +00:00
fmt.Println("--- Please press v---")
2016-11-24 10:55:21 +00:00
eve := robotgo.AddEvent("v")
if eve == 0 {
2017-03-24 15:31:17 +00:00
fmt.Println("--- You press v---", "v")
2016-11-24 10:55:21 +00:00
}
2017-03-24 15:31:17 +00:00
fmt.Println("--- Please press k---")
2016-11-24 10:55:21 +00:00
keve := robotgo.AddEvent("k")
if keve == 0 {
2017-03-24 15:31:17 +00:00
fmt.Println("--- You press k---", "k")
2016-11-24 10:55:21 +00:00
}
2017-03-24 15:31:17 +00:00
fmt.Println("--- Please press f1---")
2017-01-02 11:07:45 +00:00
feve := robotgo.AddEvent("f1")
if feve == 0 {
2017-03-24 15:31:17 +00:00
fmt.Println("You press...", "f1")
2017-01-02 11:07:45 +00:00
}
2017-03-24 15:31:17 +00:00
fmt.Println("--- Please press left mouse button---")
2016-11-24 10:55:21 +00:00
mleft := robotgo.AddEvent("mleft")
if mleft == 0 {
2017-03-24 15:31:17 +00:00
fmt.Println("--- You press left mouse button---", "mleft")
2016-11-24 10:55:21 +00:00
}
2017-01-16 09:23:32 +00:00
mright := robotgo.AddEvent("mright")
if mright == 0 {
2017-03-24 15:31:17 +00:00
fmt.Println("--- You press right mouse button---", "mright")
2017-01-16 09:23:32 +00:00
}
2016-11-24 10:55:21 +00:00
2017-03-04 16:17:33 +00:00
// Stop AddEvent
2017-01-05 12:29:13 +00:00
// robotgo.StopEvent()
2016-11-24 10:55:21 +00:00
2017-01-24 15:16:12 +00:00
////////////////////////////////////////////////////////////////////////////////
// Window Handle
////////////////////////////////////////////////////////////////////////////////
2017-03-04 16:17:33 +00:00
abool := robotgo.ShowAlert("hello", "robotgo") // Show Alert Window
2016-11-24 10:55:21 +00:00
if abool == 0 {
2016-12-10 15:31:47 +00:00
fmt.Println("ok@@@", "ok")
2016-11-24 10:55:21 +00:00
}
robotgo.ShowAlert("hello", "robotgo", "Ok", "Cancel")
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
pid := robotgo.GetPID() // Get the current process id
2017-01-18 13:13:26 +00:00
fmt.Println("pid----", pid)
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
mdata := robotgo.GetActive() // Get current Window Active
2017-03-24 15:31:17 +00:00
hwnd := robotgo.GetHandle() // Get current Window Handle
2016-12-10 15:31:47 +00:00
fmt.Println("hwnd---", hwnd)
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
bhwnd := robotgo.GetBHandle() // Get current Window Handle
2017-02-09 11:04:19 +00:00
fmt.Println("bhwnd---", bhwnd)
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
title := robotgo.GetTitle() // Get current Window title
2016-12-10 15:31:47 +00:00
fmt.Println("title-----", title)
2017-03-24 15:31:17 +00:00
2017-03-04 16:17:33 +00:00
robotgo.CloseWindow() // close current Window
robotgo.SetActive(mdata) // set Window Active
2016-11-24 10:55:21 +00:00
}