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 =
'4.0.3'

Instance Method Summary collapse

Instance Method Details

#arbitrary_click(, self) ⇒ Object



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

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

    const uint_t button = NUM2UINT(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;
}

#arbitrary_click_down(, self) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'ext/mouse/mouse.c', line 447

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

    const uint_t button = NUM2UINT(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;
}

#arbitrary_click_up(, self) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'ext/mouse/mouse.c', line 487

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

    const uint_t button = NUM2UINT(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;
}

#click(, self) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'ext/mouse/mouse.c', line 312

static
VALUE
rb_mouse_click(const int argc, VALUE* const argv, UNUSED const 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(, self) ⇒ Object



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

static
VALUE
rb_mouse_click_down(const int argc,
                    VALUE* const argv,
                    UNUSED const 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(, self) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/mouse/mouse.c', line 283

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

    return CURRENT_POSITION;
}

#current_positionCGPoint

Returns the current co-ordinates of the mouse cursor



43
44
45
46
47
48
# File 'ext/mouse/mouse.c', line 43

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

#double_click(, self) ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'ext/mouse/mouse.c', line 657

static
VALUE
rb_mouse_double_click(const int argc,
                      VALUE* const argv,
                      UNUSED const 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(, self) ⇒ Object



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

static
VALUE
rb_mouse_drag_to(const int argc,
                 VALUE* const argv,
                 UNUSED const VALUE self)
{
    switch (argc) {
    case 0:
        rb_raise(rb_eArgError, "drag_to requires at least a one arg");
    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;
}

#horizontal_scroll(, self) ⇒ Object Also known as: hscroll



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/mouse/mouse.c', line 195

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

    const int amt = NUM2INT(argv[0]);

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

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

        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(SYM2ID(input_units)));

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

    return argv[0];
}

#middle_click(, self) ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'ext/mouse/mouse.c', line 577

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

    return CURRENT_POSITION;
}

#move_to(, self) ⇒ Object



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

static
VALUE
rb_mouse_move_to(const int argc,
                 VALUE* const argv,
                 UNUSED const VALUE self)
{
    switch (argc) {
    case 0:
        rb_raise(rb_eArgError, "move_to requires at least a one arg");
    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(, self) ⇒ Object



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'ext/mouse/mouse.c', line 613

static
VALUE
rb_mouse_multi_click(const int argc,
                     VALUE* const argv,
                     UNUSED const 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
    const 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;
}

#pinch(, self) ⇒ Object



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'ext/mouse/mouse.c', line 841

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

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

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

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

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

#rotate(, self) ⇒ Object



917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'ext/mouse/mouse.c', line 917

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


    const VALUE input_dir = argv[0];
    CGRotateDirection direction = kCGRotateNone;

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

    const double angle = NUM2DBL(argv[1]);

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

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

#scroll(, self) ⇒ Object



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
165
166
167
168
# File 'ext/mouse/mouse.c', line 134

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

    const int amt = NUM2INT(argv[0]);

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

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

        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(SYM2ID(input_units)));

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

    return argv[0];
}

#secondary_click(, self) ⇒ Object Also known as: right_click



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'ext/mouse/mouse.c', line 412

static
VALUE
rb_mouse_secondary_click(const int argc,
                         VALUE* const argv,
                         UNUSED const 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;
}

#secondary_click_down(, self) ⇒ Object Also known as: right_click_down



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/mouse/mouse.c', line 345

static
VALUE
rb_mouse_secondary_click_down(const int argc,
                              VALUE* const argv,
                              UNUSED const 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;
}

#secondary_click_up(, self) ⇒ Object Also known as: right_click_up



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'ext/mouse/mouse.c', line 379

static
VALUE
rb_mouse_secondary_click_up(const int argc,
                            VALUE* const argv,
                            UNUSED const 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;
}

#smart_magnify(, self) ⇒ Object Also known as: two_finger_double_tap



727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'ext/mouse/mouse.c', line 727

static
VALUE
rb_mouse_smart_magnify(const int argc,
                       VALUE* const argv,
                       UNUSED const 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;
}

#swipe(, self) ⇒ Object



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'ext/mouse/mouse.c', line 769

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

    const VALUE direction_input = argv[0];
    CGSwipeDirection direction = kCGSwipeDirectionNone;
    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;
    }

    const CGPoint point = rb_mouse_unwrap_point(argv[1]);
    mouse_swipe2(direction, point);
    return CURRENT_POSITION;
}

#triple_click(, self) ⇒ Object



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'ext/mouse/mouse.c', line 693

static
VALUE
rb_mouse_triple_click(const int argc,
                      VALUE* const argv,
                      UNUSED const 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;
}