robotgo/examples/window/main.go

162 lines
3.1 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2016-2025 AtomAI, All rights reserved.
//
// See the COPYRIGHT file at the top-level directory of this distribution and at
2017-05-25 14:25:28 +00:00
// 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>
//
// This file may not be copied, modified, or distributed
2017-05-25 14:25:28 +00:00
// except according to those terms.
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
// "go-vgo/robotgo"
)
2018-10-07 16:15:03 +00:00
func alert() {
2017-07-02 03:52:37 +00:00
// show Alert Window
abool := robotgo.Alert("hello", "robotgo")
2020-11-05 14:57:51 +00:00
if abool {
2017-05-25 14:25:28 +00:00
fmt.Println("ok@@@", "ok")
}
robotgo.Alert("hello", "robotgo", "Ok", "Cancel")
2018-10-07 16:15:03 +00:00
}
func get() {
2017-07-02 03:52:37 +00:00
// get the current process id
pid := robotgo.GetPid()
2017-05-25 14:25:28 +00:00
fmt.Println("pid----", pid)
2017-07-02 03:52:37 +00:00
// get current Window Active
mdata := robotgo.GetActive()
2017-05-25 14:25:28 +00:00
2017-07-02 03:52:37 +00:00
// get current Window Handle
hwnd := robotgo.GetHandle()
2017-05-25 14:25:28 +00:00
fmt.Println("hwnd---", hwnd)
2017-07-02 03:52:37 +00:00
// get current Window title
title := robotgo.GetTitle()
2017-05-25 14:25:28 +00:00
fmt.Println("title-----", title)
2017-07-02 03:52:37 +00:00
// set Window Active
robotgo.SetActive(mdata)
2018-10-07 16:15:03 +00:00
}
2017-07-01 15:07:30 +00:00
2018-10-07 16:15:03 +00:00
func findIds() {
2017-07-02 03:52:37 +00:00
// find the process id by the process name
2017-07-01 15:07:30 +00:00
fpid, err := robotgo.FindIds("Google")
if err != nil {
fmt.Println(err)
return
}
if len(fpid) > 0 {
robotgo.KeyTap("a", fpid[0])
robotgo.TypeStr("Hi galaxy!", fpid[0])
robotgo.KeyToggle("a", fpid[0], "cmd")
robotgo.KeyToggle("a", fpid[0], "cmd", "up")
}
fmt.Println("pids...", fpid)
if len(fpid) > 0 {
err = robotgo.ActivePid(fpid[0])
if err != nil {
fmt.Println(err)
}
2018-02-03 09:42:38 +00:00
tl := robotgo.GetTitle(fpid[0])
fmt.Println("pid[0] title is: ", tl)
2018-08-23 14:49:39 +00:00
x, y, w, h := robotgo.GetBounds(fpid[0])
fmt.Println("GetBounds is: ", x, y, w, h)
2018-08-16 12:46:29 +00:00
// Windows
// hwnd := robotgo.FindWindow("google")
// hwnd := robotgo.GetHWND()
robotgo.MinWindow(fpid[0])
robotgo.MaxWindow(fpid[0])
robotgo.CloseWindow(fpid[0])
2018-07-29 17:23:58 +00:00
robotgo.Kill(fpid[0])
2017-07-01 15:07:30 +00:00
}
2018-10-07 16:15:03 +00:00
}
2017-07-01 15:07:30 +00:00
2020-04-07 15:30:29 +00:00
func active() {
robotgo.ActivePid(100)
2020-04-07 15:30:29 +00:00
// robotgo.Sleep(2)
robotgo.ActiveName("code")
robotgo.Sleep(1)
robotgo.ActiveName("chrome")
}
2018-10-07 16:15:03 +00:00
func findName() {
// find the process name by the process id
name, err := robotgo.FindName(100)
if err == nil {
fmt.Println("name: ", name)
}
// find the all process name
names, err := robotgo.FindNames()
if err == nil {
fmt.Println("name: ", names)
}
2019-05-13 13:59:13 +00:00
p, err := robotgo.FindPath(100)
if err == nil {
fmt.Println("path: ", p)
}
2018-10-07 16:15:03 +00:00
}
2018-01-18 13:39:27 +00:00
2018-10-07 16:15:03 +00:00
func ps() {
2017-07-02 03:52:37 +00:00
// determine whether the process exists
2017-07-01 15:07:30 +00:00
isExist, err := robotgo.PidExists(100)
2018-02-03 09:42:38 +00:00
if err == nil && isExist {
2017-07-01 15:07:30 +00:00
fmt.Println("pid exists is", isExist)
2018-02-03 09:42:38 +00:00
robotgo.Kill(100)
2017-07-01 15:07:30 +00:00
}
2017-07-02 03:52:37 +00:00
// get the all process id
2017-07-01 15:07:30 +00:00
pids, err := robotgo.Pids()
if err == nil {
fmt.Println("pids: ", pids)
}
2017-07-02 03:52:37 +00:00
// get the all process struct
2017-07-01 15:07:30 +00:00
ps, err := robotgo.Process()
if err == nil {
fmt.Println("process: ", ps)
}
2018-10-07 16:15:03 +00:00
}
func window() {
////////////////////////////////////////////////////////////////////////////////
// Window Handle
////////////////////////////////////////////////////////////////////////////////
alert()
2020-04-07 15:30:29 +00:00
//
2018-10-07 16:15:03 +00:00
get()
findIds()
2020-04-07 15:30:29 +00:00
active()
2018-10-07 16:15:03 +00:00
findName()
2020-04-07 15:30:29 +00:00
//
2018-10-07 16:15:03 +00:00
ps()
2017-07-02 04:31:58 +00:00
// close current Window
robotgo.CloseWindow()
2017-05-25 14:25:28 +00:00
}
2018-07-13 11:23:09 +00:00
func main() {
window()
}