• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 2039次浏览

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

- (int)keyCodeForCharacter: (NSString*)character {
    if(![character length]) return -1;
    
    char code;
    BOOL shift, alt;
    if(Ascii2Virtual( (char)[character characterAtIndex: 0],shift,alt,code)) {
        return code;
    }
    return -1;
}
 
BOOL Ascii2Virtual(char pcar, BOOL *pshift, BOOL *palt, char *pkeycode)
{
    KeyboardLayoutRef keyboard;
    const void *keyboardData; // keyboard layout data
    UInt16 nbblocs;
    char *modblocs, *blocs, *deadkeys;
    int ix, ifin, numbloc, keycode;
    
    BOOL shift, alt;
    // get the current keyboard
    if(KLGetCurrentKeyboardLayout()) return NO;
    // get the description of the current keyboard layout
    if(KLGetKeyboardLayoutProperty(keyboard, kKLKCHRData, )) return NO;
    // get pointer early numbers of blocks for each combination of modifiers
    modblocs = ((char *)keyboardData) + 2;
    // get number of blocks keycode->ascii
    nbblocs = *((UInt16 *)(keyboardData + 258));
    // get pointer early blocks keycode-> ascii
    blocs = ((char *)keyboardData) + 260;
    // determining the size of all tables keycode-> ascii a scanner
    ifin = nbblocs*128;
    // determining pointer early in the tables of dead keys
    deadkeys = blocs+ifin;
    // Now it runs blocks keycode-> ascii to find the car ascii
    for (ix=0; ix<ifin ; ix++)
    {
        if (blocs[ix]==pcar)
        {
  
            // found ascii value: now we must determine which block it is
            keycode = ix & 0×7f; // 0111 1111 mask
            numbloc = ix >> 7;
            break;
        }
    }
  
    // not found: bail out (error)
    if (ix >= ifin) return NO;
  
    // from block number, we must find the combination of modifiers using this block
    for (ix=0; ix<15; ix++)
    {
        // it does not address whether the modifiers are not "capital" and "option"
        if (ix&1 || ix&4) continue;
        // Combining modifiers found for the block
        if (modblocs[ix]==numbloc)
        {
            shift = (ix&2) ? YES : NO;
            alt   = (ix&8) ? YES : NO;
            break;        
        }
    }
    // combination modifiers not found: bail
    if (ix>=15) return NO;
    // save our parameters
    *pkeycode=keycode;
    *pshift=shift;
    *palt=alt;
    
    return YES;
}
</ifin>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明objective c 将字符转换为键盘码的代码
喜欢 (0)
加载中……