关于Cocoa与Qt完美混编

最近在研究关于Mac下面的全局的鼠标键盘事件,并捕捉鼠标划词的功能

以下是关于Qt开发部分代码:

#include "mainwindow.h"
#include <QApplication>
#include "mainwindow.h"
#import <Cocoa/Cocoa.h>

@interface KeyLoggerApplication : NSApplication

{

                                      }

  @end

  @implementation KeyLoggerApplication

  - (BOOL)sendEvent:(NSEvent *)anEvent {

    NSEventType type = [anEvent type];

    bool handled = NO;

    if (type == NSKeyUp)

    {

        switch( [anEvent keyCode] )

        {

        default:

            NSLog(@"Keypressed: %d, **%@**", [anEvent keyCode], [anEvent characters]);

            break;

        }

    }else if(type == NSMouseMoved)
    {
        NSLog(@"Mouse: %@**", [anEvent characters]);
    }

    //handle only the keys i need then let the other go through the regular channels

    //this stops the annoying beep

    if( !handled )

        [super sendEvent:anEvent];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask | NSRightMouseDownMask | NSMouseMovedMask | NSLeftMouseDraggedMask | NSRightMouseDraggedMask handler:^(NSEvent *event)
    {
        NSString *text;
        NSInteger delta;
        NSLog(@"applicationDidFinishLaunching: %s**", "NSMouseMoved");
        switch (event.type) {
            case NSLeftMouseDown:
//                text = [[NSString alloc] initWithFormat:@"%d", ++leftClicked];
//                leftClickedText.stringValue = text;
                break;
            case NSRightMouseDown:
//                text = [[NSString alloc] initWithFormat:@"%d", ++rightClicked];
//                rightClickedText.stringValue = text;
                break;
            case NSMouseMoved:
            NSLog(@"NSMouseMoved: %s**", "NSMouseMoved");
            case NSLeftMouseDragged:
            NSLog(@"NSLeftMouseDragged: %s**", "NSLeftMouseDragged");
            case NSRightMouseDragged:
//                delta = (NSInteger)sqrt(event.deltaX * event.deltaX + event.deltaY * event.deltaY);
//                moved += delta;
//                text = [[NSString alloc] initWithFormat:@"%d px", moved];
//                movedText.stringValue = text;
             NSLog(@"NSRightMouseDragged: %s**", "NSRightMouseDragged");
                break;
            default:
                break;
        }
        [text release];
    }];
}
@end

int main(int argc, char* argv[])

{
    [KeyLoggerApplication sharedApplication];

    QApplication a(argc, argv);

    MainWindow mw;

    mw.show();

    return a.exec();

}
新建一个qt gui应用

pro里加入

mac:{
LIBS+= -framework AppKit
}

并且将main.cpp改成main.mm文件


最近在GitHub上面看到一个开源的关于Qt Mix Cocoa的项目,特此引荐Qt与Cocoa混编

你可能感兴趣的:(C++,cocoa,mac,鼠标,qt)