[Openmcl-devel] Making a text input first responder in a modal dialog

R. Matthew Emerson rme at acm.org
Wed Aug 29 18:22:49 PDT 2018



> On Aug 29, 2018, at 5:48 PM, Ron Garret <ron at flownet.com> wrote:
> 
> I’m trying to build a password-input dialog.  This works:
> 
> (defun passwd-dialog (&optional (prompt "Enter password:"))
>  (let ((alert (make-instance 'ns:ns-alert))
>        (input (make-instance 'ns:ns-secure-text-field)))
>    (#/setMessageText: alert (ccl::%make-nsstring prompt))
>    (#/addButtonWithTitle: alert (ccl::%make-nsstring "OK"))
>    (#/addButtonWithTitle: alert (ccl::%make-nsstring "Cancel"))
>    (#/initWithFrame: input (ns:make-ns-rect 0 0 200 24))
>    (#/setAccessoryView: alert input)
>    ;;; (#/makeFirstResponder: (#/window alert) input)
>    (#/autorelease input)
>    (if (eql (#/runModal alert) #$NSAlertFirstButtonReturn)
>      (ccl::lisp-string-from-nsstring (#/stringValue input)))))
> 
> but it requires the user to click on the dialog to make the input field active.  The commented-out line is my attempt to fix this problem, but it doesn’t work.  Not only does it fail to make the input field active, it actually breaks the whole dialog so that it always returns an empty string.

It might be better not to use an NSAlert here, and to use a regular NSWindow (or maybe NSPanel) instead.

The behavior you describe (have to click on dialog to make keyboard input work) reminds me of the setBecomesKeyOnlyIfNeeded: method of NSPanel.

I would not be surprised if NSAlert creates and uses an NSPanel internally, but I don't think you can access that.







More information about the Openmcl-devel mailing list