Class: KeyCoder

Inherits:
Object
  • Object
show all
Defined in:
ext/key_coder/key_coder.c,
ext/key_coder/key_coder.c

Overview

Class that encapsulates some low level work for finding key code mappings and posting keyboard events to the system.

Class Method Summary collapse

Class Method Details

.dynamic_mappingHash{String=>Number}

Generate the mapping of characters to key codes for keys that can be remapped based on keyboard layout. Changing the keyboard layout at runtime will cause the returned hash to be different.

Examples:


KeyCoder.dynamic_mapping  => { "a" => 0, "b" => 24, ... }

Returns:

  • (Hash{String=>Number})


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

static
VALUE
keycoder_dynamic_mapping()
{

  VALUE map = rb_hash_new();

#ifdef NOT_MACRUBY
  @autoreleasepool {
#endif

  TISInputSourceRef keyboard = TISCopyCurrentKeyboardLayoutInputSource();
  CFDataRef layout_data = (CFDataRef)TISGetInputSourceProperty(keyboard, kTISPropertyUnicodeKeyLayoutData);
  const UCKeyboardLayout* layout = (const UCKeyboardLayout*)CFDataGetBytePtr(layout_data);

  void (^key_coder)(int) = ^(int key_code) {
    UniChar      string[255];
    UniCharCount string_length = 0;
    UInt32       dead_key_state = 0;
    UCKeyTranslate(
		   layout,
       key_code,
		   kUCKeyActionDown,
		   0,
		   LMGetKbdType(),  // kb type
		   0,               // OptionBits keyTranslateOptions,
		   &dead_key_state,
		   255,
		   &string_length,
		   string
		  );

    NSString* nsstring = [NSString stringWithCharacters:string length:string_length];
    rb_hash_aset(map, rb_str_new_cstr([nsstring UTF8String]), INT2FIX(key_code));
  };

  // skip 65-92 since they are hard coded and do not change
  for (int key_code = 0;  key_code < 65;  key_code++)
    key_coder(key_code);
  for (int key_code = 93; key_code < 127; key_code++)
    key_coder(key_code);

  RELEASE(keyboard);

#ifdef NOT_MACRUBY
  }; // Close the autorelease pool
#endif


  return map;
}

.post_event(event) ⇒ true

Post the given event to the system and return true. This method will also add a small (9000 microsecond) delay after posting to ensure that keyboard actions do not go too fast.

Examples:


KeyCoder.post_event [0, true]  -> true

Parameters:

  • (Array(Number, Boolean))

Returns:

  • (true)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/key_coder/key_coder.c', line 105

static
VALUE
keycoder_post_event(VALUE self, VALUE event)
{
  VALUE code  = rb_ary_entry(event, 0);
  VALUE state = rb_ary_entry(event, 1);

  CGEventRef event_ref = CGEventCreateKeyboardEvent(NULL, FIX2LONG(code), state);
  CGEventPost(kCGHIDEventTap, event_ref);
  RELEASE(event_ref);

  usleep(9000); // 9000 is a magic number
  return Qtrue;
}