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

VERSION =
'3.0.0'

Instance Method Summary (collapse)

Instance Method Details

- (CGPoint) arbitrary_click

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.



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'ext/mouse/mouse.c', line 470

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(argv[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;
}

- (CGPoint) arbitrary_click_down

Generate the down click part of an arbitrary click event

This might be useful in concert with arbitrary_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.



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

static
VALUE
rb_mouse_arbitrary_click_down(int argc, VALUE* argv, VALUE self)
{
  if (argc == 0)
    rb_raise(rb_eArgError, "arbitrary_click_down requires at least one arg");

  uint_t button = NUM2INT(argv[0]);

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

  return CURRENT_POSITION;
}

- (CGPoint) arbitrary_click_up

Generate the up click part of an arbitrary click event

This might be useful in concert with arbitrary_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.



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

static
VALUE
rb_mouse_arbitrary_click_up(int argc, VALUE* argv, VALUE self)
{
  if (argc == 0)
    rb_raise(rb_eArgError, "arbitrary_click_up requires at least one arg");

  uint_t button = NUM2INT(argv[0]);

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

  return CURRENT_POSITION;
}

- (CGPoint) click

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.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'ext/mouse/mouse.c', line 276

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

- (CGPoint) click_down

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.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'ext/mouse/mouse.c', line 221

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

- (CGPoint) click_up

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.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/mouse/mouse.c', line 250

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

- (CGPoint) current_position

Returns the current co-ordinates of the mouse cursor



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

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

- (CGPoint) double_click

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.



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'ext/mouse/mouse.c', line 574

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

- (CGPoint) drag_to

Drag the mouse cursor to the given co-ordinates



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

- (Number) horizontal_scroll Also known as: hscroll

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 of horizontal scroll events at the current cursor position

Returns number of lines scrolled. A positive amount will scroll left and a negative amount will scroll right.

An animation duration can also be specified.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/mouse/mouse.c', line 177

static
VALUE
rb_mouse_horizontal_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);

  int amt = NUM2INT(argv[0]);

  if (argc == 1) {
    mouse_horizontal_scroll(amt);

  } else {
    VALUE             input_units = argv[1];
    CGScrollEventUnit units;

    if (input_units == sym_pixel)
      units = kCGScrollEventUnitPixel;
    else if (input_units == sym_line)
      units = kCGScrollEventUnitLine;
    else
      rb_raise(rb_eArgError, "unknown units `%s'", rb_id2name(input_units));

    if (argc == 2)
      mouse_horizontal_scroll2(amt, units);
    else
      mouse_horizontal_scroll3(amt, units, NUM2DBL(argv[2]));
  }

  return argv[0];
}

- (CGPoint) middle_click

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.



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'ext/mouse/mouse.c', line 505

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

- (CGPoint) move_to

Move the mouse cursor to the given co-ordinates



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

- (CGPoint) multi_click

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.



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'ext/mouse/mouse.c', line 535

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(argv[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;
}

- (CGPoint) pinch

Perform a pinch gesture in given direction

You can optionally specify the magnification factor and/or duration for the pinch event.

Available pinch directions are:

  • :zoom or :expand
  • :unzoom or :contract

Magnification is a relative magnification setting. A zoom value of 1.0 means 1.0 more than the current zoom level. 2.0 would be 2.0 levels higher than the current zoom.

You can also optionally specify a point on screen for the mouse pointer to be moved to before the gesture begins. The movement will be instantaneous.

An animation duration can also be specified.



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'ext/mouse/mouse.c', line 731

static
VALUE
rb_mouse_pinch(int argc, VALUE* argv, VALUE self)
{
  if (!argc)
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1+)", argc);

  VALUE            input_direction = argv[0];
  CGPinchDirection       direction = kCGPinchNone;

  if (input_direction == sym_expand || input_direction == sym_zoom)
    direction = kCGPinchExpand;
  else if (input_direction == sym_contract || input_direction == sym_unzoom)
    direction = kCGPinchContract;
  else
    rb_raise(
	     rb_eArgError,
	     "invalid pinch direction `%s'",
	     rb_id2name(SYM2ID(input_direction))
	     );

  if (argc == 1) {
    mouse_pinch(direction);
    return CURRENT_POSITION;
  }

  double magnification = NUM2DBL(argv[1]);
  if (argc == 2) {
    mouse_pinch2(direction, magnification);
    return CURRENT_POSITION;
  }

  CGPoint point = rb_mouse_unwrap_point(argv[2]);
  if (argc == 3) {
    mouse_pinch3(direction, magnification, point);
    return CURRENT_POSITION;
  }

  double duration = NUM2DBL(argv[3]);
  mouse_pinch4(direction, magnification, point, duration);
  return CURRENT_POSITION;
}

- (CGPoint) rotate

Perform a rotation gesture in the given direction the given angle degrees

