Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: StandardAlert handling keystrokes?



on Sun, 28 May 2006 16:55:52, Alice Hartley <email@hidden> wrote:

> Some time ago our application switched from using a home grown
> implementation of alert dialogs to using
> CreateStandardAlert plus  RunStandardAlert.
> In the process we lost a feature.
> Formerly one could select a button by typing
> the first character of it's name,
> that is if the buttons were named
> "No" "Cancel" "Yes"
> then typing the character n  would act as a click in the "No" button,
> c the "Cancel" button etc.
> Typing escape, return,  or enter worked as expected.
> With the change, typing escape, return, or enter still
> work as expected, but I am having a problem handling
> the typing of other characters. Some users have
> requested that the ability to type e.g. n to choose the
> "No" button be reinstated.
> 
> It seems as if calling RunStandardAlert with
> a filterproc created with NewModalFilterUPP should be
> helpful in doing so.
> 
> However when the filterproc is called and I do
> GetEventClass and GetEventKind of the event,
> the class is nonsense (corresponds to no known
> event class and varies) and the kind is always 0.
> I would have expected to find kEventClassKeyboard
> and kEventRawKeyDown when a character is typed.
> Never happens.
> Also when the filterproc is used, typing escape return or enter
> cease to function.
> 
> So either:
> a) I doing something wrong
> b) I shouldn't  expect this approach to work
> c) There another way to handle keystrokes in StandardAlert
> d) its not possible

Below is source code of event filter that does almost what
you say (it specifically filters out default/cancel buttons
on the grounds they are already accessible by enter/escape
keys; comment the corresponding lines in GetDialogButtonShortcut
if you don't like this). Beware: it is not unicode-savvy.
Enjoy.

Mike

static UInt8 GetDialogButtonShortcut(DialogRef dialog, DialogItemIndex item)
{
    ControlRef c;
    DialogItemType type;
    Rect r;
    Str255 title;
    
    if (item == GetDialogDefaultItem(dialog)) return 0;
    if (item == GetDialogCancelItem(dialog)) return 0;
    GetDialogItem(dialog, item, &type, (Handle *)&c, &r);
    if ((type & ~kItemDisableBit) != kButtonDialogItem) return 0;
    if (!c) return 0;
    if (r.left > 0x2000) return 0; // invisible
    if (!IsControlVisible(c)) return 0;
    GetControlTitle(c, title);
    if (!title[0] || !title[1]) return 0;
    return toupper(title[1]);
}

static pascal Boolean TheAlertEventFilter(DialogRef dialog,
    EventRecord *event, short *itemHit)
{
    ControlRef c;
    UInt32 L;
    DialogItemType type;
    Rect r;
    UInt8 ch;
    DialogItemIndex button;
    
    do {
        if (event->what != keyDown) break;
        if ((event->modifiers & cmdKey) == 0) break;
        ch = event->message & charCodeMask;
        if (!ch) break;
        ch = toupper(ch);
        
        for (button = 1; button <= 3; button++) {
            if (GetDialogButtonShortcut(dialog, button) == ch) {
                GetDialogItem(dialog, button, &type, (Handle *)&c, &r);
                HiliteControl(c, kControlButtonPart);
                QDFlushPortBuffer(GetWindowPort(GetDialogWindow(dialog)),
                    NULL);
                Delay(8, &L);
                HiliteControl(c, kControlNoPart);
                QDFlushPortBuffer(GetWindowPort(GetDialogWindow(dialog)),
                    NULL);
                *itemHit = button;
                return true;
            }
        }
    } while (false);
    
    return StdFilterProc(dialog, event, itemHit);
}

Mike


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >StandardAlert handling keystrokes? (From: Alice Hartley <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.