On clicked in text field ?

Hi !

Is it possible to detect if the user clicked inside the text field? I tried on begin editing but it only detects when the text changes at the first time.

Thanks

I thought of an indirect way to do this.

Suppose I have a window with 2 text fields. I want to know when I clicked in the 1st text field. the 2nd text field doesn’t have to be a text field, it can be any object that can become first responder At application launch I make sure the first text field is not the first responder of the window. Then in an “on idle” handler I check which object is currently first responder, and if the first text field becomes first responder then I know I clicked in it. I use a dummy variable to make sure I only perform some action the first time the text field becomes first responder. Here’s the code for this…

Note: when a text field is first responder the first responder object is actually something called a “field editor” instead of the text field. So in order to check for the text field we have to get the “delegate” of the field editor. That’s what this line of code is for…
set firstResponderObject to call method “delegate” of (call method “firstResponder” of window 1)

global clickedInTextField1

on launched theObject -- make sure the text field of interest is not first responder
	set first responder of window 1 to text field "tf2" of window 1
	set clickedInTextField1 to false
end launched

on idle theObject
	set firstResponderObject to call method "delegate" of (call method "firstResponder" of window 1)
	if clickedInTextField1 is false then
		if firstResponderObject is text field "tf1" of window 1 then
			set clickedInTextField1 to true
			-- perform your clicked code here
			log "clicked in text field 1"
		end if
	else
		if firstResponderObject is not text field "tf1" of window 1 then
			set clickedInTextField1 to false
		end if
	end if
end idle

Thanks ! it works !

But if there are any other solutions (preferably without on idle) the I’d be glad to see those too !

Edit:
I actually needed it to clear the contents of a text field when clicked in it. This can be done by setting a place holder in IB. :slight_smile: