Module: Mouse

Defined in:
ext/mouse/mouse.c,
lib/mouse/version.rb,
ext/mouse/mouse.c

Overview

A module with methods that "tap" into the system input methods. This is done by wrapping wrapping around the CoreGraphics event taps API provided by OS X.

The module provides a simple Ruby interface to performing mouse interactions such as moving and clicking.

This module can be used in a stand alone fashion or you can mix it into another class.

Reference

Constant Summary collapse

VERSION =
'1.0.5'

Instance Method Summary collapse

Instance Method Details

#arbitrary_click(*args) ⇒ CGPoint

Generate a click using an arbitrary mouse button (down and up events)

Numbers are used to map the mouse buttons. At the time of writing, the documented values are:

  • kCGMouseButtonLeft = 0
  • kCGMouseButtonRight = 1
  • kCGMouseButtonCenter = 2

And the rest are not documented! Though they should be easy enough to figure out. See the CGMouseButton enum in the reference documentation for the most up to date list.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'ext/mouse/mouse.c', line 294

static
VALUE
rb_mouse_arbitrary_click(int argc, VALUE *argv, VALUE self)
{
  if (argc == 0) {
    rb_raise(rb_eArgError, "arbitrary_click requires at least one arg");
    return Qnil;
  }

  uint_t button = NUM2INT(rb_funcall(argv[0], sel_to_i, 0));

  switch (argc)
    {
    case 1:
      mouse_arbitrary_click(button);
      break;
    case 2:
    default:
      mouse_arbitrary_click2(button, rb_mouse_unwrap_point(argv[1]));
    }

  return CURRENT_POSITION;
}

#click(*args) ⇒ CGPoint

Generate a regular click event (both up and down events)

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'ext/mouse/mouse.c', line 228

