13

I've got working code in objective c class which is like follows :

closeButton.keyEquivalent = @"\e";

This does not work in swift, as it says "Invalid escape sequence in literal." I've tried to use the following code:

closeButton.keyEquivalent = "\u{53}"

But also no luck. Any ideas ?

2
  • The correct key code for escape would be u{1b} but that does not work either. Probably escape is caught in NSResponder somewhere else. E.g. if focus is on a dropdown it opens the menu.
    – qwerty_so
    Feb 16, 2015 at 12:40
  • @NickCatib yeah double checked now, does not work with \\e
    – Krzysztof
    Feb 16, 2015 at 12:41

2 Answers 2

25

You need to assign

closeButton.keyEquivalent = "\u{1b}"

Just tried in a test app.

Edit: According to @Lucasware's comment the ObjC assignment is

[NSString stringWithFormat:@"%C", 0x1b]
2
  • How do I do this in objective c?
    – YeaTheMans
    Aug 17, 2017 at 2:13
  • 1
    Thanks but I figured it out = [NSString stringWithFormat:@"%C", 0x1b];
    – YeaTheMans
    Aug 19, 2017 at 23:01
0

Thanks this code working fine to close window when click on esc key

standardWindowButton(.closeButton)?.keyEquivalent = "\u{1b}"
1
  • Please explain the additional insight which your post contributes to existing answers (especially stackoverflow.com/a/28541684/7733418 ). The core of the implementation seems to be very similar. Please make the relevant difference more obvious and point out its advantages. See How to Answer and edit accordingly. Otherwise your posts reads too much like "thanks", which is not considered an answer on StackOverflow and risks being flagged and deleted as "not an answer".
    – Yunnosch
    Jan 4 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.