How to intercept toggling fullscreen mode - xcode

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

Related

Prevent QLPreviewView from grabbing focus

I have a list of files. Next to it I have a QLPreviewView which shows the currently selected file.
Unfortunately QLPreviewView loads a web view to preview bookmark files. Some web pages can grab keyboard focus. E.g. the Gmail login form places the insertion point into the user name field.
This breaks the flow of my application. I want to navigate my list using arrow keys. This is disrupted when keyboard focus is taken away from the table view.
So far the best I could come up with is to override - [NSWindow makeFirstResponder:] and not call super for instances of classes named with a QL prefix. Yuck.
Is there a more reasonable way to
Prevent unwanted changes of first responder?
or prevent user interaction on QLPreviewView and its subviews?
I ended up using a NSWindow subclass that allows QLPreviewViews and its private subviews to become first responder on user interaction, but prevents these views from simply stealing focus.
- (BOOL)makeFirstResponder:(NSResponder *)aResponder
{
NSString *classname = NSStringFromClass([aResponder class]);
// This is a hack to prevent Quick Look from stealing first responder
if ([classname hasPrefix:#"QL"]) {
BOOL shouldMakeFirstRespnder = NO;
NSEvent *currentEvent = [[NSApplication sharedApplication] currentEvent] ;
NSEventType eventType = currentEvent.type;
if ((eventType == NSLeftMouseDown) || (eventType == NSRightMouseDown) || (eventType == NSMouseEntered)) {
if ([aResponder isKindOfClass:[NSView class]]) {
NSView *view = (NSView *)aResponder;
NSPoint locationInWindow = currentEvent.locationInWindow;
NSPoint locationInView = [view convertPoint:locationInWindow fromView:nil];
BOOL pointInRect = NSPointInRect(locationInView, [view bounds]);
shouldMakeFirstRespnder = pointInRect;
}
}
if (!shouldMakeFirstRespnder) {
return NO;
}
}
return [super makeFirstResponder:aResponder];
}
Maybe you can subclass QLPreviewView and override its becomeFirstResponder so that you can either enable or disable it when your application should allow it to accept focus.
Header
#interface MyQLPreviewView : QLPreviewView
#end
Implementation
#implementation
- (BOOL)becomeFirstResponder
{
return NO;
}
#end

Hiding the dock icon without hiding the menu bar

I use the ideas in this thread to hide the dock icon of my app optionally. If the dock icon is shown after all, the menu bar should be shown too. Only with Jiulong's answer I haven't been able to make this work. The menu bar is still hidden.
So basically 'Application is agent' is set to '1' in the InfoPList, and this code is used :
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"LaunchAsAgentApp"]) {
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetSystemUIMode(kUIModeNormal, 0);
[[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:#"com.apple.dock" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:nil];
[[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];
}
So why doesn't the menu bar show up, until I hide and refocus the app? Is there any fix for this? I saw that the 'Quick Search Box' for mac app doesn't show the menu bar upon launching either...
EDIT : I contacted Apple, and they gave me a carbon and a non-carbon solution. Given a new project with 'Application is Agent' set to 'YES' in the Plist file, then this code could be used in the AppDelegate class :
#define USE_CARBON 0
//
// Note: NSLogDebug is defined in the projects pre-compiled (.pch) file
//
#implementation AppDelegate
{
BOOL show_icon;
}
// Application will finish launching
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
NSLogDebug();
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
if (![[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
// exit(173);
}
#if 1
show_icon = YES;
#else
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *hasDockIconDefaultsKey = #"Has Dock Icon?";
// note: toggles value on each run (normally set from user pref pannel)
show_icon = [userDefaults boolForKey:hasDockIconDefaultsKey];
[userDefaults setBool:!show_icon forKey:hasDockIconDefaultsKey];
#endif // if 1
if (show_icon) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
[NSMenu setMenuBarVisible:NO];
[NSMenu setMenuBarVisible:YES];
}
[NSApp activateIgnoringOtherApps:YES];
} // applicationWillFinishLaunching
// Application did finish launching
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSLogDebug();
// Insert code here to initialize your application
if (show_icon) {
#if USE_CARBON
ProcessSerialNumber psn = {0, kCurrentProcess};
OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
if (noErr != returnCode) {
NSLog(#"TransformProcessType error: %d (0x%0X)", returnCode, returnCode);
}
ProcessSerialNumber psnx = {0, kNoProcess};
GetNextProcess(&psnx);
SetFrontProcess(&psnx);
#else // if 0
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSRunningApplication * menuBarOwningApplication = [sharedWorkspace menuBarOwningApplication];
(void) [menuBarOwningApplication activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif
[self performSelector:#selector(setFront) withObject:nil afterDelay:0.];
}
} // applicationDidFinishLaunching
// Close app when main window is closed
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return (YES);
}
- (void)setFront;
{
#if USE_CARBON
ProcessSerialNumber psn = {0, kCurrentProcess};
SetFrontProcess(&psn);
#else // if USE_CARBON
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif // USE_CARBON
}
#end
Note that I filed a bug report too.
Here's a Swift version of the non-carbon solution :
func applicationWillFinishLaunching(_ notification: Notification) {
if showIcon {
NSApp.setActivationPolicy(.regular)
NSApp.presentationOptions = []
NSMenu.setMenuBarVisible(false)
NSMenu.setMenuBarVisible(true)
}
NSApp.activate(ignoringOtherApps: true)
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSApplication.shared.activate(ignoringOtherApps: true)
if showIcon {
let workspace = NSWorkspace.shared
let application = workspace.menuBarOwningApplication
application?.activate(options: .activateIgnoringOtherApps)
self.perform(#selector(activate), with: nil, afterDelay: 0.0)
}
}
#objc private func activate() {
NSRunningApplication.current.activate(options: .activateIgnoringOtherApps)
}
First, you should use -[NSApplication setActivationPolicy:] rather than TransformProcessType() and -[NSApplication setPresentationOptions:] rather than SetSystemUIMode() in modern code. If switching to those is not enough to fix the problem with the menu bar not updating, I recommend that you try using -setPresentationOptions: or +[NSMenu setMenuBarVisible:] to hide the menu bar and then immediately reverse that operation.
Also, drop that business with trying to activate the Dock.
So, something like:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"LaunchAsAgentApp"]) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setPresentationOptions:NSApplicationPresentationDefault]; // probably not necessary since it's the default
[NSMenu setMenuBarVisible:NO]; // these two lines may not be necessary, either; using -setActivationPolicy: instead of TransformProcessType() may be enough
[NSMenu setMenuBarVisible:YES];
}

