NSAlert: Make second button both the default and cancel button












1















Apple's original HIGs (now disappeared from the web site, sadly) stated that:



The rightmost button in the dialog, the action button, is the button that confirms the alert message text. The action button is usually, but not always, the default button



In my case, I have some destructive operations (such as erasing a disk) that need "safe" confirmation dialogs, like this:



"Safe" confirmation dialog



The worst option would be to make a dialog where the rightmost button would become the "do not erase" button, and the one left of it, which is usually, the Cancel button, would become the "erase" button, because that would lead easily to disaster (happened to me with a Microsoft-made dialog once), because people are trained to click the second button whenever they want to cancel an operation.



So, what I need is that the left (cancel) button becomes both the default button, and also reacts to the Return, Esc and cmd-period keys.



To make it default and also react to the Return key, I simply have to set the first button's keyEquivalent to an empty string, and the second button's to "r".



But how to I also make the alert cancel when Esc or cmd-. are typed?










share|improve this question

























  • It's certainly against HIG that one button responds to both ↩ and ⎋. You can't assign two different key equivalents to a button anyway. You might design a custom view and implement your own logic for handling mouse and keyboard events

    – vadian
    Nov 17 '18 at 17:05













  • Is your Cancel button actually titled "Cancel"?

    – Ken Thomases
    Nov 17 '18 at 17:35











  • @vadian I disagree. Can you show a HIG article for macOS that supports your claim?

    – Thomas Tempelmann
    Nov 17 '18 at 19:24













  • @KenThomases Yes, I forgot to mention that it's not called "Cancel", but rather "Don't Erase" or something similar. I realize that naming it Cancel might be a simply solution, perhaps.

    – Thomas Tempelmann
    Nov 17 '18 at 19:25











  • Counter question: Can you show me an example (an app) where a default button responds to both return and ESC?

    – vadian
    Nov 17 '18 at 19:41


















1















Apple's original HIGs (now disappeared from the web site, sadly) stated that:



The rightmost button in the dialog, the action button, is the button that confirms the alert message text. The action button is usually, but not always, the default button



In my case, I have some destructive operations (such as erasing a disk) that need "safe" confirmation dialogs, like this:



"Safe" confirmation dialog



The worst option would be to make a dialog where the rightmost button would become the "do not erase" button, and the one left of it, which is usually, the Cancel button, would become the "erase" button, because that would lead easily to disaster (happened to me with a Microsoft-made dialog once), because people are trained to click the second button whenever they want to cancel an operation.



So, what I need is that the left (cancel) button becomes both the default button, and also reacts to the Return, Esc and cmd-period keys.



To make it default and also react to the Return key, I simply have to set the first button's keyEquivalent to an empty string, and the second button's to "r".



But how to I also make the alert cancel when Esc or cmd-. are typed?










share|improve this question

























  • It's certainly against HIG that one button responds to both ↩ and ⎋. You can't assign two different key equivalents to a button anyway. You might design a custom view and implement your own logic for handling mouse and keyboard events

    – vadian
    Nov 17 '18 at 17:05













  • Is your Cancel button actually titled "Cancel"?

    – Ken Thomases
    Nov 17 '18 at 17:35











  • @vadian I disagree. Can you show a HIG article for macOS that supports your claim?

    – Thomas Tempelmann
    Nov 17 '18 at 19:24













  • @KenThomases Yes, I forgot to mention that it's not called "Cancel", but rather "Don't Erase" or something similar. I realize that naming it Cancel might be a simply solution, perhaps.

    – Thomas Tempelmann
    Nov 17 '18 at 19:25











  • Counter question: Can you show me an example (an app) where a default button responds to both return and ESC?

    – vadian
    Nov 17 '18 at 19:41
















1












1








1


1






Apple's original HIGs (now disappeared from the web site, sadly) stated that:



The rightmost button in the dialog, the action button, is the button that confirms the alert message text. The action button is usually, but not always, the default button



In my case, I have some destructive operations (such as erasing a disk) that need "safe" confirmation dialogs, like this:



"Safe" confirmation dialog



The worst option would be to make a dialog where the rightmost button would become the "do not erase" button, and the one left of it, which is usually, the Cancel button, would become the "erase" button, because that would lead easily to disaster (happened to me with a Microsoft-made dialog once), because people are trained to click the second button whenever they want to cancel an operation.



So, what I need is that the left (cancel) button becomes both the default button, and also reacts to the Return, Esc and cmd-period keys.



To make it default and also react to the Return key, I simply have to set the first button's keyEquivalent to an empty string, and the second button's to "r".



But how to I also make the alert cancel when Esc or cmd-. are typed?










share|improve this question
















Apple's original HIGs (now disappeared from the web site, sadly) stated that:



The rightmost button in the dialog, the action button, is the button that confirms the alert message text. The action button is usually, but not always, the default button



In my case, I have some destructive operations (such as erasing a disk) that need "safe" confirmation dialogs, like this:



"Safe" confirmation dialog



The worst option would be to make a dialog where the rightmost button would become the "do not erase" button, and the one left of it, which is usually, the Cancel button, would become the "erase" button, because that would lead easily to disaster (happened to me with a Microsoft-made dialog once), because people are trained to click the second button whenever they want to cancel an operation.



So, what I need is that the left (cancel) button becomes both the default button, and also reacts to the Return, Esc and cmd-period keys.



To make it default and also react to the Return key, I simply have to set the first button's keyEquivalent to an empty string, and the second button's to "r".



But how to I also make the alert cancel when Esc or cmd-. are typed?







macos cocoa nsalert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 21:21







Thomas Tempelmann

















asked Nov 17 '18 at 16:37









Thomas TempelmannThomas Tempelmann

5,75625092




5,75625092













  • It's certainly against HIG that one button responds to both ↩ and ⎋. You can't assign two different key equivalents to a button anyway. You might design a custom view and implement your own logic for handling mouse and keyboard events

    – vadian
    Nov 17 '18 at 17:05













  • Is your Cancel button actually titled "Cancel"?

    – Ken Thomases
    Nov 17 '18 at 17:35











  • @vadian I disagree. Can you show a HIG article for macOS that supports your claim?

    – Thomas Tempelmann
    Nov 17 '18 at 19:24













  • @KenThomases Yes, I forgot to mention that it's not called "Cancel", but rather "Don't Erase" or something similar. I realize that naming it Cancel might be a simply solution, perhaps.

    – Thomas Tempelmann
    Nov 17 '18 at 19:25











  • Counter question: Can you show me an example (an app) where a default button responds to both return and ESC?

    – vadian
    Nov 17 '18 at 19:41





















  • It's certainly against HIG that one button responds to both ↩ and ⎋. You can't assign two different key equivalents to a button anyway. You might design a custom view and implement your own logic for handling mouse and keyboard events

    – vadian
    Nov 17 '18 at 17:05













  • Is your Cancel button actually titled "Cancel"?

    – Ken Thomases
    Nov 17 '18 at 17:35











  • @vadian I disagree. Can you show a HIG article for macOS that supports your claim?

    – Thomas Tempelmann
    Nov 17 '18 at 19:24













  • @KenThomases Yes, I forgot to mention that it's not called "Cancel", but rather "Don't Erase" or something similar. I realize that naming it Cancel might be a simply solution, perhaps.

    – Thomas Tempelmann
    Nov 17 '18 at 19:25











  • Counter question: Can you show me an example (an app) where a default button responds to both return and ESC?

    – vadian
    Nov 17 '18 at 19:41



















It's certainly against HIG that one button responds to both ↩ and ⎋. You can't assign two different key equivalents to a button anyway. You might design a custom view and implement your own logic for handling mouse and keyboard events

– vadian
Nov 17 '18 at 17:05







It's certainly against HIG that one button responds to both ↩ and ⎋. You can't assign two different key equivalents to a button anyway. You might design a custom view and implement your own logic for handling mouse and keyboard events

– vadian
Nov 17 '18 at 17:05















Is your Cancel button actually titled "Cancel"?

– Ken Thomases
Nov 17 '18 at 17:35





Is your Cancel button actually titled "Cancel"?

– Ken Thomases
Nov 17 '18 at 17:35













@vadian I disagree. Can you show a HIG article for macOS that supports your claim?

– Thomas Tempelmann
Nov 17 '18 at 19:24







@vadian I disagree. Can you show a HIG article for macOS that supports your claim?

– Thomas Tempelmann
Nov 17 '18 at 19:24















@KenThomases Yes, I forgot to mention that it's not called "Cancel", but rather "Don't Erase" or something similar. I realize that naming it Cancel might be a simply solution, perhaps.

– Thomas Tempelmann
Nov 17 '18 at 19:25





@KenThomases Yes, I forgot to mention that it's not called "Cancel", but rather "Don't Erase" or something similar. I realize that naming it Cancel might be a simply solution, perhaps.

– Thomas Tempelmann
Nov 17 '18 at 19:25













Counter question: Can you show me an example (an app) where a default button responds to both return and ESC?

– vadian
Nov 17 '18 at 19:41







Counter question: Can you show me an example (an app) where a default button responds to both return and ESC?

– vadian
Nov 17 '18 at 19:41














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353246%2fnsalert-make-second-button-both-the-default-and-cancel-button%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353246%2fnsalert-make-second-button-both-the-default-and-cancel-button%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word