How can I handle ESC key in Cocoa app? - cocoa

I made an app switching to full screen mode. I want to use ESC key to escaping fullscreen mode, but binding menu item to ESC key in IB is removed at runtime. How can I keep ESC key binding to a menu item?

Preferred way to handle escape key in Cocoa is this as like #Josh Caswell said.
#pragma mark - NSResponder
- (void)cancelOperation:(id)sender
{
[self exitFullScreen];
}

One way to capture keyboard events involves subclassing:
Subclass your full-screen class (e.g.) NSView.
Add the method - (void) keyDown:(NSEvent *)theEvent to the subclass implementation.
Open up InterfaceBuilder and select the full-screen class that you previously created.
Change its class to your new subclass.
The subclass looks something like:
MySubclass.h
#interface MySubclass : NSView {
}
#end
MySubclass.m
#import <Carbon/Carbon.h>
#implementation MySubclass
- (void)keyDown:(NSEvent *)theEvent
{
switch([theEvent keyCode]) {
case kVK_Escape:
NSLog(#"ESC");
// Call the full-screen mode method
break;
default:
[super keyDown:theEvent];
}
}
#end
This doesn't bind the ESC key to the menu item, but it does give you equivalent functionality (and a bit more flexability since you can intercept all keyboard events).

Many people try to implement esc key functionality. There is cancelOperation in the responder chain to handle escape events.
//WRONG
- (void)keyDown:(NSEvent *)event
{
//unichar character = 0;
//if ([event type] == NSEventTypeKeyDown) {
// if ([[event charactersIgnoringModifiers] length] == 1) {
// character = [[event characters] characterAtIndex:0];
// }
//}
switch (character) {
//THIS IS WRONG correct is to implement interpretKeyEvents+moveRight
//case NSRightArrowFunctionKey:
// [self moveSelectedIndexRight];
// break;
//THIS IS WRONG correct is to implement interpretKeyEvents+ moveLeft
//case NSLeftArrowFunctionKey:
// [self moveSelectedIndexLeft];
// break;
//THIS IS WRONG correct is to implement interpretKeyEvents+ moveLeft
//case NSCarriageReturnCharacter:
// [self dismissWithCurrentlySelectedToken];
// break;
default:
[self interpretKeyEvents:#[event]];
[super keyDown:event]
break;
}
}
//CORRECT
- (void)keyDown:(NSEvent *)event
{
[self interpretKeyEvents:#[event]];
[super keyDown:event];
}
`/* Catch the commands interpreted by interpretKeyEvents:. Normally, if we don't implement (or any other view in the hierarchy implements) the selector, the system beeps. Menu navigation generally doesn't beep, so stop doCommandBySelector: from calling up t`he hierarchy just to stop the beep.
*/
- (void)doCommandBySelector:(SEL)selector {
if ( selector == #selector(moveRight:)
|| selector == #selector(moveLeft:)
|| selector == #selector(cancelOperation:)
|| selector == #selector(insertNewline:) )
{
[super doCommandBySelector:selector];
}
// do nothing, let the menu handle it (see call to super in -keyDown:)
// But don't call super to prevent the system beep
}
- (void)cancelOperation:(id)sender
{
//do your escape stuff
}
- (void)insertNewline:(id)sender
{
//do your enter stuff
}
- (void)moveRight:(nullable id)sender
{
[self moveSelectedIndexRight];
}
- (void)moveLeft:(nullable id)sender
{
[self moveSelectedIndexLeft];
}

I needed to dodge WKWebView crashes when ESC is pressed(?) so I sub-class it, and added:
import Carbon.HIToolbox
override func keyDown(with event: NSEvent) {
if event.keyCode == UInt16(kVK_Escape) {
// We crash otherwise, so just close window
self.window?.performClose(event)
}
else
{
// still here?
super.keyDown(with: event)
}
}

Related

Set selectedRange when NSTextField becomes focused

I'm overriding becomeFirstResponder to know when my NSTextField is focused. Once focused, I'm trying to move the cursor to the end. The following snippet does not work:
#interface MyTextField : NSTextField
#end
#implementation MyTextField
- (BOOL)becomeFirstResponder
{
if ([super becomeFirstResponder]) {
self.currentEditor.selectedRange = NSMakeRange(self.stringValue.length, 0);
return YES;
}
return NO;
}
#end
By overriding textView:didChangeSelection:, I found that the selection is made, but it's then overwritten by some internal code that runs in response to the NSEventTypeLeftMouseDown event.
The logs look like this:
location=0, length=25
location=25, length=0 // The desired selection.
location=0, length=0
location=5, length=0 // Where the user clicked.
Override the mouseDown: method in your NSTextField subclass.
Then, set selectedRange after calling super.
- (void)mouseDown:(NSEvent *)event
{
[super mouseDown:event];
self.currentEditor.selectedRange = NSMakeRange(self.stringValue.length, 0);
}
NSTextField only has its mouseDown: method called when its "field editor" is not yet focused, so the user can still change the selection after the NSTextField gains focus.
This isn't a perfect solution, because the user may have focused the NSTextField indirectly (eg: with the Tab key). You can always set selectedRange in both mouseDown: and becomeFirstResponder though.
Use performSelector:withObject:afterDelay: from inside becomeFirstResponder to ensure the selectedRange is set after the NSEventTypeLeftMouseDown event is handled.
- (BOOL)becomeFirstResponder
{
if ([super becomeFirstResponder]) {
[self performSelector:#selector(textFieldDidFocus) withObject:nil afterDelay:0.0];
return YES;
}
return NO;
}
- (void)textFieldDidFocus
{
self.currentEditor.selectedRange = NSMakeRange(self.stringValue.length, 0);
}

How to intercept toggling fullscreen mode

I'm creating Mac OS plugin(bundle) for Unity3D. How can I intercept entering cmd-f combination (toggling full screen mode)? I can't create my own window, I can only use default (mainWindow). I've tried to use NSNotificationCenter, but I need to stop event, I don't need just a notification. I've tried to create NSResponder and add it to capture input events, but something don't work. Any ideas how to do it?
NSWindow* window = [[NSApplication sharedApplication] mainWindow];
NSView* view = [window contentView];
NSResponder* oldresp = [view nextResponder];
MyResponder* myres = [MyResponder alloc];
[myres retain];
[view setNextResponder:myres];
and
#interface MyResponder : NSResponder
{
}
- (void)keyDown:(NSEvent *)theEvent;
#end
#implementation MyResponder
- (void)keyDown:(NSEvent *)theEvent
{
NSLog(#"%#",#"!KeyDown Event");
NSString *theArrow = [theEvent charactersIgnoringModifiers];
unichar keyChar = 0;
if ( [theArrow length] == 1 )
{
keyChar = [theArrow characterAtIndex:0];
if ( keyChar == NSModeSwitchFunctionKey )
{
NSLog(#"%#",#"!!!___!!! GOT NSModeSwitchFunctionKey !!!");
return;
}
NSLog(#"%# %d",#"! Key:",keyChar);
}
[super keyDown:theEvent];
}
#end
One solution is to check constantly if the user has switched to fullscreen mode, and if he has, toggle fullscreen off from your program. This may cause some brief lag or graphics sketchiness momentarily, but it should work.
function Update ()
{
if (Screen.fullScreen) {
Screen.fullScreen = false;
}
}
Im not sure of a way to intercept the key press and ignore the command before the program switches though.
See this related post on unity answers about dealing with this on windows:
http://answers.unity3d.com/questions/544183/block-or-override-alt-enter-fullscreen.html

Keyboard Shortcuts for NSSegmentedControl Button in COCOA Mac Application

In my COCOA Mac Application, there is one NSSegmentedControl Button for Queue ON/OFF functionality. Now I need to provide keyboard shortcut for the same. For example, for Queue ON if already it was Off and vice versa.
Is anyone did this type of functionality?
Thanks and Regards,
Barun
Since I didn't find a way to add a keyboard binding to the NSSegmentedControl via interface builder, I did id programmatically. I created a custom Window class named PMWindow:
#implementation PMWindow
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)keyDown:(NSEvent *)theEvent {
if([self.viewController.lastNextControl isEnabled]) {
if([theEvent keyCode] == 123) {
[self.viewController last];
} else if([theEvent keyCode] == 124) {
[self.viewController next];
}
}
}
#end
I added my view controller named PMViewController via the interface builder to the Window class PMWindow. In this example I'm reacting to the key strokes < left arrow > (123) and < right arrow > (124).
For completeness of this example I added here the implementation of the method which gets called when somebody hits a cell of my NSSegmentedControl element:
- (IBAction)lastOrNext:(id)sender {
switch ([sender selectedSegment]) {
case 0:
[self last];
break;
case 1:
[self next];
break;
}
}

Subclassing NSButton, need to make it look like a regular button

I'm subclassing NSButton because I need to repeat a selector while the mouse is being held down.
I'm doing that like this:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
[self setBezelStyle:NSBezelBorder];
PotRightIsDown = NO;
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
- (void)mouseDown:(NSEvent *)theEvent;
{
NSLog(#"pot right mouse down");
PotRightIsDown = YES;
holdDownTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(sendCommand) userInfo:nil repeats:YES];
}
- (void)mouseUp:(NSEvent *)theEvent;
{
NSLog(#"pot right mouse up");
PotRightIsDown = NO;
}
-(void)sendCommand
{
if (PotRightIsDown)
{
NSLog(#"run the stuff here");
}
else
{
[holdDownTimer invalidate];
}
}
Works like a champ, sends the command every 100ms.
In the window in IB, I've dragged a Bevel Button onto the window and set it's class to this subclass. When I ran the application, the button is invisible however it works. I'm guessing this is because I have an empty drawRect function in the subclass.
How can I make this subclassed button look like a Bevel button?
Thank you,
Stateful
If you aren't adding any functionality to a particular subclass method then you can simply avoid implementing it altogether, which will allow the superclass to provide the default behaviour.
Alternatively (as pointed out my #Carl Norum) you can explicitly do that using:
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
}
But it's a bit pointless.

Detect space bar event on Mac?

I've searched the entire internet for something on this with no luck. I'm trying to detect if the user hits the space bar on a Mac app.
-(void)keyDown:(NSEvent*)theEvent; works great when the user presses on a character key, but not enter or space.
-(BOOL)performKeyEquivalent:(NSEvent *)theEvent; is called when the user hits the enter key or the arrow keys, but not the space bar.
Any ideas?
Was able to get it working by subclassing NSWindow and implementing this method:
- (void)sendEvent:(NSEvent *)theEvent
{
NSString* keysPressed = [theEvent characters];
if ( [keysPressed isEqualToString:#" "] )
{
if(theEvent.type==NSKeyDown)
NSLog(#"spaceDown");
if(theEvent.type==NSKeyUp)
NSLog(#"spaceUp");
}
}
You can use this other loop with the key number 32 according to the space bar constant.
if ([theArrow length] == 1)
{
keyChar = [theArrow characterAtIndex:0];
NSLog(#"Dentro2 %hu", keyChar);
switch (keyChar)
{
case 32: // Space Bar management
break;
}
}
// Manage when any key is dropped.
if(downOrUp == FALSE)
{
NSLog(#"Tecla soltada.");
self.playerVelocity = CGPointMake(0.0, 0.0);
}
// Methods to handle key push and key drop
- (void)keyDown:(NSEvent *)event
{
[self handleKeyEvent:event keyDown:YES];
}
- (void)keyUp:(NSEvent *)event
{
[self handleKeyEvent:event keyDown:NO];
}
I stumbled upon it by myself resently. Strangely enough, keyDown: event is detected only by the the method described by moby. But keyUp: works like a charm with space bar press
After trying few solutions i come with one that din't break other things for me
#import Carbon;
typedef void (^CustomWindowSpacebarKeyCallback)(NSEventType eventType);
#interface CustomWindow : NSWindow
#property (nonatomic, copy) CustomWindowSpacebarKeyCallback spacebarKeyCallback;
#end
#implementation CustomWindow
- (void) sendEvent:(NSEvent *)theEvent
{
[super sendEvent:theEvent];
if (([theEvent type] == NSKeyDown || [theEvent type] == NSKeyUp) && _spacebarKeyCallback) {
NSString *keysPressed = [theEvent characters];
if ([keysPressed length] == 1) {
unichar keyChar = [keysPressed characterAtIndex:0];
if (keyChar == kSpaceCharCode) {
_spacebarKeyCallback([theEvent type]);
}
}
}
}
#end

Resources