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 (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

#positionPosition

Returns:



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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • x (Integer)
  • y (Integer)


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

Parameters:

  • type (Integer)

    e.g. ‘MacOS::Library::CoreGraphicsEventType::MOUSE_MOVED`

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

    e.g. ‘MacOS::Library::CoreGraphicsMouseButton::MIDDLE`



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, button)

  Library::CoreGraphics.CGEventPost(Library::CoreGraphics::EventTapLocation::HID_EVENT_TAP, event)
  Library::CoreGraphics.CFRelease(event)
end