diff options
| author | Teddy Wing | 2018-08-27 18:29:44 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2018-08-27 18:29:44 +0200 | 
| commit | f4c4e70ac7b86b9b0c64a4786adc1d9e4b50097c (patch) | |
| tree | 2024dfad2e0e726616ccee92928b2042a5352a21 | |
| parent | 62729cbbc786943eff6093ceb4b13146470295ec (diff) | |
| download | DomeKey-f4c4e70ac7b86b9b0c64a4786adc1d9e4b50097c.tar.bz2 | |
Add char_to_key_code.m
This file includes functions for getting a `CGKeyCode` from a character
reference.
Copied from Stack Overflow by Théo Winterhalter.
| -rw-r--r-- | DomeKey.xcodeproj/project.pbxproj | 13 | ||||
| -rw-r--r-- | lib/char_to_key_code.m | 68 | 
2 files changed, 81 insertions, 0 deletions
| diff --git a/DomeKey.xcodeproj/project.pbxproj b/DomeKey.xcodeproj/project.pbxproj index da85841..fb73c83 100644 --- a/DomeKey.xcodeproj/project.pbxproj +++ b/DomeKey.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@  		D11184622125206E00961687 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D11184612125206E00961687 /* AppDelegate.m */; };  		D160C2A12118EF9D007D1B50 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D160C2A02118EF9D007D1B50 /* main.m */; };  		D1E101FE2133FA0300B2CA29 /* KeyboardSimulator.m in Sources */ = {isa = PBXBuildFile; fileRef = D1E101FD2133FA0300B2CA29 /* KeyboardSimulator.m */; }; +		D1E1020821345E2300B2CA29 /* char_to_key_code.m in Sources */ = {isa = PBXBuildFile; fileRef = D1E1020721345E2200B2CA29 /* char_to_key_code.m */; };  /* End PBXBuildFile section */  /* Begin PBXContainerItemProxy section */ @@ -74,6 +75,7 @@  		D160C31521197983007D1B50 /* DDHidLib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = DDHidLib.xcodeproj; path = lib/DDHidLib/DDHidLib.xcodeproj; sourceTree = "<group>"; };  		D1E101FC2133FA0300B2CA29 /* KeyboardSimulator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyboardSimulator.h; sourceTree = "<group>"; };  		D1E101FD2133FA0300B2CA29 /* KeyboardSimulator.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KeyboardSimulator.m; sourceTree = "<group>"; }; +		D1E1020721345E2200B2CA29 /* char_to_key_code.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = char_to_key_code.m; path = lib/char_to_key_code.m; sourceTree = SOURCE_ROOT; };  /* End PBXFileReference section */  /* Begin PBXFrameworksBuildPhase section */ @@ -92,6 +94,7 @@  			isa = PBXGroup;  			children = (  				D160C29F2118EF9D007D1B50 /* DomeKey */, +				D1E1020621345DF100B2CA29 /* lib */,  				D160C31421197982007D1B50 /* Frameworks */,  				D160C29E2118EF9D007D1B50 /* Products */,  			); @@ -138,6 +141,15 @@  			name = Products;  			sourceTree = "<group>";  		}; +		D1E1020621345DF100B2CA29 /* lib */ = { +			isa = PBXGroup; +			children = ( +				D1E1020721345E2200B2CA29 /* char_to_key_code.m */, +			); +			name = lib; +			path = DomeKey/lib; +			sourceTree = "<group>"; +		};  /* End PBXGroup section */  /* Begin PBXNativeTarget section */ @@ -235,6 +247,7 @@  			files = (  				D110C9482122E2D80094F963 /* HeadphoneKey.m in Sources */,  				D160C2A12118EF9D007D1B50 /* main.m in Sources */, +				D1E1020821345E2300B2CA29 /* char_to_key_code.m in Sources */,  				D11184622125206E00961687 /* AppDelegate.m in Sources */,  				D1E101FE2133FA0300B2CA29 /* KeyboardSimulator.m in Sources */,  			); diff --git a/lib/char_to_key_code.m b/lib/char_to_key_code.m new file mode 100644 index 0000000..b4fcdec --- /dev/null +++ b/lib/char_to_key_code.m @@ -0,0 +1,68 @@ +// Théo Winterhalter +// https://stackoverflow.com/users/1275975/th%c3%a9o-winterhalter +// https://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode/33584460#33584460 + +NSString* keyCodeToString(CGKeyCode keyCode) +{ +  TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); +  CFDataRef uchr = +    (CFDataRef)TISGetInputSourceProperty(currentKeyboard, +                                         kTISPropertyUnicodeKeyLayoutData); +  const UCKeyboardLayout *keyboardLayout = +    (const UCKeyboardLayout*)CFDataGetBytePtr(uchr); + +  if(keyboardLayout) +  { +    UInt32 deadKeyState = 0; +    UniCharCount maxStringLength = 255; +    UniCharCount actualStringLength = 0; +    UniChar unicodeString[maxStringLength]; + +    OSStatus status = UCKeyTranslate(keyboardLayout, +                                     keyCode, kUCKeyActionDown, 0, +                                     LMGetKbdType(), 0, +                                     &deadKeyState, +                                     maxStringLength, +                                     &actualStringLength, unicodeString); + +    if (actualStringLength == 0 && deadKeyState) +    { +      status = UCKeyTranslate(keyboardLayout, +                                       kVK_Space, kUCKeyActionDown, 0, +                                       LMGetKbdType(), 0, +                                       &deadKeyState, +                                       maxStringLength, +                                       &actualStringLength, unicodeString); +    } +    if(actualStringLength > 0 && status == noErr) +      return [[NSString stringWithCharacters:unicodeString +                        length:(NSUInteger)actualStringLength] lowercaseString]; +  } + +  return nil; +} + +NSNumber* charToKeyCode(const char c) +{ +  static NSMutableDictionary* dict = nil; + +  if (dict == nil) +  { +    dict = [NSMutableDictionary dictionary]; + +    // For every keyCode +    size_t i; +    for (i = 0; i < 128; ++i) +    { +      NSString* str = keyCodeToString((CGKeyCode)i); +      if(str != nil && ![str isEqualToString:@""]) +      { +        [dict setObject:[NSNumber numberWithInt:i] forKey:str]; +      } +    } +  } + +  NSString * keyChar = [NSString stringWithFormat:@"%c" , c]; + +  return [dict objectForKey:keyChar]; +} | 
