Class: MacOS::Mouse

Inherits:
Object
  • Object
show all
Defined in:
lib/macos/mouse.rb

Overview

Simulates a macOS mouse.

Constant Summary collapse

Position =
Data.define(:x, :y)

Instance Method Summary collapse

Instance Method Details

#left_click(x:, y:) ⇒ Object

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

#positionPosition

Returns:



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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • x (Float)
  • y (Float)


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

Parameters:

  • type (Integer)

    e.g. ‘MacOS::CG::EventType::MOUSE_MOVED`

  • x (Float)
  • y (Float)
  • button (Integer) (defaults to: 0)

    e.g. ‘MacOS::CG::MouseButton::LEFT` / `MacOS::CG::MouseButton::RIGHT` / etc.



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, button)
  Library.CGEventPost(CG::EventTapLocation::HID_EVENT_TAP, event)
  Library.CFRelease(event)
end