[Objective-C] objective c 将字符转换为键盘码的代码 →→→→→进入此内容的聊天室

来自 , 2020-05-08, 写在 Objective-C, 查看 115 次.
URL http://www.code666.cn/view/2adee881
  1. - (int)keyCodeForCharacter: (NSString*)character {
  2.     if(![character length]) return -1;
  3.    
  4.     char code;
  5.     BOOL shift, alt;
  6.     if(Ascii2Virtual( (char)[character characterAtIndex: 0],shift,alt,code)) {
  7.         return code;
  8.     }
  9.     return -1;
  10. }
  11.  
  12. BOOL Ascii2Virtual(char pcar, BOOL *pshift, BOOL *palt, char *pkeycode)
  13. {
  14.     KeyboardLayoutRef keyboard;
  15.     const void *keyboardData; // keyboard layout data
  16.     UInt16 nbblocs;
  17.     char *modblocs, *blocs, *deadkeys;
  18.     int ix, ifin, numbloc, keycode;
  19.    
  20.     BOOL shift, alt;
  21.     // get the current keyboard
  22.     if(KLGetCurrentKeyboardLayout(&keyboard;)) return NO;
  23.     // get the description of the current keyboard layout
  24.     if(KLGetKeyboardLayoutProperty(keyboard, kKLKCHRData, &keyboardData;)) return NO;
  25.     // get pointer early numbers of blocks for each combination of modifiers
  26.     modblocs = ((char *)keyboardData) + 2;
  27.     // get number of blocks keycode->ascii
  28.     nbblocs = *((UInt16 *)(keyboardData + 258));
  29.     // get pointer early blocks keycode-> ascii
  30.     blocs = ((char *)keyboardData) + 260;
  31.     // determining the size of all tables keycode-> ascii a scanner
  32.     ifin = nbblocs*128;
  33.     // determining pointer early in the tables of dead keys
  34.     deadkeys = blocs+ifin;
  35.     // Now it runs blocks keycode-> ascii to find the car ascii
  36.     for (ix=0; ix<ifin ; ix++)
  37.     {
  38.         if (blocs[ix]==pcar)
  39.         {
  40.  
  41.             // found ascii value: now we must determine which block it is
  42.             keycode = ix & 0×7f; // 0111 1111 mask
  43.             numbloc = ix >> 7;
  44.             break;
  45.         }
  46.     }
  47.  
  48.     // not found: bail out (error)
  49.     if (ix >= ifin) return NO;
  50.  
  51.     // from block number, we must find the combination of modifiers using this block
  52.     for (ix=0; ix<15; ix++)
  53.     {
  54.         // it does not address whether the modifiers are not "capital" and "option"
  55.         if (ix&1 || ix&4) continue;
  56.         // Combining modifiers found for the block
  57.         if (modblocs[ix]==numbloc)
  58.         {
  59.             shift = (ix&2) ? YES : NO;
  60.             alt   = (ix&8) ? YES : NO;
  61.             break;        
  62.         }
  63.     }
  64.     // combination modifiers not found: bail
  65.     if (ix>=15) return NO;
  66.     // save our parameters
  67.     *pkeycode=keycode;
  68.     *pshift=shift;
  69.     *palt=alt;
  70.    
  71.     return YES;
  72. }
  73. </ifin>
  74. //objectc/5094

回复 "objective c 将字符转换为键盘码的代码"

这儿你可以回复上面这条便签

captcha