How to grab Unicode from key events without an NSTextView

I would like to capture key events from any application window and interpret them as Unicode. For example, if the user enters the -ee option (on the English keyboard configured by default), I would like to recognize this as "é".

I tried to capture keypress events and trigger -[NSEvent characters]. However, as the documentation says, "this method returns an empty string for dead keys, such as Option-e." If I type Option-ee, then this gives me nothing for Option-e and plain "e" for the second e.

Is there a way to combine a series of (from -[NSEvent keyCode]) key codes into a Unicode character?

Or a way to get an event for every Unicode character you type (e.g. Java key-typed event )?

+4
source share
2 answers

Here you can take a series of key click events and get the Unicode characters that they typed.

Basically, call UCKeyTranslate () for each received keystroke event. Use its argument deadKeyStateto capture the dead key and pass it in for later recall.

Example:

  • Get a keypress event for the -e option.
  • Call UCKeyTranslate()using the virtual key code (for e), the state of the modifier key (for the option) and the variable to save the state of the dead key.
    • UCKeyTranslate() .
  • e.
  • UCKeyTranlate() key code ( e) , .
    • UCKeyTranslate() "é".

( ):

/**
 * Returns the Unicode characters that would be typed by a key press.
 *
 * @param event A key press event.
 * @param deadKeyState To capture multi-keystroke characters (e.g. Option-E-E for "é"), pass a reference to the same
 *      variable on consecutive calls to this function. Before the first call, you should initialize the variable to 0.
 * @return One or more Unicode characters.
 */
CFStringRef getCharactersForKeyPress(NSEvent *event, UInt32 *deadKeyState)
{
    // http://stackoverflow.com/questions/12547007/convert-key-code-into-key-equivalent-string
    // http://stackoverflow.com/questions/8263618/convert-virtual-key-code-to-unicode-string

    TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
    CFDataRef layoutData = TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
    const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);

    CGEventFlags flags = [event modifierFlags];
    UInt32 modifierKeyState = (flags >> 16) & 0xFF;

    const size_t unicodeStringLength = 4;
    UniChar unicodeString[unicodeStringLength];
    UniCharCount realLength;

    UCKeyTranslate(keyboardLayout,
                   [event keyCode],
                   kUCKeyActionDown,
                   modifierKeyState,
                   LMGetKbdType(),
                   0,
                   deadKeyState,
                   unicodeStringLength,
                   &realLength,
                   unicodeString);
    CFRelease(currentKeyboard);
    return CFStringCreateWithCharacters(kCFAllocatorDefault, unicodeString, realLength);
}
+10

: /, "é",

BOOL optionE_Pressed;

keyDown:

-(void) keyDown:(NSEvent *)theEvent {

    NSString *chars = theEvent.charactersIgnoringModifiers;
    unichar aChar = [chars characterAtIndex: 0];

    if (aChar==101 && [theEvent modifierFlags]&NSAlternateKeyMask) {
        optionE_Pressed=YES;
    }
    else if (aChar==101 && optionE_Pressed) {
        NSLog(@"spanish é pressed");
    }
    else {
        optionE_Pressed=NO;
    }

    [super keyDown:theEvent];
}

"optionE_Pressed" , e. - e, , é, " é ". Boolien NO. "" -

0

Source: https://habr.com/ru/post/1532886/


All Articles