static
VALUE
rb_mouse_click(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_click();
      break;
    case 1:
    default:
      mouse_click2(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}

#click_down(*args) ⇒ CGPoint

Generate the down click part of a click event

This might be useful in concert with #click_up if you want to inject some behaviour between the down and up click events.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/mouse/mouse.c', line 173

static
VALUE
rb_mouse_click_down(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_click_down();
      break;
    case 1:
    default:
      mouse_click_down2(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}

#click_up(*args) ⇒ CGPoint

Generate the up click part of a click event

This might be useful in concert with #click_down if you want to inject some behaviour between the down and up click events.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/mouse/mouse.c', line 202

static
VALUE
rb_mouse_click_up(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_click_up();
      break;
    case 1:
    default:
      mouse_click_up(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}

#current_positionCGPoint

Returns the current co-ordinates of the mouse cursor

Returns:



51
52
53
54
55
56
# File 'ext/mouse/mouse.c', line 51

static
VALUE
rb_mouse_current_position(VALUE self)
{
  return CURRENT_POSITION;
}

#double_click(*args) ⇒ CGPoint

Perform a double click at the given mouse position

Implemented by first generating a single click, and then a double click., Apps seem to respond more consistently to this behaviour since that is how a human would have to generate a double click event.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'ext/mouse/mouse.c', line 398

static
VALUE
rb_mouse_double_click(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_double_click();
      break;
    case 1:
    default:
      mouse_double_click2(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}

#drag_to(*args) ⇒ CGPoint

Drag the mouse cursor to the given co-ordinates

Parameters:

  • point (CGPoint, Array(Number,Number), #to_point)
  • duration (Number)

    animation time, in seconds (optional)

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/mouse/mouse.c', line 91

static
VALUE
rb_mouse_drag_to(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      rb_raise(rb_eArgError, "drag_to requires at least a one arg");
      break;
    case 1:
      mouse_drag_to(rb_mouse_unwrap_point(argv[0]));
      break;
    case 2:
    default:
      mouse_drag_to2(rb_mouse_unwrap_point(argv[0]), NUM2DBL(argv[1]));
    }

  return CURRENT_POSITION;
}

#middle_click(*args) ⇒ CGPoint

Generate a click event for the middle mouse button (down and up events)

It doesn't matter if you don't have a middle mouse button.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'ext/mouse/mouse.c', line 329

static
VALUE
rb_mouse_middle_click(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_middle_click();
      break;
    case 1:
    default:
      mouse_middle_click(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}

#move_to(*args) ⇒ CGPoint

Move the mouse cursor to the given co-ordinates

Parameters:

  • point (CGPoint, Array(Number,Number), #to_point)
  • duration (Number)

    animation time, in seconds (optional)

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/mouse/mouse.c', line 65

static
VALUE
rb_mouse_move_to(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      rb_raise(rb_eArgError, "move_to requires at least a one arg");
      break;
    case 1:
      mouse_move_to(rb_mouse_unwrap_point(argv[0]));
      break;
    case 2:
    default:
      mouse_move_to2(rb_mouse_unwrap_point(argv[0]), NUM2DBL(argv[1]));
    }

  return CURRENT_POSITION;
}

#multi_click(*args) ⇒ CGPoint

Generate a multi-click event at the current mouse position

Unlike #double_click and #triple_click this will generate a single event with the given number of clicks.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'ext/mouse/mouse.c', line 359

static
VALUE
rb_mouse_multi_click(int argc, VALUE *argv, VALUE self)
{

  if (argc == 0) {
    rb_raise(rb_eArgError, "multi_click requires at least one arg");
    return Qnil;
  }

  // TODO: there has got to be a more idiomatic way to do this coercion
  size_t num_clicks = NUM2SIZET(rb_funcall(argv[0], sel_to_i, 0));

  switch (argc)
    {
    case 1:
      mouse_multi_click(num_clicks);
      break;
    case 2:
    default:
      mouse_multi_click2(num_clicks, rb_mouse_unwrap_point(argv[1]));
    }

  return CURRENT_POSITION;
}

#scroll(*args) ⇒ Number

Note:

Scrolling by :pixel may not actually be by real pixels, but instead correspond to Cocoa co-ords (I don't have a retina display, so I haven't checked it out yet).

Generate amount scroll events at the current cursor position

Returns number of lines scrolled. A positive amount will scroll up and a negative amount will scroll down.

An invalid type of units will default to :line.

Parameters:

  • amount (Number)
  • units (Symbol)

    :pixel or :line (default: :line ) (optional)

Returns:

  • (Number)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/mouse/mouse.c', line 127

static
VALUE
rb_mouse_scroll(int argc, VALUE *argv, VALUE self)
{
  if (argc == 0 || argc > 3)
    rb_raise(rb_eArgError, "scroll requires 1..3 arguments, you gave %d", argc);

  VALUE  amount = rb_funcall(argv[0], sel_to_i, 0);
  size_t amt    = NUM2SIZET(amount);

  if (argc == 1)
    mouse_scroll(NUM2SIZET(amt));

  ID units = rb_to_id(argv[1]);

  if (argc == 2) {
    if (units == unit_pixel)
      mouse_scroll2(amt, kCGScrollEventUnitPixel);
    else
      mouse_scroll2(amt, kCGScrollEventUnitLine);
  }

  if (argc == 3) {
    double duration = NUM2DBL(argv[2]);

    if (units == unit_pixel)
      mouse_scroll3(amt, kCGScrollEventUnitPixel, duration);
    else
      mouse_scroll3(amt, kCGScrollEventUnitLine, duration);
  }

  return amount;
}

#secondary_click(*args) ⇒ CGPoint Also known as: right_click

Generate a secondary click (both down and up events)

Secondary click is often referred to as "right click".

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'ext/mouse/mouse.c', line 256

static
VALUE
rb_mouse_secondary_click(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_secondary_click();
      break;
    case 1:
    default:
      mouse_secondary_click2(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}

#triple_click(*args) ⇒ CGPoint

Perform a triple click at the given mouse position

Implemented by first generating a single click, then a double click, and finally a triple click. Apps seem to respond more consistently to this behaviour since that is how a human would have to generate a triple click event.

You can optionally specify a point to click; the mouse cursor will instantly jump to the given point.

Parameters:

Returns:



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'ext/mouse/mouse.c', line 429

static
VALUE
rb_mouse_triple_click(int argc, VALUE *argv, VALUE self)
{
  switch (argc)
    {
    case 0:
      mouse_triple_click();
      break;
    case 1:
    default:
      mouse_triple_click2(rb_mouse_unwrap_point(argv[0]));
    }

  return CURRENT_POSITION;
}