mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-09 01:53:56 +00:00
use gops to simplify code
This commit is contained in:
parent
116e0c1843
commit
1aff63a651
107
robotgo.go
107
robotgo.go
@ -51,7 +51,7 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
// "fmt"
|
// "fmt"
|
||||||
"image"
|
"image"
|
||||||
"os"
|
// "os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -64,7 +64,7 @@ import (
|
|||||||
|
|
||||||
"github.com/go-vgo/robotgo/clipboard"
|
"github.com/go-vgo/robotgo/clipboard"
|
||||||
hook "github.com/robotn/gohook"
|
hook "github.com/robotn/gohook"
|
||||||
"github.com/shirou/gopsutil/process"
|
ps "github.com/vcaesar/gops"
|
||||||
"github.com/vcaesar/imgo"
|
"github.com/vcaesar/imgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -1591,20 +1591,12 @@ func internalGetBounds(pid int32, hwnd int) (int, int, int, int) {
|
|||||||
|
|
||||||
// Pids get the all process id
|
// Pids get the all process id
|
||||||
func Pids() ([]int32, error) {
|
func Pids() ([]int32, error) {
|
||||||
var ret []int32
|
return ps.Pids()
|
||||||
pid, err := process.Pids()
|
|
||||||
if err != nil {
|
|
||||||
return ret, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return pid, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PidExists determine whether the process exists
|
// PidExists determine whether the process exists
|
||||||
func PidExists(pid int32) (bool, error) {
|
func PidExists(pid int32) (bool, error) {
|
||||||
abool, err := process.PidExists(pid)
|
return ps.PidExists(pid)
|
||||||
|
|
||||||
return abool, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is64Bit determine whether the sys is 64bit
|
// Is64Bit determine whether the sys is 64bit
|
||||||
@ -1622,26 +1614,11 @@ type Nps struct {
|
|||||||
// Process get the all process struct
|
// Process get the all process struct
|
||||||
func Process() ([]Nps, error) {
|
func Process() ([]Nps, error) {
|
||||||
var npsArr []Nps
|
var npsArr []Nps
|
||||||
|
nps, err := ps.Process()
|
||||||
pid, err := process.Pids()
|
for i := 0; i < len(nps); i++ {
|
||||||
if err != nil {
|
|
||||||
return npsArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(pid); i++ {
|
|
||||||
nps, err := process.NewProcess(pid[i])
|
|
||||||
if err != nil {
|
|
||||||
return npsArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
names, err := nps.Name()
|
|
||||||
if err != nil {
|
|
||||||
return npsArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
np := Nps{
|
np := Nps{
|
||||||
pid[i],
|
nps[i].Pid,
|
||||||
names,
|
nps[i].Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
npsArr = append(npsArr, np)
|
npsArr = append(npsArr, np)
|
||||||
@ -1652,81 +1629,24 @@ func Process() ([]Nps, error) {
|
|||||||
|
|
||||||
// FindName find the process name by the process id
|
// FindName find the process name by the process id
|
||||||
func FindName(pid int32) (string, error) {
|
func FindName(pid int32) (string, error) {
|
||||||
nps, err := process.NewProcess(pid)
|
return ps.FindName(pid)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
names, err := nps.Name()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return names, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindNames find the all process name
|
// FindNames find the all process name
|
||||||
func FindNames() ([]string, error) {
|
func FindNames() ([]string, error) {
|
||||||
var strArr []string
|
return ps.FindNames()
|
||||||
pid, err := process.Pids()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return strArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(pid); i++ {
|
|
||||||
nps, err := process.NewProcess(pid[i])
|
|
||||||
if err != nil {
|
|
||||||
return strArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
names, err := nps.Name()
|
|
||||||
if err != nil {
|
|
||||||
return strArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
strArr = append(strArr, names)
|
|
||||||
return strArr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return strArr, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindIds finds the all processes named with a subset
|
// FindIds finds the all processes named with a subset
|
||||||
// of "name" (case insensitive),
|
// of "name" (case insensitive),
|
||||||
// return matched IDs.
|
// return matched IDs.
|
||||||
func FindIds(name string) ([]int32, error) {
|
func FindIds(name string) ([]int32, error) {
|
||||||
var pids []int32
|
return ps.FindIds(name)
|
||||||
nps, err := Process()
|
|
||||||
if err != nil {
|
|
||||||
return pids, err
|
|
||||||
}
|
|
||||||
|
|
||||||
name = strings.ToLower(name)
|
|
||||||
for i := 0; i < len(nps); i++ {
|
|
||||||
psname := strings.ToLower(nps[i].Name)
|
|
||||||
abool := strings.Contains(psname, name)
|
|
||||||
if abool {
|
|
||||||
pids = append(pids, nps[i].Pid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pids, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindPath find the process path by the process pid
|
// FindPath find the process path by the process pid
|
||||||
func FindPath(pid int32) (string, error) {
|
func FindPath(pid int32) (string, error) {
|
||||||
nps, err := process.NewProcess(pid)
|
return ps.FindPath(pid)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := nps.Exe()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return f, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func internalActive(pid int32, hwnd int) {
|
func internalActive(pid int32, hwnd int) {
|
||||||
@ -1756,6 +1676,5 @@ func ActiveName(name string) error {
|
|||||||
|
|
||||||
// Kill kill the process by PID
|
// Kill kill the process by PID
|
||||||
func Kill(pid int32) error {
|
func Kill(pid int32) error {
|
||||||
ps := os.Process{Pid: int(pid)}
|
return ps.Kill(pid)
|
||||||
return ps.Kill()
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user