mouse.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //go:build windows && amd64
  2. // +build windows,amd64
  3. package goautoit
  4. import (
  5. "log"
  6. "unsafe"
  7. )
  8. // MouseClickDrag -- Perform a mouse click and drag operation.
  9. func MouseClickDrag(button string, x1, y1, x2, y2 int, args ...interface{}) int {
  10. var nSpeed int
  11. var ok bool
  12. if len(args) == 0 {
  13. nSpeed = 10
  14. } else if len(args) == 1 {
  15. if nSpeed, ok = args[0].(int); !ok {
  16. panic("nSpeed must be a int")
  17. }
  18. } else {
  19. panic("Error parameters")
  20. }
  21. ret, _, lastErr := mouseClickDrag.Call(strPtr(button), intPtr(x1), intPtr(x2), intPtr(y2), intPtr(nSpeed))
  22. if int(ret) != 1 {
  23. log.Print("failure!!!")
  24. log.Println(lastErr)
  25. }
  26. return int(ret)
  27. }
  28. // MouseDown -- Perform a mouse down event at the current mouse position.
  29. func MouseDown(args ...interface{}) int {
  30. var button string
  31. var ok bool
  32. if len(args) == 0 {
  33. button = DefaultMouseButton
  34. } else if len(args) == 1 {
  35. if button, ok = args[0].(string); !ok {
  36. panic("nSpeed must be a int")
  37. }
  38. } else {
  39. panic("Error parameters")
  40. }
  41. ret, _, lastErr := mouseDown.Call(strPtr(button))
  42. if int(ret) != 1 {
  43. log.Print("failure!!!")
  44. log.Println(lastErr)
  45. }
  46. return int(ret)
  47. }
  48. // MouseUp -- Perform a mouse up event at the current mouse position.
  49. func MouseUp(args ...interface{}) int {
  50. var button string
  51. var ok bool
  52. if len(args) == 0 {
  53. button = DefaultMouseButton
  54. } else if len(args) == 1 {
  55. if button, ok = args[0].(string); !ok {
  56. panic("nSpeed must be a int")
  57. }
  58. } else {
  59. panic("Error parameters")
  60. }
  61. ret, _, lastErr := mouseUp.Call(strPtr(button))
  62. if int(ret) != 1 {
  63. log.Print("failure!!!")
  64. log.Println(lastErr)
  65. }
  66. return int(ret)
  67. }
  68. // MouseGetCursor -- Returns the cursor ID Number for the current Mouse Cursor.
  69. func MouseGetCursor() int {
  70. ret, _, lastErr := mouseGetCursor.Call()
  71. if int(ret) == -1 {
  72. log.Println(lastErr)
  73. }
  74. return int(ret)
  75. }
  76. // MouseGetPos -- Retrieves the current position of the mouse cursor.
  77. func MouseGetPos() (int32, int32) {
  78. var point = POINT{}
  79. ret, _, lastErr := mouseGetPos.Call(uintptr(unsafe.Pointer(&point)))
  80. if ret == 0 {
  81. log.Println(lastErr)
  82. }
  83. return point.X, point.Y
  84. }
  85. // MouseMove -- Moves the mouse pointer.
  86. func MouseMove(x, y int, args ...interface{}) {
  87. var nSpeed int
  88. var ok bool
  89. if len(args) == 0 {
  90. nSpeed = -1
  91. } else if len(args) == 1 {
  92. if nSpeed, ok = args[0].(int); !ok {
  93. panic("nSpeed must be a int")
  94. }
  95. } else {
  96. panic("Error parameters")
  97. }
  98. ret, _, lastErr := mouseMove.Call(intPtr(x), intPtr(y), intPtr(nSpeed))
  99. if ret == 0 {
  100. log.Println(lastErr)
  101. }
  102. }
  103. // MouseWheel -- Moves the mouse wheel up or down.
  104. func MouseWheel(szDirection string, args ...interface{}) int {
  105. var nClicks int
  106. var ok bool
  107. if len(args) == 0 {
  108. nClicks = 1
  109. } else if len(args) == 1 {
  110. if nClicks, ok = args[0].(int); !ok {
  111. panic("nClicks must be a int")
  112. }
  113. } else {
  114. panic("Error parameters")
  115. }
  116. ret, _, lastErr := mouseWheel.Call(strPtr(szDirection), intPtr(nClicks))
  117. if ret == 0 {
  118. log.Println(lastErr)
  119. }
  120. return int(ret)
  121. }