Class: MacOS::Mouse
- Inherits:
-
Object
- Object
- MacOS::Mouse
- Defined in:
- lib/macos/mouse.rb
Overview
Simulates a macOS mouse.
Constant Summary collapse
- Position =
Data.define(:x, :y)
Instance Method Summary collapse
- #left_click(x:, y:) ⇒ Object
- #left_down(x:, y:) ⇒ Object
- #left_up(x:, y:) ⇒ Object
- #middle_click(x:, y:) ⇒ Object
- #middle_down(x:, y:) ⇒ Object
- #middle_up(x:, y:) ⇒ Object
- #move(x:, y:) ⇒ Object
- #position ⇒ Position
- #right_click(x:, y:) ⇒ Object
- #right_down(x:, y:) ⇒ Object
- #right_up(x:, y:) ⇒ Object
- #trigger(type:, x:, y:, button: 0) ⇒ Object
Instance Method Details
#left_click(x:, y:) ⇒ Object
56 57 58 59 |
# File 'lib/macos/mouse.rb', line 56 def left_click(x:, y:) left_down(x:, y:) left_up(x:, y:) end |
#left_down(x:, y:) ⇒ Object
44 45 46 |
# File 'lib/macos/mouse.rb', line 44 def left_down(x:, y:) trigger(type: CG::EventType::LEFT_MOUSE_DOWN, x:, y:, button: CG::MouseButton::LEFT) end |
#left_up(x:, y:) ⇒ Object
50 51 52 |
# File 'lib/macos/mouse.rb', line 50 def left_up(x:, y:) trigger(type: CG::EventType::LEFT_MOUSE_UP, x:, y:, button: CG::MouseButton::LEFT) end |
#middle_click(x:, y:) ⇒ Object
94 95 96 97 |
# File 'lib/macos/mouse.rb', line 94 def middle_click(x:, y:) middle_down(x:, y:) middle_up(x:, y:) end |
#middle_down(x:, y:) ⇒ Object
82 83 84 |
# File 'lib/macos/mouse.rb', line 82 def middle_down(x:, y:) trigger(type: CG::EventType::OTHER_MOUSE_DOWN, x:, y:, button: CG::MouseButton::CENTER) end |
#middle_up(x:, y:) ⇒ Object
88 89 90 |
# File 'lib/macos/mouse.rb', line 88 def middle_up(x:, y:) trigger(type: CG::EventType::OTHER_MOUSE_UP, x:, y:, button: CG::MouseButton::CENTER) end |
#move(x:, y:) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/macos/mouse.rb', line 35 def move(x:, y:) position = CG::Point.new position[:x] = x position[:y] = y Library.CGWarpMouseCursorPosition(position) end |
#position ⇒ Position
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/macos/mouse.rb', line 9 def position event = Library.CGEventCreate(nil) position = Library.CGEventGetLocation(event) Library.CFRelease(event) x = position[:x] y = position[:y] Position.new(x, y) end |
#right_click(x:, y:) ⇒ Object
75 76 77 78 |
# File 'lib/macos/mouse.rb', line 75 def right_click(x:, y:) right_down(x:, y:) right_up(x:, y:) end |
#right_down(x:, y:) ⇒ Object
63 64 65 |
# File 'lib/macos/mouse.rb', line 63 def right_down(x:, y:) trigger(type: CG::EventType::RIGHT_MOUSE_DOWN, x:, y:, button: CG::MouseButton::RIGHT) end |
#right_up(x:, y:) ⇒ Object
69 70 71 |
# File 'lib/macos/mouse.rb', line 69 def right_up(x:, y:) trigger(type: CG::EventType::RIGHT_MOUSE_UP, x:, y:, button: CG::MouseButton::RIGHT) end |
#trigger(type:, x:, y:, button: 0) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/macos/mouse.rb', line 24 def trigger(type:, x:, y:, button: 0) position = CG::Point.new position[:x] = x position[:y] = y event = Library.CGEventCreateMouseEvent(nil, type, position, ) Library.CGEventPost(CG::EventTapLocation::HID_EVENT_TAP, event) Library.CFRelease(event) end |