filter for dialog window

I am trying to filter out the 'return' key and 'esc' key in a dialog window. I know how to pass a UniversalProcPtr to ModalDialog( gMyModalProc, &itemHit); and filter out the keys. When I use DialogSelect(&gMainEventRecord, &hitWindow, &itemHit); and try to filter out the keys the keys get passed anyway.  I have a dialog window with three edit text fields, I would like to limit two edit fields to 4 chars each and the other to 31 chars. How can I get around this?

     if(DialogSelect(&gMainEventRecord, &hitWindow, &itemHit))
                    HandleFileInfoDialog(itemHit, &gMainEventRecord);

Boolean HandleFileInfoDialog (short itemHit, EventRecord *theEvent)

      if(theEvent->what  == keyDown)
            switch((theEvent->message) & charCodeMask)
                  case 0x0d:
                  case 0x03:
                  case 0x1b:
                        itemHit = 1;
                  case 1:

   case  2:

Who is Participating?
boonstraConnect With a Mentor Commented:
The problem is that DialogSelect is processing your keyDown events.  It is probably also processing 0x0D and 0x03 as keyboard equivalents for the default button in your dialog.

Unfortunately the filterProcs you refer to in your question apploy only to modal dialogs.  Yours is probably nonModal.  You need to intercept the keyDown events before they get to DialogSelect.  Something like:

    if (IsDialogEvent(&ev)) {
      if (FrontWindow()==gDialog) {
        notHandled = true;
        dlgP = (DialogPeek)gDialog;
        if (ev.what==keyDown) {
          ch = ev.message & charCodeMask;
          if ((ch==0x0D) || (ch==0x03)) {
            notHandled = false;
          if (dlgP->editField == kItemOfInterest1-1) {
            /* count and filter characters for item kItemOfInterest1 */
            if (++gCharCount1>4) notHandled = false;
          } else if (dlgP->editField == kItemOfInterest2-1) {
            /* count and filter characters for item kItemOfInterest2 */
            if (++gCharCount2>4) notHandled = false;
        if (notHandled && DialogSelect(&ev, &wind, &itemHit)) {
          /* need to update gCharCounts here */
      if (gDialog==wind) switch (itemHit) {
        case ok:
        case cancel:
dwp090598Author Commented:
yes, my dialog is non modal. I was trying to do the filtering after calling DialogSelect().

Thanks Again...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.