KeyUp events on TextField

Hi Everyone,

I’m just trying to get my head around the new way of doing things in Xcode 4 and I can’t seem to figure out how I can traps key up events on a text field.

Previously I’d do it in the following way but that no longer works…

on keyboard up theObject event theEvent
	 set KC to (get key code of theEvent)
	 --Do something with KC here...
end keyboard up

Can someone please point me in the right direction…

Cheers

Chris

That would be something like:

on keyUp_(theEvent)
set keyPressed to character 1 of |characters|() of theEvent
...

But what are trying to do exactly? There might be a better way.

Thanks Shane, that helps a lot.

You’re right there is a better way. Previously I would check key codes to determine if an enter key was pressed when in the text field. I found last night that I can set the field to only send the event when the enter key is pressed so I no longer need to check key codes at all. I was just stuck in the way I previously did things.

I purchased your book the other day and I’m slowly working my way through it. I was almost ready to give up on XCode and AppleScript until I found your book.

My next task is to figure out how to add a single line of text to the end of the text already in a Text Area and also how to scroll the text area to the bottom so that the latest text is always displayed.

Cheers

Chris

I am assuming you are using a Text Area, not a Text Field. To get or set the value of a Text Area, use “string()” and “setString_()”.

L :slight_smile:

What was edited? Nothing.

Thanks L :slight_smile:

I was trying the examples from Shane’s book, using the TextStorage but I was running into all sorts of issues. Using the setString_() with a Linefeed does exactly what I need.

I’ll try to figure out my next problem myself before taking the easy way out and asking for help… :slight_smile:

Cheers

The TextView is in a Scroll View. Look up the NSScrollView to see how to scroll that.

Hi Everyone,

Ok I sorry to say that I’m still stuck on this. What I want to do is have a TextView that the user can type in, then when they press the “return key” I want to run some code to grab the last line of text that they have typed then do some processing on it. I want to do everything with just the TextView, no other fields or buttons. But my issue is I just can’t get the keyboard events to fire… I’ve tested with a button and everything else is working.

I’m really stumped now and would really appreciate someone explaining how to get the keyboard events to work…

Regards

Chris

What you’re trying to do it not standard UI, and one of the things about Cocoa is that it generally makes standard things relatively easy, but others can be quite complicated. That said, the best approach will probably be to subclass the txt view, and implement -doCommandBySelector: and trapping for insertNewLine:/

I think you can do this by just overriding keyDown: in your script. I don’t know how much experience you have in writing subclasses, so please excuse me if I’m over explaining. The script needs to be a subclass of NSTextView – which just means that the parent class will be NSTextView instead of NSObject. Then in IB you need to set the class of your text view to your script’s class — it should be one of the choices in the pull down menu next to the class of the text view in the identity pane of the inspector. This code example will add a return to the text when the user presses return, insert some new text on the next line, and then log the whole contents of the text view. You could do whatever processing you want instead.

script TextViewTestAppDelegate
	property parent : class "NSTextView"
	
	on keyDown_(theEvent)
		if theEvent's keyCode as integer is 36 then -- keyCode 36 is "return"
			continue keyDown_(theEvent)
			insertText_("Here is some new text.")
			log |string|()
		else
			continue keyDown_(theEvent)
		end if
	end keyDown_
	
end script

Ric

Thanks Ric,

Exactly what I needed and appears to work fine.

Cheers

Chris

The advantage of using doCommandBySelector: rather than keyDown: is that you don’t have to worry about keyCodes – what happens if the user hits the enter key, or shift-return? Instead, you trap for the action of insertNewLine:. It also means not passing every keystroke through your handler.

It took me a while to figure out how to use doCommandBySelector: The documentation isn’t all that clear on how it gets invoked or how to use it. Anyway, the code below shows how to use it or the related method textView:doCommandBySelector:. As written below, the script has to be a subclass of NSTextView, but if you use the commented out method instead, you don’t need to subclass the text view (but you do need to make your script the delegate of the text view).

script TextViewTestAppDelegate
	property parent : class "NSTextView"
	
	on doCommandBySelector_(cmd)
		if cmd is "insertNewLine:" then
			log cmd
			--do whatever you want here
		end if
	end doCommandBySelector_
	
	(*	on textView_doCommandBySelector_(tv, command)
		if command is "insertNewLine:" then log "return pushed"
		return 0
	end textView_doCommandBySelector_*)
	
end script

Both of these methods get invoked only when you press certain keys that cause actions to happen rather than just adding a letter (like return, escape, delete and the arrow keys).

Ric

That’s an understatement…