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

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

- (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;
}