Domanda

(This question is similar to the one I recently posted here except that now I'm using CGEventPost to simulate more keystrokes instead of modifying current event)

The code bellow was able to insert an 'a' character every time the 'h' button is pressed, it worked fine with all the applications.

CGEventRef KeyHandler(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    UniCharCount actualStringLength;
    UniCharCount maxStringLength = 1;   
    UniChar chars[3];

    CGEventKeyboardGetUnicodeString(event, maxStringLength, &actualStringLength, chars);

    if (chars[0] == 'h') {

        CGEventRef keyEventDown = CGEventCreateKeyboardEvent( NULL, 1, true);
        CGEventRef keyEventUp = CGEventCreateKeyboardEvent( NULL, 1, false);

        chars[0] = 'a';
        CGEventKeyboardSetUnicodeString(keyEventDown, 1, chars);
        CGEventKeyboardSetUnicodeString(keyEventUp, 1, chars);
        CGEventSetIntegerValueField(keyEventDown, kCGKeyboardEventKeycode, 0); // 0 is key code of 'a' button
        CGEventSetIntegerValueField(keyEventUp, kCGKeyboardEventKeycode, 0);

        CGEventPost(kCGSessionEventTap, keyEventDown);
        CGEventPost(kCGSessionEventTap, keyEventUp);
        return NULL;
    }

    return event;
}

If I replace 'a' with backspace character, some applications (e.g. Microsoft Excel on Mac ... ) don't recognize it anymore. Do you have any idea why ?

CGEventRef KeyHandler(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
    UniCharCount actualStringLength;
    UniCharCount maxStringLength = 1;   
    UniChar chars[3];

    CGEventKeyboardGetUnicodeString(event, maxStringLength, &actualStringLength, chars);

    if (chars[0] == 'h') {

        CGEventRef keyEventDown = CGEventCreateKeyboardEvent( NULL, 1, true);
        CGEventRef keyEventUp = CGEventCreateKeyboardEvent( NULL, 1, false);

        chars[0] = '\b';
        CGEventKeyboardSetUnicodeString(keyEventDown, 1, chars);
        CGEventKeyboardSetUnicodeString(keyEventUp, 1, chars);
        CGEventSetIntegerValueField(keyEventDown, kCGKeyboardEventKeycode, 51);
        CGEventSetIntegerValueField(keyEventUp, kCGKeyboardEventKeycode, 51);

        CGEventPost(kCGSessionEventTap, keyEventDown);
        CGEventPost(kCGSessionEventTap, keyEventUp);
        return NULL;       
    }

    return event;
}
È stato utile?

Soluzione

Figured it out. If I remove the code to set unicode string for the events, everything will work just fine:

chars[0] = '\b';
CGEventKeyboardSetUnicodeString(keyEventDown, 1, chars);
CGEventKeyboardSetUnicodeString(keyEventUp, 1, chars);

However, I have no idea why this happens to some specific applications on Mac OS.

Altri suggerimenti

The "\b" escape sequence produces ASCII 0x08. In my testing, the event generated when I press the delete (i.e. backspace) key is ASCII 0x7f.

In general, that seems the obvious technique to use to figure out how to simulate an event. Perform that event with a tool that captures it and dumps its specifics.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top