Possible directions are:

  • :cw, ':clockwise, ':clock_wise to rotate in the clockwise direction
  • :ccw, ':counter_clockwise,:counter_clock_wise` to rotate in the the counter clockwise direction

The angle parameter is a number of degrees to rotate. There are 360 degrees in a full rotation, as you would expect in Euclidian geometry.

You can also optionally specify a point on screen for the mouse pointer to be moved to before the gesture begins. The movement will be instantaneous.

An animation duration can also be specified.



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'ext/mouse/mouse.c', line 799

static
VALUE
rb_mouse_rotate(int argc, VALUE* argv, VALUE self)
{
  if (argc < 2)
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", argc);

  CGRotateDirection direction = kCGRotateNone;
  VALUE             input_dir = argv[0];
  if (
      input_dir == sym_cw ||
      input_dir == sym_clockwise ||
      input_dir == sym_clock_wise
      )
    direction = kCGRotateClockwise;
  else if (
      input_dir == sym_ccw ||
      input_dir == sym_counter_clockwise ||
      input_dir == sym_counter_clock_wise
	   )
    direction = kCGRotateCounterClockwise;
  else
    rb_raise(
	     rb_eArgError,
	     "invalid rotation direction `%s'",
	     rb_id2name(SYM2ID(input_dir))
	     );

  double angle = NUM2DBL(argv[1]);

  if (argc == 2) {
    mouse_rotate(direction, angle);
    return CURRENT_POSITION;
  }

  CGPoint point = rb_mouse_unwrap_point(argv[2]);
  if (argc == 3) {
    mouse_rotate2(direction, angle, point);
    return CURRENT_POSITION;
  }

  mouse_rotate3(direction, angle, point, NUM2DBL(argv[3]));
  return CURRENT_POSITION;
}

- (Number) scroll

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 animation duration can also be specified.



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
# File 'ext/mouse/mouse.c', line 128

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

  int amt = NUM2INT(argv[0]);

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

  } else {
    VALUE             input_units = argv[1];
    CGScrollEventUnit units;

    if (input_units == sym_pixel)
      units = kCGScrollEventUnitPixel;
    else if (input_units == sym_line)
      units = kCGScrollEventUnitLine;
    else
      rb_raise(rb_eArgError, "unknown units `%s'", rb_id2name(input_units));

    if (argc == 2)
      mouse_scroll2(amt, units);
    else
      mouse_scroll3(amt, units, NUM2DBL(argv[2]));
  }

  return argv[0];
}

- (CGPoint) secondary_click 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.



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'ext/mouse/mouse.c', line 363

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

- (CGPoint) secondary_click_down Also known as: right_click_down

Generate the down click part of a secondary/right click event

This might be useful in concert with secondary_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.



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/mouse/mouse.c', line 306

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

  return CURRENT_POSITION;
}

- (CGPoint) secondary_click_up Also known as: right_click_up

Generate the up click part of a secondary/right click event

This might be useful in concert with secondary_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.



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

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

  return CURRENT_POSITION;
}

- (CGPoint) smart_magnify Also known as: two_finger_double_tap

Perform a smart magnify (double tap on trackpad)

You can optionally specify the point on the screen where to perform the smart magnification.



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'ext/mouse/mouse.c', line 634

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

  return CURRENT_POSITION;
}

- (CGPoint) swipe

Perform a swipe gesture in the given direction

You can optionally specify a point on screen for the mouse pointer to be moved to before the gesture begins. The movement will be instantaneous.

You can also optionally specify the duration of the swipe event.



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'ext/mouse/mouse.c', line 665

static
VALUE
rb_mouse_swipe(int argc, VALUE* argv, VALUE self)
{
  if (!argc)
    rb_raise(rb_eArgError, "wrong number of arguments (0 for 1+)");

  CGSwipeDirection direction;
  VALUE direction_input = argv[0];
  if (direction_input == sym_up)
    direction = kCGSwipeDirectionUp;
  else if (direction_input == sym_down)
    direction = kCGSwipeDirectionDown;
  else if (direction_input == sym_left)
    direction = kCGSwipeDirectionLeft;
  else if (direction_input == sym_right)
    direction = kCGSwipeDirectionRight;
  else
    rb_raise(
	     rb_eArgError,
	     "invalid swipe direction `%s'",
	     rb_id2name(SYM2ID(direction_input))
	     );

  if (argc == 1) {
    mouse_swipe(direction);
    return CURRENT_POSITION;
  }

  CGPoint point = rb_mouse_unwrap_point(argv[1]);

  if (argc == 2)
    mouse_swipe2(direction, point);
  else
    mouse_swipe3(direction, point, NUM2DBL(argv[1]));

  return CURRENT_POSITION;
}

- (CGPoint) triple_click

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.



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'ext/mouse/mouse.c', line 605

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