当前位置:主页 > 开发频道 > Mac开发 > Cocoa >

将字符转换为键盘码的代码

时间: 2009-06-11 20:08 点击:

以下这段代码可以将字符串转换为键盘码,以备需要时用到。 - ( int ) keyCodeForCharacter : ( NSString * ) character { if ( ! [ character length ] ) return -1 ; char code ; BOOL shift, alt; if ( Ascii2Virtual ( ( char

以下这段代码可以将字符串转换为键盘码,以备需要时用到。

  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. }

 

  1. BOOL Ascii2Virtual(char pcar, BOOL *pshift, BOOL *palt, char *pkeycode)
  2. {
  3.     KeyboardLayoutRef keyboard;
  4.     const void *keyboardData; // keyboard layout data
  5.     UInt16 nbblocs;
  6.     char *modblocs, *blocs, *deadkeys;
  7.     int ix, ifin, numbloc, keycode;
  8.    
  9.     BOOL shift, alt;
  10.     // get the current keyboard
  11.     if(KLGetCurrentKeyboardLayout(&keyboard)) return NO;
  12.     // get the description of the current keyboard layout
  13.     if(KLGetKeyboardLayoutProperty(keyboard, kKLKCHRData, &keyboardData)) return NO;
  14.     // get pointer early numbers of blocks for each combination of modifiers
  15.     modblocs = ((char *)keyboardData) + 2;
  16.     // get number of blocks keycode->ascii
  17.     nbblocs = *((UInt16 *)(keyboardData + 258));
  18.     // get pointer early blocks keycode-> ascii
  19.     blocs = ((char *)keyboardData) + 260;
  20.     // determining the size of all tables keycode-> ascii a scanner
  21.     ifin = nbblocs*128;
  22.     // determining pointer early in the tables of dead keys
  23.     deadkeys = blocs+ifin;
  24.     // Now it runs blocks keycode-> ascii to find the car ascii
  25.     for (ix=0; ix<ifin ; ix++)
  26.     {
  27.         if (

    顶一下(0)

    0%

    踩一下(0)

    0%

    发表评论

    评价: 中立 好评 差评 请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。

    表情:

论坛新帖 进入论坛...