-2

I have Created Simple cocoa app. In which I use NSOutlineView. Now my task is to get the event of Escape Key pressed. In my appdelegate.m I implemented all require method for NSOutlineView.

Mahesh Mahajan
  • 101
  • 1
  • 11

2 Answers2

3

SubClass your NSOutlineView and capture its key down event as:

- (void)keyDown:(NSEvent *)theEvent
{       
    switch([theEvent keyCode]) 
    {
        case 53: // esc
            NSLog(@"ESC");
            // Implement the functionality you want when esc is pressed

            break;

        default:
           [super keyDown:theEvent];
    }
}
Neha
  • 1,751
  • 14
  • 36
  • Thanx. I know this way by sub classing the nsoutlineview. But I want do with Appdelegate.m only. I also tried with NSControlTextDidChangeNotification. This gives me escape event when textfield is not blank. All in vain when textfield is empty/blank. – Mahesh Mahajan Oct 22 '13 at 12:08
  • In that case you may try by registering a global keydown event, check if the key pressed is Esc and your application is front most. And if yes, then implement your functionality. – Neha Oct 22 '13 at 12:26
  • You may register for global key event as: [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event){//your code here}]; – Neha Oct 22 '13 at 12:27
  • Thanxx. But Its better to do iPhone Programming than mac programming. At least google provide lots of options to apply on problem. Mac programming has limited resources for newbie. – Mahesh Mahajan Oct 22 '13 at 13:11
0

Well you can find out the whther escape key was pressed or not try below one line and used in your method where it is required:-

NSUInteger flags = [theEvent keycode];
//After that 

If (flags==53)
{

NSlog(@"Escape key pressed");
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56