Detecting Table Selection Change with Keyboard

I wonder if anyone knows of a way to detect and take action when the user changes the selection within a table with the keyboard… something similar to setting up a double click action with:

setDoubleAction_

But for keyboard selection changes?

Anyone?

First a setDoubleAction: doesn’t mean that the assigned method will be fired when the selection changes. It can also be fired without changing the selection. Then an NSTableView is a subclass of NSResponder so it will receive an key down and key up event when the user pressed a button.

The easiest way is subclassing an NSTableView and add the keyDown event


--this is something I almost literally translated from Objective-C to ASOC in text editor. So the code doesn't have to work but should indicate how to tackle the issue. 
on keyDown_(theEvent)
    set chars to theEvent's characters()
    repeat with char in chars
    if char = NSUpArrowFunctionKey then
        --do something 
    end
    end repeat
    continue keyDown_(theEvent)--continue following the parenting chain
end keyDown_

Do you need to know whether the selection change was done from the keyboard, or just that there was a selection change? If it’s the latter, you can use the delegate method, tableViewSelectionDidChange:. It’s triggered regardless of how the selection was changed.

That sounds like exactly what I need… just to know if a change was made. Don’t care how the change was made. How exactly do I implement that?

I put this in my AppDelegate.applescript but nothing happens. Do I need to wire that up or format it differently?

on tableViewSelectionDidChange_(aNotification)
	log "TABLE CHANGE"
end

Hello,

Did you set the table view delegate to your script? If not, your application won’t receive notifications.

Regards,

Got an answer here:

http://macscripter.net/viewtopic.php?id=34754

Put this in my script:


set theNotificationCenter to current application's NSNotificationCenter's defaultCenter()
tell theNotificationCenter to addObserver_selector_name_object_(me, "tableViewSelectionChanged:", "NSTableViewSelectionDidChangeNotification", missing value)

and:

on tableViewSelectionChanged_(aNotification)
	log "TABLE CHANGE"
end
	

It’s much more convenient to set this directly in IB. No code to add.

NOTE: Convenient in the coder’s perspective.

I would love to know how!

Sadly I had to get rid of the calls to the notification center I mention above as they were firing off multiple times when table rows were created and at other random times.

I simply connected my table to a subroutine through the interface and that fixed it. It doesn’t register clicks up and down on the keyboard which was my original intention of this but I’ve given up on that.

Make your script the delegate of the table view. Control-click on the table view, then drag from delegate to your script.