CGEventRef KeyHandler - Xcode - Mac

时间:2016-02-22 22:34:20

标签: objective-c xcode macos keyboard cgeventtap

所以我有一个应用程序,当您按下键盘上的数字时,它通常会键入符号。 但是通过应用程序,它可以输入数字。

但是如果我在xcode中运行它,一切正常,但是当我在xcode之外打开应用程序时它不起作用。

我有mac os x和xcode的最新更新。

视频:

https://youtu.be/67hRybEmJJY

有人可以帮帮我吗?

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

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


NSString *rusLetters1 = @"&";
if (chars[0] == [rusLetters1 characterAtIndex:0]) {
    chars[0] = '1';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters2 = @"é";
if (chars[0] == [rusLetters2 characterAtIndex:0]) {
    chars[0] = '2';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters3 = @"\"";
if (chars[0] == [rusLetters3 characterAtIndex:0]) {
    chars[0] = '3';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters4 = @"'";
if (chars[0] == [rusLetters4 characterAtIndex:0]) {
    chars[0] = '4';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters5 = @"(";
if (chars[0] == [rusLetters5 characterAtIndex:0]) {
    chars[0] = '5';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters6 = @"§";
if (chars[0] == [rusLetters6 characterAtIndex:0]) {
    chars[0] = '6';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters7 = @"è";
if (chars[0] == [rusLetters7 characterAtIndex:0]) {
    chars[0] = '7';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters8 = @"!";
if (chars[0] == [rusLetters8 characterAtIndex:0]) {
    chars[0] = '8';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters9 = @"ç";
if (chars[0] == [rusLetters9 characterAtIndex:0]) {
    chars[0] = '9';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters0 = @"à";
if (chars[0] == [rusLetters0 characterAtIndex:0]) {
    chars[0] = '0';
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}

//_________________________________________________________________________________________________________________

NSString *rusLetters11 = @"&";
if (chars[0] == '1') {
    chars[0] = [rusLetters11 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters12 = @"é";
if (chars[0] == '2') {
    chars[0] = [rusLetters12 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters13 = @"\"";
if (chars[0] == '3') {
    chars[0] = [rusLetters13 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters14 = @"'";
if (chars[0] == '4') {
    chars[0] = [rusLetters14 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters15 = @"(";
if (chars[0] == '5') {
    chars[0] = [rusLetters15 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters16 = @"§";
if (chars[0] == '6') {
    chars[0] = [rusLetters16 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters17 = @"è";
if (chars[0] == '7') {
    chars[0] = [rusLetters17 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters18 = @"!";
if (chars[0] == '8') {
    chars[0] = [rusLetters18 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters19 = @"ç";
if (chars[0] == '9') {
    chars[0] = [rusLetters19 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}
NSString *rusLetters10 = @"à";
if (chars[0] == '0') {
    chars[0] = [rusLetters10 characterAtIndex:0];
    CGEventKeyboardSetUnicodeString(event, 1, chars);
    return event;
}

return event;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    CFMachPortRef      eventTap;
    CGEventMask        eventMask;
    CFRunLoopSourceRef runLoopSource;

    eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, KeyHandler, NULL);

    if (!eventTap) {
        exit(1);
    }

    runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(eventTap, true);
    CFRunLoopRun();
}

1 个答案:

答案 0 :(得分:0)

这就是我要做的。计算修改后的角色有很多种方法,这就是其中之一。

{
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA",
   books: [12346789, 234567890, ...]
}

{
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: [ "Kristina Chodorow", "Mike Dirolf" ],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English"
}