Cocoa webview textfield not accepting keystrokes

I am a newbie in OSx development.
I have a Cocoa application which uses a Webview. Everything is working fine, except for the textfield in the webview. I know how to enable keystrokes in NSTextField, but not the ones in the Webview. I've been searching the web all day, but with no luck.
I badly need some help on how to enable the keystrokes to implement keyboard shortcut keys.
Example:
copy -> command + c
paste -> command + v
cut -> command + x
Any help would be very much appreciated.
I got the answer now. I've realized that I forgot to implement
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
to the class which handles the Webview.
#Kimpoy, thanks for the reference to performKeyEquivalent!
For completeness, I implemented it this way...
Subclass your webview from WebView and implement the method:
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent {
NSString * chars = [theEvent characters];
BOOL status = NO;
if ([theEvent modifierFlags] & NSCommandKeyMask){
if ([chars isEqualTo:#"a"]){
[self selectAll:nil];
status = YES;
}
if ([chars isEqualTo:#"c"]){
[self copy:nil];
status = YES;
}
if ([chars isEqualTo:#"v"]){
[self paste:nil];
status = YES;
}
if ([chars isEqualTo:#"x"]){
[self cut:nil];
status = YES;
}
}
if (status)
return YES;
return [super performKeyEquivalent:theEvent];
}
Credit to #aventurella over here: https://github.com/Beats-Music/mac-miniplayer/issues/3. Just modified slightly to return the super response as default because it should propagate down to its subviews.
As a note, I'd recommend implementing a log or similar in your custom webview to make sure you really are working with your class:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
NSLog(#"Custom webview running...");
}

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

Drag and Drop with NSStatusItem

I'm trying to write an application that allows the user to drag files from the Finder and drop them onto an NSStatusItem. So far, I've created a custom view that implements the drag and drop interface. When I add this view as a subview of an NSWindow it all works correctly -- the mouse cursor gives appropriate feedback, and when dropped my code gets executed.
However, when I use the same view as an NSStatusItem's view it doesn't behave correctly. The mouse cursor gives appropriate feedback indicating that the file can be dropped, but when I drop the file my drop code never gets executed.
Is there something special I need to do to enable drag and drop with an NSStatusItem?
I finally got around to testing this and it works perfectly, so there's definitely something wrong with your code.
Here's a custom view that allows dragging:
#implementation DragStatusView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//register for drags
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
//the status item will just be a yellow rectangle
[[NSColor yellowColor] set];
NSRectFill([self bounds]);
}
//we want to copy the files
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
return NSDragOperationCopy;
}
//perform the drag and log the files that are dropped
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
NSLog(#"Files: %#",files);
}
return YES;
}
#end
Here's how you'd create the status item:
NSStatusItem* item = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
DragStatusView* dragView = [[DragStatusView alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)];
[item setView:dragView];
[dragView release];
Since Yosemite, the method for setting a view on NSStatusItem is deprecated but fortunately there is a much nicer way using the new NSStatusItemButton property on NSStatusItem:
- (void)applicationDidFinishLaunching: (NSNotification *)notification {
NSImage *icon = [NSImage imageNamed:#"iconName"];
//This is the only way to be compatible to all ~30 menu styles (e.g. dark mode) available in Yosemite
[normalImage setTemplate:YES];
statusItem.button.image = normalImage;
// register with an array of types you'd like to accept
[statusItem.button.window registerForDraggedTypes:#[NSFilenamesPboardType]];
statusItem.button.window.delegate = self;
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
return NSDragOperationCopy;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
//drag handling logic
}
Please be aware that the button property is only available starting in 10.10 and you might have to keep your old solution if you support 10.9 Mavericks or below.

Resources