I have a button that I have set the return key to activate. I set it through the attribute menu for the button. I have a Multi-line textField that when return is clicked it just highlights the text in the textField. How do I disable the return key in the textfield?
on writeBtn_(sender)
my getTexFromTextField_(writeBtn_)
end writeBtn_
This is set to the return key as the default button in the attributes menu and it is darkened when I run the script.
on getTexFromTextField_(sender)
-- get the text from the textField and assign it to a variable
set textFieldValue to textField's stringValue()
-- write the data to the file and close it
my writeToLocalFile_(textFieldValue)
end getTexFromTextField_
I have no problem writing this off to a file.
Thank you,
Rich
What you describe is the “correct” way for a text field to behave, so changing it is going to be difficult. You could try making a script a delegate of it, and implementing controlTextDidEndEditing_. Another option would be to use a text view instead; it might have more useful delegate methods.
But you’re fighting the frameworks – maybe you should have a rethink about what you’re trying to achieve.
I had a feeling I was fighting what it was suppose to do. They do not want just a single line textField because they want to view everything they enter in one window as a whole. They want to be able to scroll if need be. Would i be able to do this with a textView instead? I was using a textView but of course every time the return was hit it would just go to the next line as it should. I would most likely be doing the same thing with a textView ‘fighting what it is suppose to do’
I changed my item back to a textView. Can any one give me a hint or point me in the right direction on how I may be able to do this with ApplescriptOBJ. I am going through the Objective-C header files trying to figure it out with the methods supplied and not having much luck. I am looking at NSTextInputClient and Binding Keystrokes
“ doCommandBySelector: . I am not sure how to construct it in Obj-C. I know AppleScript not ApplescriptOBJ. Yes I am a beginner with Xcode.
My objective is to over ride the return key in the textView so my default button will accept it
What you want seems impossible to implement. If you have a text view/multiple lines text field, HOW would the application guess “this is a line feed for the text” or “this is the OK button” if it just gets “Return”?
On other applications, when a multiple-line text is entered in a dialog, the “OK” button has no (or an other) key equivalent.
I suggest you to set ⌘⎠(command + return) for the button equivalent, so you can use “normal” returns into the text.
That works just fine. I will need to explain to them and see if they will accept it. There really doesn’t seem to be any way around it. I agree with you that both are just a return. I guess they expect not to use returns in the textView because it wraps but I am sure at some point some will.
I was really just wondering if there was a way around it because I will be questioned about it.
Is it possible to displayed the command return at the end of the name of the button or I guess I could put it in a Tool Tip.
You can do what you want with a text view, you just need to make a subclass of NSTextView and override keyDown.
Add another applescript class to your project and set your textview to that class. Here is the script for that class:
script CutomView
property parent : class "NSTextView"
on keyDown_(theEvent)
if theEvent's keyCode as integer is 36 and theEvent's modifierFlags as integer is 256 then
performSelector_("doStuff")
else
continue keyDown_(theEvent)
end if
end
on doStuff()
log "doing stuff"
end
end script
Pressing return will run the method “doStuff”, and the user can still get a standard return by pushing control-return or alt-return.
Overriding keyDown is probably a bit of a sledgehammer in that it will be called for every character, although if there’s not much text it shouldn’t affect performance much. But overriding doCommandBySelector_ will narrow things down considerably.
Choose and NSTextView and set theDelegate to to object with controlTextDidChange_(aNotification) handler in it. You can also use keydown command for a smoother workflow. But controlTextDidChange_ also handle drops of text from another application.
EDIT: Shane is probably right for ASOC that keydown is a bit of a sledgehammer. Though for Objective-C it would be a better choice .
Thanks for the script redlman,
the text they are entering may not be that long. This would most likely work well. Right now I have used fiz’s suggestion and placed the actual command in the name of the button. I will see what they want to do. I am also going to take a look into DJ Bazzie Wazzie suggestion "Choose and NSTextView and set theDelegate to to object with controlTextDidChange_(aNotification) handler in it. "