简体   繁体   中英

How to catch a keyboard event without overriding the implement method keyDown for NSTableView?

I have implemented a custom class MyTableView that inherits from NSTableView. I then added two of this custom table view in my .xib file. What I would like to do is just navigating through both tables using arrow keys (left and right). The selected row would just simply go from one table to another (I don't know if I'm clear)

Instead of overriding the KeyDown method that would make me reimplement everything (I think being lazy is a good habit in programming :) ), I would just like to say after the classic keyDown method have been called, "Hey, if the key pressed is left or right arrow, change of table view and select me the appropriate row).

I have a MyTableViewController that controls the MyTableView class. Can I just catch keyDown event in this class and deal with these events (again without changing the KeyDown method in MyTableView)?

Generally speaking, is there a way to inherit from a method without writing everything again? Like the classes? You do not have to recreate all the class's behaviour with heritage, that would be super cool to do so with methods.

You can do this by making a menu with left-arrow key. I believe this is actually a recommended way to make a key-command because this will make the command visible to users. Here's an example.

SEL         s1  =   NSSelectorFromString(@"test1:");    
unichar     c1  =   NSLeftArrowFunctionKey;
NSMenuItem* m4  =   [[NSMenuItem alloc] initWithTitle:@"Test 1" action:s1 keyEquivalent:[NSString stringWithCharacters:&c1 length:1]];

[m4 setKeyEquivalentModifierMask:0];  // Remove need for command key.

Add this menu to the main menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM