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.6'

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:



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/mouse/mouse.c', line 299

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:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/mouse/mouse.c', line 233

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:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/mouse/mouse.c', line 178

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:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'ext/mouse/mouse.c', line 207

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:



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

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:



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'ext/mouse/mouse.c', line 403

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:



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

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:



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'ext/mouse/mouse.c', line 334

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:



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

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:



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'ext/mouse/mouse.c', line 364

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.

Parameters:

  • amount (Number)
  • units (Symbol)

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

Returns:

  • (Number)


126
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
160
161
162
163
164
# File 'ext/mouse/mouse.c', line 126

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);
  int   amt    = NUM2INT(amount);

  if (argc == 1) {
    mouse_scroll(amt);
    return amount;
  }

  ID units = rb_to_id(argv[1]);

  if (argc == 2) {
    if (units == unit_pixel)
      mouse_scroll2(amt, kCGScrollEventUnitPixel);
    else if (units == unit_line)
      mouse_scroll2(amt, kCGScrollEventUnitLine);
    else
      rb_raise(rb_eArgError, "unknown units `%s'", rb_id2name(units));
  }

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

    if (units == unit_pixel)
      mouse_scroll3(amt, kCGScrollEventUnitPixel, duration);
    else if (units == unit_line)
      mouse_scroll3(amt, kCGScrollEventUnitLine, duration);
    else
      rb_raise(rb_eArgError, "unknown units `%s'", rb_id2name(units));
  }

  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:



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'ext/mouse/mouse.c', line 261

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:



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'ext/mouse/mouse.c', line 434

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;
}