main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. package main
  11. import (
  12. "fmt"
  13. "github.com/go-vgo/robotgo"
  14. // "go-vgo/robotgo"
  15. )
  16. func alert() {
  17. // show Alert Window
  18. abool := robotgo.Alert("hello", "robotgo")
  19. if abool {
  20. fmt.Println("ok@@@", "ok")
  21. }
  22. robotgo.Alert("hello", "robotgo", "Ok", "Cancel")
  23. }
  24. func get() {
  25. // get the current process id
  26. pid := robotgo.GetPid()
  27. fmt.Println("pid----", pid)
  28. // get current Window Active
  29. mdata := robotgo.GetActive()
  30. // get current Window Handle
  31. hwnd := robotgo.GetHandle()
  32. fmt.Println("hwnd---", hwnd)
  33. // get current Window title
  34. title := robotgo.GetTitle()
  35. fmt.Println("title-----", title)
  36. // set Window Active
  37. robotgo.SetActive(mdata)
  38. }
  39. func findIds() {
  40. // find the process id by the process name
  41. fpid, err := robotgo.FindIds("Google")
  42. if err != nil {
  43. fmt.Println(err)
  44. return
  45. }
  46. if len(fpid) > 0 {
  47. robotgo.KeyTap("a", fpid[0])
  48. robotgo.TypeStr("Hi galaxy!", fpid[0])
  49. robotgo.KeyToggle("a", fpid[0], "cmd")
  50. robotgo.KeyToggle("a", fpid[0], "cmd", "up")
  51. }
  52. fmt.Println("pids...", fpid)
  53. if len(fpid) > 0 {
  54. err = robotgo.ActivePid(fpid[0])
  55. if err != nil {
  56. fmt.Println(err)
  57. }
  58. tl := robotgo.GetTitle(fpid[0])
  59. fmt.Println("pid[0] title is: ", tl)
  60. x, y, w, h := robotgo.GetBounds(fpid[0])
  61. fmt.Println("GetBounds is: ", x, y, w, h)
  62. // Windows
  63. // hwnd := robotgo.FindWindow("google")
  64. // hwnd := robotgo.GetHWND()
  65. robotgo.MinWindow(fpid[0])
  66. robotgo.MaxWindow(fpid[0])
  67. robotgo.CloseWindow(fpid[0])
  68. robotgo.Kill(fpid[0])
  69. }
  70. }
  71. func active() {
  72. robotgo.ActivePid(100)
  73. // robotgo.Sleep(2)
  74. robotgo.ActiveName("code")
  75. robotgo.Sleep(1)
  76. robotgo.ActiveName("chrome")
  77. }
  78. func findName() {
  79. // find the process name by the process id
  80. name, err := robotgo.FindName(100)
  81. if err == nil {
  82. fmt.Println("name: ", name)
  83. }
  84. // find the all process name
  85. names, err := robotgo.FindNames()
  86. if err == nil {
  87. fmt.Println("name: ", names)
  88. }
  89. p, err := robotgo.FindPath(100)
  90. if err == nil {
  91. fmt.Println("path: ", p)
  92. }
  93. }
  94. func ps() {
  95. // determine whether the process exists
  96. isExist, err := robotgo.PidExists(100)
  97. if err == nil && isExist {
  98. fmt.Println("pid exists is", isExist)
  99. robotgo.Kill(100)
  100. }
  101. // get the all process id
  102. pids, err := robotgo.Pids()
  103. if err == nil {
  104. fmt.Println("pids: ", pids)
  105. }
  106. // get the all process struct
  107. ps, err := robotgo.Process()
  108. if err == nil {
  109. fmt.Println("process: ", ps)
  110. }
  111. }
  112. func window() {
  113. ////////////////////////////////////////////////////////////////////////////////
  114. // Window Handle
  115. ////////////////////////////////////////////////////////////////////////////////
  116. alert()
  117. //
  118. get()
  119. findIds()
  120. active()
  121. findName()
  122. //
  123. ps()
  124. // close current Window
  125. robotgo.CloseWindow()
  126. }
  127. func main() {
  128. window()
  129. }