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
62 63 64 65 |
# File 'lib/macos/mouse.rb', line 62 def left_click(x:, y:) left_down(x:, y:) left_up(x:, y:) end |
#left_down(x:, y:) ⇒ Object
46 47 48 49 50 |
# File 'lib/macos/mouse.rb', line 46 def left_down(x:, y:) trigger(x:, y:, type: Library::CoreGraphics::EventType::LEFT_MOUSE_DOWN, button: Library::CoreGraphics::MouseButton::LEFT) end |
#left_up(x:, y:) ⇒ Object
54 55 56 57 58 |
# File 'lib/macos/mouse.rb', line 54 def left_up(x:, y:) trigger(x:, y:, type: Library::CoreGraphics::EventType::LEFT_MOUSE_UP, button: Library::CoreGraphics::MouseButton::LEFT) end |
#middle_click(x:, y:) ⇒ Object
108 109 110 111 |
# File 'lib/macos/mouse.rb', line 108 def middle_click(x:, y:) middle_down(x:, y:) middle_up(x:, y:) end |
#middle_down(x:, y:) ⇒ Object
92 93 94 95 96 |
# File 'lib/macos/mouse.rb', line 92 def middle_down(x:, y:) trigger(x:, y:, type: Library::CoreGraphics::EventType::OTHER_MOUSE_DOWN, button: Library::CoreGraphics::MouseButton::CENTER) end |
#middle_up(x:, y:) ⇒ Object
100 101 102 103 104 |
# File 'lib/macos/mouse.rb', line 100 def middle_up(x:, y:) trigger(x:, y:, type: Library::CoreGraphics::EventType::OTHER_MOUSE_UP, button: Library::CoreGraphics::MouseButton::CENTER) end |
#move(x:, y:) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/macos/mouse.rb', line 37 def move(x:, y:) position = Library::CoreGraphics::CGPoint.new position[:x] = x position[:y] = y Library::CoreGraphics.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::CoreGraphics.CGEventCreate(nil) position = Library::CoreGraphics.CGEventGetLocation(event) Library::CoreGraphics.CFRelease(event) x = position[:x] y = position[:y] Position.new(x, y) end |
#right_click(x:, y:) ⇒ Object
85 86 87 88 |
# File 'lib/macos/mouse.rb', line 85 def right_click(x:, y:) right_down(x:, y:) right_up(x:, y:) end |
#right_down(x:, y:) ⇒ Object
69 70 71 72 73 |
# File 'lib/macos/mouse.rb', line 69 def right_down(x:, y:) trigger(x:, y:, type: Library::CoreGraphics::EventType::RIGHT_MOUSE_DOWN, button: Library::CoreGraphics::MouseButton::RIGHT) end |
#right_up(x:, y:) ⇒ Object
77 78 79 80 81 |
# File 'lib/macos/mouse.rb', line 77 def right_up(x:, y:) trigger(x:, y:, type: Library::CoreGraphics::EventType::RIGHT_MOUSE_UP, button: Library::CoreGraphics::MouseButton::RIGHT) end |
#trigger(type:, x:, y:, button: 0) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/macos/mouse.rb', line 24 def trigger(type:, x:, y:, button: 0) position = Library::CoreGraphics::CGPoint.new position[:x] = x position[:y] = y event = Library::CoreGraphics.CGEventCreateMouseEvent(nil, type, position, ) Library::CoreGraphics.CGEventPost(Library::CoreGraphics::EventTapLocation::HID_EVENT_TAP, event) Library::CoreGraphics.CFRelease(event) end |