-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathlibeventtap_event.m
More file actions
1829 lines (1672 loc) · 99.9 KB
/
libeventtap_event.m
File metadata and controls
1829 lines (1672 loc) · 99.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
165
166
167
168
169
170
171
172
173
174
175
176
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
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
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
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
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import <Foundation/Foundation.h>
#import "TouchEvents.h"
#import "eventtap_event.h"
#import "HSuicore.h"
#include "IOHIDEventTypes.h"
@import IOKit.hidsystem ;
#define FLAGS_TAG "hs.eventtap.event.flags"
static CGEventSourceRef eventSource = NULL;
static int eventtap_event_gc(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
if (event)
CFRelease(event);
// Remove the Metatable so future use of the variable in Lua won't think its valid
lua_pushnil(L) ;
lua_setmetatable(L, 1) ;
return 0;
}
/// hs.eventtap.event:copy() -> event
/// Constructor
/// Duplicates an `hs.eventtap.event` event for further modification or injection
///
/// Parameters:
/// * None
///
/// Returns:
/// * A new `hs.eventtap.event` object
static int eventtap_event_copy(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGEventRef copy = CGEventCreateCopy(event);
new_eventtap_event(L, copy);
CFRelease(copy);
return 1;
}
/// hs.eventtap.event.newEvent() -> event
/// Constructor
/// Creates a blank event. You will need to set its type with [hs.eventtap.event:setType](#setType)
///
/// Parameters:
/// * None
///
/// Returns:
/// * a new `hs.eventtap.event` object
///
/// Notes:
/// * this is an empty event that you should set a type for and whatever other properties may be appropriate before posting.
static int eventtap_event_newEvent(lua_State* L) {
CGEventRef event = CGEventCreate(eventSource);
new_eventtap_event(L, event);
CFRelease(event);
return 1;
}
/// hs.eventtap.event.newEventFromData(data) -> event
/// Constructor
/// Creates an event from the data encoded in the string provided.
///
/// Parameters:
/// * data - a string containing binary data provided by [hs.eventtap.event:asData](#asData) representing an event.
///
/// Returns:
/// * a new `hs.eventtap.event` object or nil if the string did not represent a valid event
static int eventtap_event_newEventFromData(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TSTRING, LS_TBREAK] ;
NSData *data = [skin toNSObjectAtIndex:1 withOptions:LS_NSLuaStringAsDataOnly] ;
CGEventRef event = CGEventCreateFromData(NULL, (__bridge CFDataRef)data);
if (event) {
new_eventtap_event(L, event);
CFRelease(event);
} else {
lua_pushnil(L) ;
}
return 1;
}
/// hs.eventtap.event.newGesture(gestureType[, gestureValue]) -> event
/// Constructor
/// Creates an gesture event.
///
/// Parameters:
/// * gestureType - the type of gesture you want to create as a string (see notes below).
/// * [gestureValue] - an optional value for the specific gesture (i.e. magnification amount or rotation in degrees).
///
/// Returns:
/// * a new `hs.eventtap.event` object or `nil` if the `gestureType` is not valid.
///
/// Notes:
/// * Valid gestureType values are:
/// * `beginMagnify` - Starts a magnification event with an optional magnification value as a number (defaults to 0). The exact unit of measurement is unknown.
/// * `endMagnify` - Starts a magnification event with an optional magnification value as a number (defaults to 0.1). The exact unit of measurement is unknown.
/// * `beginRotate` - Starts a rotation event with an rotation value in degrees (i.e. a value of 45 turns it 45 degrees left - defaults to 0).
/// * `endRotate` - Starts a rotation event with an rotation value in degrees (i.e. a value of 45 turns it 45 degrees left - defaults to 45).
/// * `beginSwipeLeft` - Begin a swipe left.
/// * `endSwipeLeft` - End a swipe left.
/// * `beginSwipeRight` - Begin a swipe right.
/// * `endSwipeRight` - End a swipe right.
/// * `beginSwipeUp` - Begin a swipe up.
/// * `endSwipeUp` - End a swipe up.
/// * `beginSwipeDown` - Begin a swipe down.
/// * `endSwipeDown` - End a swipe down.
/// * `smartMagnify` - Performs smart mangify.
///
/// Examples:
/// ```lua
/// hs.hotkey.bind({"cmd", "alt", "ctrl"}, "1", function()
/// print("Magnify slightly")
/// a = require("hs.eventtap.event").newGesture("beginMagnify", 0)
/// b = require("hs.eventtap.event").newGesture("endMagnify", 0.1)
/// a:post()
/// b:post()
/// end)
/// hs.hotkey.bind({"cmd", "alt", "ctrl"}, "2", function()
/// print("Swipe down")
/// a = require("hs.eventtap.event").newGesture("beginSwipeDown")
/// b = require("hs.eventtap.event").newGesture("endSwipeDown")
/// a:post()
/// b:post()
/// end)
/// hs.hotkey.bind({"cmd", "alt", "ctrl"}, "3", function()
/// print("Rotate 45 degrees left")
/// a = require("hs.eventtap.event").newGesture("beginRotate", 0)
/// b = require("hs.eventtap.event").newGesture("endRotate", 45)
/// a:post()
/// b:post()
/// end)
/// hs.hotkey.bind({"cmd", "alt", "ctrl"}, "4", function()
/// print("Rotate 45 degrees right")
/// a = require("hs.eventtap.event").newGesture("beginRotate", 0)
/// b = require("hs.eventtap.event").newGesture("endRotate", -45)
/// a:post()
/// b:post()
/// end)```
static int eventtap_event_newGesture(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TSTRING, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK] ;
NSString *gesture = [skin toNSObjectAtIndex:1];
NSDictionary* gestureDict;
if ([gesture isEqualToString:@"beginSwipeLeft"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseBegan), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"endSwipeLeft"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kTLInfoSwipeLeft), kTLInfoKeySwipeDirection,
@(kIOHIDEventPhaseEnded), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"beginSwipeRight"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseBegan), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"endSwipeRight"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kTLInfoSwipeRight), kTLInfoKeySwipeDirection,
@(kIOHIDEventPhaseEnded), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"beginSwipeUp"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseBegan), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"endSwipeUp"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kTLInfoSwipeUp), kTLInfoKeySwipeDirection,
@(kIOHIDEventPhaseEnded), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"beginSwipeDown"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseBegan), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"endSwipeDown"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSwipe), kTLInfoKeyGestureSubtype,
@(kTLInfoSwipeDown), kTLInfoKeySwipeDirection,
@(kIOHIDEventPhaseEnded), kTLInfoKeyGesturePhase,
nil];
}
else if ([gesture isEqualToString:@"beginMagnify"]) {
NSNumber *magnificationValue = [skin toNSObjectAtIndex:2];
double magnification = 0.0;
if (magnificationValue) {
magnification = [magnificationValue floatValue];
}
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeMagnify), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseBegan), kTLInfoKeyGesturePhase,
@(magnification), kTLInfoKeyMagnification,
nil];
}
else if ([gesture isEqualToString:@"endMagnify"]) {
NSNumber *magnificationValue = [skin toNSObjectAtIndex:2];
double magnification = 0.1;
if (magnificationValue) {
magnification = [magnificationValue floatValue];
}
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeMagnify), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseEnded), kTLInfoKeyGesturePhase,
@(magnification), kTLInfoKeyMagnification,
nil];
}
else if ([gesture isEqualToString:@"smartMagnify"]) {
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeSmartMagnify), kTLInfoKeyGestureSubtype,
nil];
}
else if ([gesture isEqualToString:@"beginRotate"]) {
NSNumber *rotationValue = [skin toNSObjectAtIndex:2];
double rotation = 0.0;
if (rotationValue) {
rotation = [rotationValue floatValue];
}
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeRotate), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseBegan), kTLInfoKeyGesturePhase,
@(rotation), kTLInfoKeyRotation,
nil];
}
else if ([gesture isEqualToString:@"endRotate"]) {
NSNumber *rotationValue = [skin toNSObjectAtIndex:2];
double rotation = 45;
if (rotationValue) {
rotation = [rotationValue floatValue];
}
gestureDict = [NSDictionary dictionaryWithObjectsAndKeys:
@(kTLInfoSubtypeRotate), kTLInfoKeyGestureSubtype,
@(kIOHIDEventPhaseEnded), kTLInfoKeyGesturePhase,
@(rotation), kTLInfoKeyRotation,
nil];
}
else
{
[LuaSkin logError:@"hs.eventtap.event.newGesture() - Invalid gesture identifier supplied."];
lua_pushnil(L) ;
return 1;
}
CGEventRef event = tl_CGEventCreateFromGesture((__bridge CFDictionaryRef)(gestureDict), (__bridge CFArrayRef)@[]);
if (event) {
new_eventtap_event(L, event);
CFRelease(event);
} else {
lua_pushnil(L) ;
}
return 1;
}
/// hs.eventtap.event:asData() -> string
/// Method
/// Returns a string containing binary data representing the event. This can be used to record events for later use.
///
/// Parameters:
/// * None
///
/// Returns:
/// * a string representing the event or nil if the event cannot be represented as a string
///
/// Notes:
/// * You can recreate the event for later posting with [hs.eventtap.event.newEventFromData](#newEventFromData)
static int eventtap_event_asData(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CFDataRef data = CGEventCreateData(NULL, event) ;
if (data) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin pushNSObject:(__bridge_transfer NSData *)data] ;
} else {
lua_pushnil(L) ;
}
return 1 ;
}
/// hs.eventtap.event:location([pointTable]) -> event | table
/// Method
/// Get or set the current mouse pointer location as defined for the event.
///
/// Parameters:
/// * pointTable - an optional point table specifying the x and y coordinates of the mouse pointer location for the event
///
/// Returns:
/// * if pointTable is provided, returns the `hs.eventtap.event` object; otherwise returns a point table containing x and y key-value pairs specifying the mouse pointer location as specified for this event.
///
/// Notes:
/// * the use or effect of this method is undefined if the event is not a mouse type event.
static int eventtap_event_location(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TTABLE | LS_TOPTIONAL, LS_TBREAK] ;
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
if (lua_gettop(L) == 1) {
[skin pushNSPoint:NSPointFromCGPoint(CGEventGetLocation(event))] ;
} else {
NSPoint theLocation = [skin tableToPointAtIndex:2] ;
CGEventSetLocation(event, NSPointToCGPoint(theLocation)) ;
lua_pushvalue(L, 1) ;
}
return 1 ;
}
/// hs.eventtap.event:timestamp([absolutetime]) -> event | integer
/// Method
/// Get or set the timestamp of the event.
///
/// Parameters:
/// * absolutetime - an optional integer specifying the timestamp for the event.
///
/// Returns:
/// * if absolutetime is provided, returns the `hs.eventtap.event` object; otherwise returns the current timestamp for the event.
///
/// Notes:
/// * Synthesized events have a timestamp of 0 by default.
/// * The timestamp, if specified, is expressed as an integer representing the number of nanoseconds since the system was last booted. See `hs.timer.absoluteTime`.
/// * This field appears to be informational only and is not required when crafting your own events with this module.
static int eventtap_event_timestamp(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TNUMBER | LS_TINTEGER | LS_TOPTIONAL, LS_TBREAK] ;
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
if (lua_gettop(L) == 1) {
lua_pushinteger(L, (lua_Integer)CGEventGetTimestamp(event)) ;
} else {
CGEventSetTimestamp(event, (CGEventTimestamp)lua_tointeger(L, 2)) ;
lua_pushvalue(L, 1) ;
}
return 1 ;
}
/// hs.eventtap.event:setType(type) -> event
/// Method
/// Set the type for this event.
///
/// Parameters:
/// * type - an integer matching one of the event types described in [hs.eventtap.event.types](#types)
///
/// Returns:
/// * the `hs.eventtap.event` object
static int eventtap_event_setType(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TNUMBER | LS_TINTEGER, LS_TBREAK] ;
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGEventSetType(event, (CGEventType)(lua_tointeger(L, 2))) ;
lua_pushvalue(L, 1) ;
return 1 ;
}
/// hs.eventtap.event:rawFlags([flags]) -> event | integer
/// Method
/// Experimental method to get or set the modifier flags for an event directly.
///
/// Parameters:
/// * flags - an optional integer, made by logically combining values from [hs.eventtap.event.rawFlagMasks](#rawFlagMasks) specifying the modifier keys which should be set for this event
///
/// Returns:
/// * if flags is provided, returns the `hs.eventtap.event` object; otherwise returns the current flags set as an integer
///
/// Notes:
/// * This method is experimental and may undergo changes or even removal in the future
/// * See [hs.eventtap.event.rawFlagMasks](#rawFlagMasks) for more information
static int eventtap_event_rawFlags(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TNUMBER | LS_TINTEGER | LS_TOPTIONAL, LS_TBREAK] ;
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
if (lua_gettop(L) == 1) {
lua_pushinteger(L, (lua_Integer)(CGEventGetFlags(event))) ;
} else {
CGEventSetFlags(event, (CGEventFlags)(lua_tointeger(L, 2))) ;
lua_pushvalue(L, 1) ;
}
return 1 ;
}
/// hs.eventtap.event:getFlags() -> table
/// Method
/// Gets the keyboard modifiers of an event
///
/// Parameters:
/// * None
///
/// Returns:
/// * A table containing the keyboard modifiers that present in the event - i.e. zero or more of the following keys, each with a value of `true`:
/// * cmd
/// * alt
/// * shift
/// * ctrl
/// * fn
/// * The table responds to the following methods:
/// * contain(mods) -> boolean
/// * Returns true if the modifiers contain all of given modifiers
/// * containExactly(mods) -> boolean
/// * Returns true if the modifiers contain all of given modifiers exactly and nothing else
/// * Parameter mods is a table containing zero or more of the following:
/// * cmd or ⌘
/// * alt or ⌥
/// * shift or ⇧
/// * ctrl or ⌃
/// * fn
static int eventtap_event_getFlags(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
lua_newtable(L);
CGEventFlags curAltkey = CGEventGetFlags(event);
if (curAltkey & kCGEventFlagMaskAlternate) { lua_pushboolean(L, YES); lua_setfield(L, -2, "alt"); }
if (curAltkey & kCGEventFlagMaskShift) { lua_pushboolean(L, YES); lua_setfield(L, -2, "shift"); }
if (curAltkey & kCGEventFlagMaskControl) { lua_pushboolean(L, YES); lua_setfield(L, -2, "ctrl"); }
if (curAltkey & kCGEventFlagMaskCommand) { lua_pushboolean(L, YES); lua_setfield(L, -2, "cmd"); }
if (curAltkey & kCGEventFlagMaskSecondaryFn) { lua_pushboolean(L, YES); lua_setfield(L, -2, "fn"); }
luaL_getmetatable(L, FLAGS_TAG);
lua_setmetatable(L, -2);
return 1;
}
/// hs.eventtap.event:setFlags(table) -> event
/// Method
/// Sets the keyboard modifiers of an event
///
/// Parameters:
/// * A table containing the keyboard modifiers to be sent with the event - i.e. zero or more of the following keys, each with a value of `true`:
/// * cmd
/// * alt
/// * shift
/// * ctrl
/// * fn
///
/// Returns:
/// * The `hs.eventap.event` object.
static int eventtap_event_setFlags(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
luaL_checktype(L, 2, LUA_TTABLE);
CGEventFlags flags = (CGEventFlags)0;
if ((void)lua_getfield(L, 2, "cmd"), lua_toboolean(L, -1)) flags |= kCGEventFlagMaskCommand;
if ((void)lua_getfield(L, 2, "alt"), lua_toboolean(L, -1)) flags |= kCGEventFlagMaskAlternate;
if ((void)lua_getfield(L, 2, "ctrl"), lua_toboolean(L, -1)) flags |= kCGEventFlagMaskControl;
if ((void)lua_getfield(L, 2, "shift"), lua_toboolean(L, -1)) flags |= kCGEventFlagMaskShift;
if ((void)lua_getfield(L, 2, "fn"), lua_toboolean(L, -1)) flags |= kCGEventFlagMaskSecondaryFn;
CGEventSetFlags(event, flags);
lua_settop(L,1) ;
return 1;
}
/// hs.eventtap.event:getRawEventData() -> table
/// Method
/// Returns raw data about the event
///
/// Parameters:
/// * None
///
/// Returns:
/// * A table with two keys:
/// * CGEventData -- a table with keys containing CGEvent data about the event.
/// * NSEventData -- a table with keys containing NSEvent data about the event.
///
/// Notes:
/// * Most of the data in `CGEventData` is already available through other methods, but is presented here without any cleanup or parsing.
/// * This method is expected to be used mostly for testing and expanding the range of possibilities available with the hs.eventtap module. If you find that you are regularly using specific data from this method for common or re-usable purposes, consider submitting a request for adding a more targeted method to hs.eventtap or hs.eventtap.event -- it will likely be more efficient and faster for common tasks, something eventtaps need to be to minimize affecting system responsiveness.
static int eventtap_event_getRawEventData(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGEventType cgType = CGEventGetType(event) ;
lua_newtable(L) ;
lua_newtable(L) ;
lua_pushinteger(L, CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode)); lua_setfield(L, -2, "keycode") ;
lua_pushinteger(L, (lua_Integer)(CGEventGetFlags(event))); lua_setfield(L, -2, "flags") ;
lua_pushinteger(L, cgType); lua_setfield(L, -2, "type") ;
lua_setfield(L, -2, "CGEventData") ;
lua_newtable(L) ;
if ((cgType != kCGEventTapDisabledByTimeout) && (cgType != kCGEventTapDisabledByUserInput)) {
NSEvent* sysEvent = [NSEvent eventWithCGEvent:event];
NSEventType type = [sysEvent type] ;
lua_pushinteger(L, (lua_Integer)([sysEvent modifierFlags])); lua_setfield(L, -2, "modifierFlags") ;
lua_pushinteger(L, (lua_Integer)type); lua_setfield(L, -2, "type") ;
lua_pushinteger(L, [sysEvent windowNumber]); lua_setfield(L, -2, "windowNumber") ;
if ((type == NSEventTypeKeyDown) || (type == NSEventTypeKeyUp)) {
lua_pushstring(L, [[sysEvent characters] UTF8String]) ; lua_setfield(L, -2, "characters") ;
lua_pushstring(L, [[sysEvent charactersIgnoringModifiers] UTF8String]) ; lua_setfield(L, -2, "charactersIgnoringModifiers") ;
lua_pushinteger(L, [sysEvent keyCode]) ; lua_setfield(L, -2, "keyCode") ;
}
if ((type == NSEventTypeLeftMouseDown) || (type == NSEventTypeLeftMouseUp) || (type == NSEventTypeRightMouseDown) || (type == NSEventTypeRightMouseUp) || (type == NSEventTypeOtherMouseDown) || (type == NSEventTypeOtherMouseUp)) {
lua_pushinteger(L, [sysEvent buttonNumber]) ; lua_setfield(L, -2, "buttonNumber") ;
lua_pushinteger(L, [sysEvent clickCount]) ; lua_setfield(L, -2, "clickCount") ;
lua_pushnumber(L, (lua_Number)[sysEvent pressure]) ; lua_setfield(L, -2, "pressure") ;
}
if ((type == NSEventTypeAppKitDefined) || (type == NSEventTypeSystemDefined) || (type == NSEventTypeApplicationDefined) || (type == NSEventTypePeriodic)) {
lua_pushinteger(L, [sysEvent data1]) ; lua_setfield(L, -2, "data1") ;
lua_pushinteger(L, [sysEvent data2]) ; lua_setfield(L, -2, "data2") ;
lua_pushinteger(L, [sysEvent subtype]) ; lua_setfield(L, -2, "subtype") ;
}
}
lua_setfield(L, -2, "NSEventData") ;
return 1;
}
/// hs.eventtap.event:getCharacters([clean]) -> string or nil
/// Method
/// Returns the Unicode character, if any, represented by a keyDown or keyUp event.
///
/// Parameters:
/// * clean -- an optional parameter, default `false`, which indicates if key modifiers, other than Shift, should be stripped from the keypress before converting to Unicode.
///
/// Returns:
/// * A string containing the Unicode character represented by the keyDown or keyUp event, or nil if the event is not a keyUp or keyDown.
///
/// Notes:
/// * This method should only be used on keyboard events
/// * If `clean` is true, all modifiers except for Shift are stripped from the character before converting to the Unicode character represented by the keypress.
/// * If the keypress does not correspond to a valid Unicode character, an empty string is returned (e.g. if `clean` is false, then Opt-E will return an empty string, while Opt-Shift-E will return an accent mark).
static int eventtap_event_getCharacters(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
BOOL clean = lua_isnone(L, 2) ? NO : (BOOL)(lua_toboolean(L, 2)) ;
CGEventType cgType = CGEventGetType(event) ;
if ((cgType == kCGEventKeyDown) || (cgType == kCGEventKeyUp)) {
if (clean)
lua_pushstring(L, [[[NSEvent eventWithCGEvent:event] charactersIgnoringModifiers] UTF8String]) ;
else
lua_pushstring(L, [[[NSEvent eventWithCGEvent:event] characters] UTF8String]) ;
} else {
lua_pushnil(L) ;
}
return 1;
}
/// hs.eventtap.event:getKeyCode() -> keycode
/// Method
/// Gets the raw keycode for the event
///
/// Parameters:
/// * None
///
/// Returns:
/// * A number containing the raw keycode, taken from `hs.keycodes.map`
///
/// Notes:
/// * This method should only be used on keyboard events
static int eventtap_event_getKeyCode(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
lua_pushinteger(L, CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode));
return 1;
}
/// hs.eventtap.event:setKeyCode(keycode)
/// Method
/// Sets the raw keycode for the event
///
/// Parameters:
/// * keycode - A number containing a raw keycode, taken from `hs.keycodes.map`
///
/// Returns:
/// * The `hs.eventtap.event` object
///
/// Notes:
/// * This method should only be used on keyboard events
static int eventtap_event_setKeyCode(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGKeyCode keycode = (CGKeyCode)luaL_checkinteger(L, 2);
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, (int64_t)keycode);
lua_settop(L,1) ;
return 1;
}
/// hs.eventtap.event:getUnicodeString()
/// Method
/// Gets the single unicode character of an event
///
/// Parameters:
/// * None
///
/// Returns:
/// * A string containing the unicode character
static int eventtap_event_getUnicodeString(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TBREAK];
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
UniChar *buffer;
UniCharCount actual = 0;
// Get the length of the string
CGEventKeyboardGetUnicodeString(event, 0, &actual, NULL);
buffer = malloc(actual * sizeof(UniChar));
CGEventKeyboardGetUnicodeString(event, actual, &actual, buffer);
// Convert buffer -> NSString
NSString *theString = [NSString stringWithCharacters:buffer length:actual];
free(buffer);
[skin pushNSObject:theString];
return 1;
}
/// hs.eventtap.event:setUnicodeString(string)
/// Method
/// Sets a unicode string as the output of the event
///
/// Parameters:
/// * string - A string containing unicode characters, which will be applied to the event
///
/// Returns:
/// * The `hs.eventtap.event` object
///
/// Notes:
/// * Calling this method will reset any flags previously set on the event (because they don't make any sense, and you should not try to set flags again)
/// * This is likely to only work with short unicode strings that resolve to a single character
static int eventtap_event_setUnicodeString(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TSTRING, LS_TBREAK];
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
NSString *theString = [skin toNSObjectAtIndex:2];
NSUInteger stringLen = [theString lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
NSUInteger usedLen = 0;
UniChar *buffer = malloc(stringLen);
BOOL result = [theString getBytes:(void*)buffer
maxLength:stringLen
usedLength:&usedLen
encoding:NSUnicodeStringEncoding
options:NSStringEncodingConversionAllowLossy
range:NSMakeRange(0, theString.length)
remainingRange:NULL];
if (!result) {
[skin logWarn:[NSString stringWithFormat:@"hs.eventtap.event:setUnicodeString() failed to convert: %@", theString]];
}
CGEventSetFlags(event, (CGEventFlags)0);
CGEventKeyboardSetUnicodeString(event, theString.length, buffer);
free(buffer);
lua_settop(L, 1);
return 1;
}
/// hs.eventtap.event:post([app])
/// Method
/// Posts the event to the OS - i.e. emits the keyboard/mouse input defined by the event
///
/// Parameters:
/// * app - An optional `hs.application` object. If specified, the event will only be sent to that application
///
/// Returns:
/// * The `hs.eventtap.event` object
static int eventtap_event_post(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TANY | LS_TOPTIONAL, LS_TBREAK];
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
if (luaL_testudata(L, 2, APPLICATION_USERDATA_TAG)) {
HSapplication *appObj = [skin toNSObjectAtIndex:2] ;
pid_t pid = appObj.pid;
ProcessSerialNumber psn;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSStatus err = GetProcessForPID(pid, &psn);
#pragma clang diagnostic pop
if (err != noErr) {
NSLog(@"ERROR: Unable to get PSN for PID: %d", pid);
} else {
CGEventPostToPSN(&psn, event);
}
}
else {
// NOTE: @latenitefilms has tried to use `kCGHIDEventTap` as discussed in #2104
// however, it doesn't seem to be any different than `kCGSessionEventTap`
CGEventPost(kCGSessionEventTap, event);
}
usleep(1000);
lua_settop(L, 1) ;
return 1 ;
}
/// hs.eventtap.event:getType([nsSpecificType]) -> number
/// Method
/// Gets the type of the event
///
/// Parameters:
/// * `nsSpecificType` - an optional boolean, default false, specifying whether or not a more specific Cocoa NSEvent type should be returned, if available.
///
/// Returns:
/// * A number containing the type of the event, taken from `hs.eventtap.event.types`
///
/// Notes:
/// * some newer events are grouped into a more generic event for watching purposes and the specific event type is determined by examining the event through the Cocoa API. The primary example of this is for gestures on a trackpad or touches of the touchbar, as all of these are grouped under the `hs.eventtap.event.types.gesture` event. For example:
/// ```lua
/// myTap = hs.eventtap.new( { hs.eventtap.event.types.gesture }, function(e)
/// local gestureType = e:getType(true)
/// if gestureType == hs.eventtap.types.directTouch then
/// -- they touched the touch bar
/// elseif gestureType == hs.eventtap.types.gesture then
/// -- they are touching the trackpad, but it's not for a gesture
/// elseif gestureType == hs.eventtap.types.magnify then
/// -- they're preforming a magnify gesture
/// -- etc -- see hs.eventtap.event.types for more
/// endif
/// end
/// ```
static int eventtap_event_getType(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TUSERDATA, EVENT_USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK] ;
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
BOOL nsEvent = (lua_gettop(L) > 1) ? (BOOL)(lua_toboolean(L, 2)) : NO ;
if (nsEvent) {
NSEvent *cocoaEvent = [NSEvent eventWithCGEvent:event] ;
NSUInteger eventType = [cocoaEvent type] ;
lua_pushinteger(L, (lua_Integer)(eventType)) ;
} else {
lua_pushinteger(L, CGEventGetType(event));
}
return 1;
}
/// hs.eventtap.event:getProperty(prop) -> number
/// Method
/// Gets a property of the event
///
/// Parameters:
/// * prop - A value taken from `hs.eventtap.event.properties`
///
/// Returns:
/// * A number containing the value of the requested property
///
/// Notes:
/// * The properties are `CGEventField` values, as documented at https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/tdef/CGEventField
static int eventtap_event_getProperty(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGEventField field = (CGEventField)(luaL_checkinteger(L, 2));
if ((field == kCGMouseEventPressure) || // These fields use a double (floating point number)
(field == kCGScrollWheelEventFixedPtDeltaAxis1) ||
(field == kCGScrollWheelEventFixedPtDeltaAxis2) ||
(field == kCGScrollWheelEventFixedPtDeltaAxis3) ||
(field == kCGTabletEventPointPressure) ||
(field == kCGTabletEventTiltX) ||
(field == kCGTabletEventTiltY) ||
(field == kCGTabletEventRotation) ||
(field == kCGTabletEventTangentialPressure)) {
lua_pushnumber(L, CGEventGetDoubleValueField(event, field));
} else {
lua_pushinteger(L, CGEventGetIntegerValueField(event, field));
}
return 1;
}
/// hs.eventtap.event:getButtonState(button) -> bool
/// Method
/// Gets the state of a mouse button in the event
///
/// Parameters:
/// * button - A number between 0 and 31. The left mouse button is 0, the right mouse button is 1 and the middle mouse button is 2. The meaning of the remaining buttons varies by hardware, and their functionality varies by application (typically they are not present on a mouse and have no effect in an application)
///
/// Returns:
/// * A boolean, true if the specified mouse button is to be clicked by the event
///
/// Notes:
/// * This method should only be called on mouse events
static int eventtap_event_getButtonState(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGMouseButton whichButton = (CGMouseButton)(luaL_checkinteger(L, 2));
if (CGEventSourceButtonState((CGEventSourceStateID)(CGEventGetIntegerValueField(event, kCGEventSourceStateID)), whichButton))
lua_pushboolean(L, YES) ;
else
lua_pushboolean(L, NO) ;
return 1;
}
/// hs.eventtap.event:setProperty(prop, value)
/// Method
/// Sets a property of the event
///
/// Parameters:
/// * prop - A value from `hs.eventtap.event.properties`
/// * value - A number containing the value of the specified property
///
/// Returns:
/// * The `hs.eventtap.event` object.
///
/// Notes:
/// * The properties are `CGEventField` values, as documented at https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/tdef/CGEventField
/// * Integer values use `CGEventSetIntegerValueField` and number values use `CGEventSetDoubleValueField`.
static int eventtap_event_setProperty(lua_State* L) {
CGEventRef event = *(CGEventRef*)luaL_checkudata(L, 1, EVENT_USERDATA_TAG);
CGEventField field = (CGEventField)(luaL_checkinteger(L, 2));
if ((field == kCGMouseEventPressure) || // These fields use a double (floating point number)
(field == kCGScrollWheelEventFixedPtDeltaAxis1) ||
(field == kCGScrollWheelEventFixedPtDeltaAxis2) ||
(field == kCGScrollWheelEventFixedPtDeltaAxis3) ||
(field == kCGTabletEventPointPressure) ||
(field == kCGTabletEventTiltX) ||
(field == kCGTabletEventTiltY) ||
(field == kCGTabletEventRotation) ||
(field == kCGTabletEventTangentialPressure)) {
double value = luaL_checknumber(L, 3) ;
CGEventSetDoubleValueField(event, field, value);
} else if (lua_isinteger(L, 3)) {
int64_t value = (int64_t)lua_tointeger(L, 3);
CGEventSetIntegerValueField(event, field, value);
} else if (lua_isnumber(L, 3)) {
double value = lua_tonumber(L, 3) ;
CGEventSetDoubleValueField(event, field, value);
} else {
[LuaSkin logError:@"hs.eventtap.event:setProperty() - Invalid value type."];
}
lua_settop(L,1) ;
return 1;
}
/// hs.eventtap.event.newKeyEvent([mods], key, isdown) -> event
/// Constructor
/// Creates a keyboard event
///
/// Parameters:
/// * mods - An optional table containing zero or more of the following:
/// * cmd
/// * alt
/// * shift
/// * ctrl
/// * fn
/// * key - A string containing the name of a key (see `hs.hotkey` for more information) or an integer specifying the virtual keycode for the key.
/// * isdown - A boolean, true if the event should be a key-down, false if it should be a key-up
///
/// Returns:
/// * An `hs.eventtap.event` object
///
/// Notes:
/// * The original version of this constructor utilized a shortcut which merged `flagsChanged` and `keyUp`/`keyDown` events into one. This approach is still supported for backwards compatibility and because it *does* work in most cases.
/// * According to Apple Documentation, the proper way to perform a keypress with modifiers is through multiple key events; for example to generate 'Å', you should do the following:
/// ~~~lua
/// hs.eventtap.event.newKeyEvent(hs.keycodes.map.shift, true):post()
/// hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, true):post()
/// hs.eventtap.event.newKeyEvent("a", true):post()
/// hs.eventtap.event.newKeyEvent("a", false):post()
/// hs.eventtap.event.newKeyEvent(hs.keycodes.map.alt, false):post()
/// hs.eventtap.event.newKeyEvent(hs.keycodes.map.shift, false):post()
/// ~~~
/// * The shortcut method is still supported, though if you run into odd behavior or need to generate `flagsChanged` events without a corresponding `keyUp` or `keyDown`, please check out the syntax demonstrated above.
/// ~~~lua
/// hs.eventtap.event.newKeyEvent({"shift", "alt"}, "a", true):post()
/// hs.eventtap.event.newKeyEvent({"shift", "alt"}, "a", false):post()
/// ~~~
/// * The shortcut approach is still limited to generating only the left version of modifiers.
static int eventtap_event_newKeyEvent(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
BOOL hasModTable = NO ;
int keyCodePos = 2 ;
CGEventFlags flags = (CGEventFlags)0;
if (lua_type(L, 1) == LUA_TTABLE) {
[skin checkArgs:LS_TTABLE, LS_TNUMBER | LS_TINTEGER, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK] ;
const char *modifier;
lua_pushnil(L);
while (lua_next(L, 1) != 0) {
modifier = lua_tostring(L, -1);
if (!modifier) {
[skin logBreadcrumb:[NSString stringWithFormat:@"hs.eventtap.event.newKeyEvent() unexpected entry in modifiers table: %d", lua_type(L, -1)]];
lua_pop(L, 1);
continue;
}
if (strcmp(modifier, "cmd") == 0 || strcmp(modifier, "⌘") == 0) flags |= kCGEventFlagMaskCommand;
else if (strcmp(modifier, "ctrl") == 0 || strcmp(modifier, "⌃") == 0) flags |= kCGEventFlagMaskControl;
else if (strcmp(modifier, "alt") == 0 || strcmp(modifier, "⌥") == 0) flags |= kCGEventFlagMaskAlternate;
else if (strcmp(modifier, "shift") == 0 || strcmp(modifier, "⇧") == 0) flags |= kCGEventFlagMaskShift;
else if (strcmp(modifier, "fn") == 0) flags |= kCGEventFlagMaskSecondaryFn;
lua_pop(L, 1);
}
hasModTable = YES ;
} else if (lua_type(L, 1) == LUA_TNIL) {
[skin checkArgs:LS_TNIL, LS_TNUMBER | LS_TINTEGER, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK] ;
} else {
[skin checkArgs:LS_TNUMBER | LS_TINTEGER, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK] ;
keyCodePos = 1 ;
}
BOOL isDown = (BOOL)(lua_toboolean(L, keyCodePos + 1)) ;
CGKeyCode keyCode = (CGKeyCode)lua_tointeger(L, keyCodePos) ;
CGEventRef keyevent = CGEventCreateKeyboardEvent(eventSource, keyCode, isDown);
if (hasModTable) CGEventSetFlags(keyevent, flags);
new_eventtap_event(L, keyevent);
CFRelease(keyevent);
return 1;
}
/// hs.eventtap.event.newSystemKeyEvent(key, isdown) -> event
/// Constructor
/// Creates a keyboard event for special keys (e.g. media playback)
///
/// Parameters:
/// * key - A string containing the name of a special key. The possible names are:
/// * SOUND_UP
/// * SOUND_DOWN
/// * MUTE
/// * BRIGHTNESS_UP
/// * BRIGHTNESS_DOWN
/// * CONTRAST_UP
/// * CONTRAST_DOWN
/// * POWER
/// * LAUNCH_PANEL
/// * VIDMIRROR
/// * PLAY
/// * EJECT
/// * NEXT
/// * PREVIOUS
/// * FAST
/// * REWIND
/// * ILLUMINATION_UP
/// * ILLUMINATION_DOWN
/// * ILLUMINATION_TOGGLE
/// * CAPS_LOCK
/// * HELP
/// * NUM_LOCK
/// * isdown - A boolean, true if the event should be a key-down, false if it should be a key-up
///
/// Returns:
/// * An `hs.eventtap.event` object
///
/// Notes:
/// * To set modifiers on a system key event (e.g. cmd/ctrl/etc), see the `hs.eventtap.event:setFlags()` method
/// * The event names are case sensitive
static int eventtap_event_newSystemKeyEvent(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TSTRING, LS_TBOOLEAN, LS_TBREAK];
NSString *keyName = [skin toNSObjectAtIndex:1];
BOOL isDown = (BOOL)(lua_toboolean(L, 2));
int keyVal = -1;
if ([keyName isEqualToString:@"SOUND_UP"]) {
keyVal = NX_KEYTYPE_SOUND_UP;
} else if ([keyName isEqualToString:@"SOUND_DOWN"]) {
keyVal = NX_KEYTYPE_SOUND_DOWN;
} else if ([keyName isEqualToString:@"POWER"]) {
keyVal = NX_POWER_KEY;
} else if ([keyName isEqualToString:@"MUTE"]) {
keyVal = NX_KEYTYPE_MUTE;
} else if ([keyName isEqualToString:@"BRIGHTNESS_UP"]) {
keyVal = NX_KEYTYPE_BRIGHTNESS_UP;
} else if ([keyName isEqualToString:@"BRIGHTNESS_DOWN"]) {
keyVal = NX_KEYTYPE_BRIGHTNESS_DOWN;
} else if ([keyName isEqualToString:@"CONTRAST_UP"]) {
keyVal = NX_KEYTYPE_CONTRAST_UP;
} else if ([keyName isEqualToString:@"CONTRAST_DOWN"]) {
keyVal = NX_KEYTYPE_CONTRAST_DOWN;
} else if ([keyName isEqualToString:@"LAUNCH_PANEL"]) {
keyVal = NX_KEYTYPE_LAUNCH_PANEL;
} else if ([keyName isEqualToString:@"EJECT"]) {
keyVal = NX_KEYTYPE_EJECT;
} else if ([keyName isEqualToString:@"VIDMIRROR"]) {
keyVal = NX_KEYTYPE_VIDMIRROR;
} else if ([keyName isEqualToString:@"PLAY"]) {
keyVal = NX_KEYTYPE_PLAY;
} else if ([keyName isEqualToString:@"NEXT"]) {
keyVal = NX_KEYTYPE_NEXT;
} else if ([keyName isEqualToString:@"PREVIOUS"]) {
keyVal = NX_KEYTYPE_PREVIOUS;
} else if ([keyName isEqualToString:@"FAST"]) {
keyVal = NX_KEYTYPE_FAST;
} else if ([keyName isEqualToString:@"REWIND"]) {
keyVal = NX_KEYTYPE_REWIND;
} else if ([keyName isEqualToString:@"ILLUMINATION_UP"]) {
keyVal = NX_KEYTYPE_ILLUMINATION_UP;
} else if ([keyName isEqualToString:@"ILLUMINATION_DOWN"]) {
keyVal = NX_KEYTYPE_ILLUMINATION_DOWN;
} else if ([keyName isEqualToString:@"ILLUMINATION_TOGGLE"]) {
keyVal = NX_KEYTYPE_ILLUMINATION_TOGGLE;
} else if ([keyName isEqualToString:@"CAPS_LOCK"]) {
keyVal = NX_KEYTYPE_CAPS_LOCK;
} else if ([keyName isEqualToString:@"HELP"]) {