diff options
author | Teddy Wing | 2018-09-05 23:10:18 +0200 |
---|---|---|
committer | Teddy Wing | 2018-09-05 23:10:18 +0200 |
commit | b44d11312c8dd908a8e6d3c83e8f9773a893e15c (patch) | |
tree | f0888902dfeeacbb161e86b80dcdc9d4e99c5e15 | |
parent | e9da806f26bd6a9b11aa461a3093346967ccaf52 (diff) | |
download | Copy-Mailto-b44d11312c8dd908a8e6d3c83e8f9773a893e15c.tar.bz2 |
Make "Quit" button work on "q" or "Escape" key press
Previously I only had a key equivalent on the "Escape" key because you
can only set a single key equivalent. But I wanted the "Quit" button to
work on both "Escape" and "q" presses.
I tried to do this by overriding `performKeyEquivalent:`:
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
{
if ([[theEvent characters] isEqualToString:@"q"] ||
[theEvent keyCode] == kVK_Escape) {
NSLog(@"%@", theEvent);
return YES;
}
return [super performKeyEquivalent:theEvent];
}
The method was called and it logged correctly, but the `IBAction`
wouldn't run. Maybe this doesn't work for what I want it to? Can't
figure out what the problem is.
Didn't want to deal any more so just overrode `keyDown:` instead and got
rid of the `IBAction`, just terminating the application in code.
-rw-r--r-- | CopyMailto.xcodeproj/project.pbxproj | 6 | ||||
-rw-r--r-- | CopyMailto/Base.lproj/MainMenu.xib | 8 | ||||
-rw-r--r-- | CopyMailto/QuitButton.h | 16 | ||||
-rw-r--r-- | CopyMailto/QuitButton.m | 22 |
4 files changed, 45 insertions, 7 deletions
diff --git a/CopyMailto.xcodeproj/project.pbxproj b/CopyMailto.xcodeproj/project.pbxproj index 26d3983..1861805 100644 --- a/CopyMailto.xcodeproj/project.pbxproj +++ b/CopyMailto.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ D1F1710121402BB200BD08A0 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D1F170FF21402BB200BD08A0 /* MainMenu.xib */; }; D1F1710421402BB300BD08A0 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D1F1710321402BB300BD08A0 /* main.m */; }; D1F1710D21403F7B00BD08A0 /* DefaultURLHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = D1F1710C21403F7B00BD08A0 /* DefaultURLHandler.m */; }; + D1F17110214071D600BD08A0 /* QuitButton.m in Sources */ = {isa = PBXBuildFile; fileRef = D1F1710F214071D600BD08A0 /* QuitButton.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -25,6 +26,8 @@ D1F1710521402BB400BD08A0 /* CopyMailto.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CopyMailto.entitlements; sourceTree = "<group>"; }; D1F1710B21403F7B00BD08A0 /* DefaultURLHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DefaultURLHandler.h; sourceTree = "<group>"; }; D1F1710C21403F7B00BD08A0 /* DefaultURLHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DefaultURLHandler.m; sourceTree = "<group>"; }; + D1F1710E214071D600BD08A0 /* QuitButton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QuitButton.h; sourceTree = "<group>"; }; + D1F1710F214071D600BD08A0 /* QuitButton.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QuitButton.m; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -59,6 +62,8 @@ children = ( D1F170FA21402BB000BD08A0 /* AppDelegate.h */, D1F170FB21402BB100BD08A0 /* AppDelegate.m */, + D1F1710E214071D600BD08A0 /* QuitButton.h */, + D1F1710F214071D600BD08A0 /* QuitButton.m */, D1F170FD21402BB100BD08A0 /* Assets.xcassets */, D1F170FF21402BB200BD08A0 /* MainMenu.xib */, D1F1710221402BB300BD08A0 /* Info.plist */, @@ -147,6 +152,7 @@ files = ( D1F1710421402BB300BD08A0 /* main.m in Sources */, D1F1710D21403F7B00BD08A0 /* DefaultURLHandler.m in Sources */, + D1F17110214071D600BD08A0 /* QuitButton.m in Sources */, D1F170FC21402BB100BD08A0 /* AppDelegate.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/CopyMailto/Base.lproj/MainMenu.xib b/CopyMailto/Base.lproj/MainMenu.xib index 9fd9f26..8b9ca42 100644 --- a/CopyMailto/Base.lproj/MainMenu.xib +++ b/CopyMailto/Base.lproj/MainMenu.xib @@ -107,19 +107,13 @@ DQ <action selector="copyEmailToClipboard:" target="Voe-Tx-rLC" id="uX1-7A-IwJ"/> </connections> </button> - <button verticalHuggingPriority="750" misplaced="YES" id="dF9-KQ-ymU"> + <button verticalHuggingPriority="750" misplaced="YES" id="dF9-KQ-ymU" customClass="QuitButton"> <rect key="frame" x="328" y="13" width="66" height="32"/> <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/> <buttonCell key="cell" type="push" title="Quit" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="aJz-qw-lxS"> <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/> <font key="font" metaFont="system"/> - <string key="keyEquivalent" base64-UTF8="YES"> -Gw -</string> </buttonCell> - <connections> - <action selector="terminate:" target="-1" id="M3t-iK-CjQ"/> - </connections> </button> </subviews> </view> diff --git a/CopyMailto/QuitButton.h b/CopyMailto/QuitButton.h new file mode 100644 index 0000000..73c2e9d --- /dev/null +++ b/CopyMailto/QuitButton.h @@ -0,0 +1,16 @@ +// +// QuitButton.h +// CopyMailto +// +// Created by tw on 9/5/18. +// Copyright © 2018 tw. All rights reserved. +// + +#import <Cocoa/Cocoa.h> +#import <Carbon/Carbon.h> + +@interface QuitButton : NSButton + +- (void)keyDown:(NSEvent *)theEvent; + +@end diff --git a/CopyMailto/QuitButton.m b/CopyMailto/QuitButton.m new file mode 100644 index 0000000..8b8bb52 --- /dev/null +++ b/CopyMailto/QuitButton.m @@ -0,0 +1,22 @@ +// +// QuitButton.m +// CopyMailto +// +// Created by tw on 9/5/18. +// Copyright © 2018 tw. All rights reserved. +// + +#import "QuitButton.h" + +@implementation QuitButton + +// Quit the application on "q" or "Escape" +- (void)keyDown:(NSEvent *)theEvent +{ + if ([[theEvent characters] isEqualToString:@"q"] || + [theEvent keyCode] == kVK_Escape) { + [NSApp terminate:self]; + } +} + +@end |