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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
- (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(&keyboard;)) return NO;
    // get the description of the current keyboard layout
    if(KLGetKeyboardLayoutProperty(keyboard, kKLKCHRData, &keyboardData;)) 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;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: JDK7目录监视服务

下一篇:iOS视图淡入淡出效果