Mac os - 鍵盤和鼠標監(jiān)聽

Mac os鍵盤和鼠標監(jiān)聽支持,也是在NSView層級別進行監(jiān)聽魁莉。

例如我現(xiàn)在需要監(jiān)聽TableView的鼠標郵件刪除或者選擇行后點擊鍵盤delete

所以第一步寫Window下的TableView實現(xiàn)。

直接貼代碼


typedef NS_ENUM(NSUInteger, FileTableViewKeyType) {
    FileTableViewKeyTypeEnterCharacter                = NSEnterCharacter,
    FileTableViewKeyTypeBackspaceCharacter            = NSBackspaceCharacter,
    FileTableViewKeyTypeTabCharacter                  = NSTabCharacter,
    FileTableViewKeyTypeNewlineCharacter              = NSNewlineCharacter,
    FileTableViewKeyTypeFormFeedCharacter             = NSFormFeedCharacter,
    FileTableViewKeyTypeCarriageReturnCharacter       = NSCarriageReturnCharacter,
    FileTableViewKeyTypeBackTabCharacter              = NSBackTabCharacter,
    FileTableViewKeyTypeDeleteCharacter               = NSDeleteCharacter,
    FileTableViewKeyTypeLineSeparatorCharacter        = NSLineSeparatorCharacter,
    FileTableViewKeyTypeParagraphSeparatorCharacter   = NSParagraphSeparatorCharacter
};


@protocol FileTableViewEventDelegate <NSObject>
@optional
- (void)tableView:(NSTableView *)tableView didClickDownAtKeyType:(FileTableViewKeyType)keyType indexSet:(NSIndexSet *)indexSet;;

- (void)tableView:(NSTableView *)tableView didClickDeleteAtIndexSet:(NSIndexSet *)indexSet;

@end

@interface FileTableView : NSTableView
@property (nullable, weak) id <FileTableViewEventDelegate> eventDelegate;
@end


#import "FileTableView.h"

@interface FileTableView()

@end

@implementation FileTableView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (void)keyDown:(NSEvent *)event {
    unichar character = [[event characters] characterAtIndex: 0];
    if([self.eventDelegate respondsToSelector:@selector(tableView:didClickDownAtKeyType:indexSet:)]) {
        [self.eventDelegate tableView:self didClickDownAtKeyType:character indexSet:[self selectedRowIndexes]];
    }
}


- (NSMenu *)menuForEvent:(NSEvent *)event {
    if (event.type == NSEventTypeRightMouseDown) {
        NSPoint menuPoint = [self convertPoint:[event locationInWindow] fromView:nil];
        NSInteger row = [self rowAtPoint:menuPoint];
        NSIndexSet *indexSet = [self selectedRowIndexes];
        if (row == -1 || [indexSet count] == 0) {
            return nil;
        }
        
        if ([indexSet containsIndex:row]) {
            NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Custom"];
            NSMenuItem *deleteItem = [[NSMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteAction:) keyEquivalent:@""];
            [menu addItem:deleteItem];
            return menu;
        }
        return nil;
    }
    return nil;
}

- (void)deleteAction:(NSMenuItem *)item {
    NSIndexSet *indexSet = [self selectedRowIndexes];
    if([self.eventDelegate respondsToSelector:@selector(tableView:didClickDeleteAtIndexSet:)]) {
        [self.eventDelegate tableView:self didClickDeleteAtIndexSet:indexSet];
    }
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末镐确,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子饼煞,更是在濱河造成了極大的恐慌源葫,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砖瞧,死亡現(xiàn)場離奇詭異息堂,居然都是意外死亡,警方通過查閱死者的電腦和手機块促,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門荣堰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人竭翠,你說我怎么就攤上這事振坚。” “怎么了斋扰?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵渡八,是天一觀的道長。 經(jīng)常有香客問我传货,道長屎鳍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任问裕,我火速辦了婚禮哥艇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘僻澎。我一直安慰自己貌踏,他們只是感情好十饥,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著祖乳,像睡著了一般逗堵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上眷昆,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天蜒秤,我揣著相機與錄音,去河邊找鬼亚斋。 笑死作媚,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的帅刊。 我是一名探鬼主播纸泡,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼赖瞒!你這毒婦竟也來了女揭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤栏饮,失蹤者是張志新(化名)和其女友劉穎吧兔,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體袍嬉,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡境蔼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了伺通。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箍土。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖泵殴,靈堂內(nèi)的尸體忽然破棺而出涮帘,到底是詐尸還是另有隱情,我是刑警寧澤笑诅,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布调缨,位于F島的核電站,受9級特大地震影響吆你,放射性物質(zhì)發(fā)生泄漏弦叶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一妇多、第九天 我趴在偏房一處隱蔽的房頂上張望伤哺。 院中可真熱鬧,春花似錦、人聲如沸立莉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蜓耻。三九已至茫舶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間刹淌,已是汗流浹背饶氏。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留有勾,地道東北人疹启。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像蔼卡,于是被迫代替她去往敵國和親喊崖。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容