Toggle Keyboard Viewer on Mac OS X programmatically - macos

I created a cocoa app which has a window with a text field to get user input, a small keyboard-icon button to bring up the keyboard viewer. When the user clicks OK or Cancel button to finish, i want to hide the keyboard viewer. What I've done is as follows:
//action for keyboard-icon button
-(IBAction)input:(id)sender
{
[self toggleKeyboard:YES];
}
//action for Cancel button
-(IBAction)cancel:(id)sender
{
[self toggleKeyboard:NO];
[NSApp abortModal];
[[self window] orderOut: self];
}
//action for OK button
-(IBAction)ok:(id)sender
{
[self toggleKeyboard:NO];
[NSApp stopModal];
[[self window] orderOut: self];
}
-(void)toggleKeyboard:(BOOL)show
{
NSDictionary *property = [NSDictionary dictionaryWithObject:(NSString*)kTISTypeKeyboardViewer
forKey:(NSString*)kTISPropertyInputSourceType];
NSArray *sources = (NSArray*)TISCreateInputSourceList((CFDictionaryRef)property, false);
TISInputSourceRef keyboardViewer = (TISInputSourceRef)[sources objectAtIndex:0];
if (show == YES)
{
TISSelectInputSource(keyboardViewer);
}
else
{
TISDeselectInputSource(keyboardViewer);
}
CFRelease((CFTypeRef)sources);
}
I can launch keyboard viewer successfully, but it cannot be hidden by TISDeselectInputSource at all times.

It is possible to programmatically hide the Keyboard Viewer. I made an AppleScript for my media center a little while ago that will toggle it. I'm not going to include the bit that shows it, partly because you've already figured that out, but mostly because the way I did it was to call some executable that I can't remember where I got it from.
Anyway, here's the AppleScript:
tell application "System Events"
if exists (process "Keyboard Viewer") then
click (process "Keyboard Viewer"'s window 1's buttons whose subrole is "AXCloseButton")
end if
end tell
This uses Accessibility, so you need to have "Enable access for assistive devices" turned on in order for it to work.
Note this same thing can be accomplished in a Objective-C / C++ app using the Accessibility API, which you could use to avoid having to call into AppleScript in your app.

Related

Autohide Toolbar only in full screen mode in Cocoa

My goal is simple and yet I cannot find a solution in spite of lots of searching.
Basically, when my app is in full-screen (kiosk) mode, I want the toolbar only to auto-hide, but I want the menu bar hidden.
Apparently this combination is not valid. I've tried:
- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions: (NSApplicationPresentationOptions)proposedOptions
{
return (NSApplicationPresentationFullScreen |
NSApplicationPresentationHideDock |
NSApplicationPresentationHideMenuBar |
NSApplicationPresentationAutoHideToolbar);
}
I get the following exception:
"... fullscreen presentation options must include NSApplicationPresentationAutoHideMenuBar if NSApplicationPresentationAutoHideToolbar is included"
Thing is, I don't want the menu bar displayed at all!
So, I'm presuming this is not possible using the standard presentation options. Any ideas how I might approach implementing this behaviour manually?
I'm thinking along the lines of: detect the mouse position and only show/hide the toolbar when the mouse is at/near the top of the screen.
I'm very new to Cocoa so not sure where I would start to achieve this. Any help much appreciated!
Many thanks,
John
I've got It to work, but only by using private APIs.
First I had to find out how to prevent the menubar from appearing. I discovered the functions _HIMenuBarPositionLock and _HIMenuBarPositionUnlock, from Carbon (link the app with Carbon.framework).
Then I had to create a custom subclass of NSToolbar, at awakeFromNib I register notification observers to lock and unlock the menubar when the window enters and exits fullscreen, respectively:
- (void)awakeFromNib
{
[super awakeFromNib];
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillEnterFullScreenNotification object:[self _window] queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
// lock menubar position when entering fullscreen so It doesn't appear when the mouse is at the top of the screen
_HIMenuBarPositionLock();
}];
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillExitFullScreenNotification object:[self _window] queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
// unlock menubar position when exiting fullscreen
_HIMenuBarPositionUnlock();
}];
[self _setupToolbarHotspotTrackingView];
}
_setupToolbarHotspotTrackingView is a method on SOToolbar which adds a view to the window, this view will be used to track the mouse location and show/hide the toolbar accordingly.
- (void)_setupToolbarHotspotTrackingView
{
NSView *contentView = [self _window].contentView;
self.toolbarHotspotTrackingView = [[SOToolbarTrackingView alloc] initWithFrame:contentView.bounds];
[contentView addSubview:self.toolbarHotspotTrackingView];
self.toolbarHotspotTrackingView.autoresizingMask = NSViewWidthSizable|NSViewHeightSizable;
self.toolbarHotspotTrackingView.toolbar = self;
}
I also had to override _attachesToMenuBar on SOToolbar so the animation works properly.
- (BOOL)_attachesToMenuBar
{
return NO;
}
SOToolbarTrackingView sets up a tracking area for mouse moved events and checks to see if the mouse is at the top of the window. It then calls some methods on the private class NSToolbarFullScreenWindowManager to show and hide the toolbar.
There's too much stuff to explain It all in detail here, I've uploaded my experimental project so you can take a look. Download the sample project here.

OS X: Show interface of application agent (UIElement)

How do I make the interface for an application that has 'Application is agent (UIElement)' set to yes reappear?
The interface shows up the first time I start the app, but if I close the window, and the click on the app's icon then nothing happens. I guess that it's because OS X is trying to start the app again, and there is some mechanism preventing that. What I would like is this:
The first click on the app's icon should launch the app and show the interface.
If the interface has been closed down (but the app is still running in the background) a subsequent click on the icon should just show the interface.
If the interface is already shown a click on the icon should simply move the window to the foreground.
Here is a way you can do it:
1) add + initialize method to your app delegate
+ (void)initialize
{
// check if there is a running instance of your app
NSArray * apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
if ([apps count] > 1)
{
//post notification to it to update inteface
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:#"updateInterface" object:nil];
//quit current instance of the app, coz you don't need two apps running continiously
exit(0);
}
}
2) Register your app for the notification
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(updateInterface:) name:#"updateInterface" object:nil];
}
3) Add updateInterface method
- (void)updateInterface:(NSNotification *)aNotification
{
// handle your interface here
// ....
// move your app forward
[NSApp activateIgnoringOtherApps:YES];
}
I found the answer here: Closing Mac application (clicking red cross on top) and reopening by clicking dock icon.
- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
hasVisibleWindows:(BOOL)flag
{
[self.window makeKeyAndOrderFront:self];
return YES;
}

NSToolbar shown when entering fullscreenmode

I am developing an app in which the toolbar can be shown/hide by the user using a button. The problem is the following: If the user chooses to hide the toolbar and then enters the fullscreen mode, the toolbar is shown.
The user interface has been created programmatically (i.e. not using Interface Builder).
This is the toolbar creation in the app delegate:
mainToolbar = [[NSToolbar alloc] initWithIdentifier:MAIN_TOOLBAR];
[mainToolbar setAllowsUserCustomization:NO];
[mainToolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
[mainToolbar setDelegate:self];
[window setToolbar: mainToolbar];
These are the actions performed by the buttons:
-(void)hideToolbar {
editing = YES;
[mainToolbar setVisible:NO];
}
-(void)showToolbar {
editing = NO;
[mainToolbar setVisible:YES];
}
I have tried to fix it using window delegate methods but still the toolbar is shown when entering full screen mode regardless the value of editing.
- (void)windowDidEnterFullScreen:(NSNotification *)notification {
[mainToolbar setVisible:!editing];
}
- (void)windowDidExitFullScreen:(NSNotification *)notification {
[mainToolbar setVisible:!editing];
}
Many thanks in advance!
I could not find a way to maintain the hidden/shown state of the toolbar when the window goes full screen, but you can set the toolbar to always be hidden in full screen, and to animate in when the user goes to the top of the screen. In your window delegate, you can set NSApplicationPresentationOptions to return NSApplicationPresentationAutoHideToolbar in as one of the options. Mine looks like this:
- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
{
return (NSApplicationPresentationFullScreen |
NSApplicationPresentationHideDock |
NSApplicationPresentationAutoHideMenuBar |
NSApplicationPresentationAutoHideToolbar);
}
Here is the relevant documentation: https://developer.apple.com/library/mac/#documentation/General/Conceptual/MOSXAppProgrammingGuide/FullScreenApp/FullScreenApp.html

NSMenu doesn't start tracking

I have a little cocoa app which usually operates in the background (as agent). Sometimes I'd like to be able to popup a contextmenu (no window or s.th. visible at this time).
As I'm only targetting Snow Leopard I tried this:
if (windows) {
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:#"test"] autorelease];
[theMenu setShowsStateColumn:NO];
[theMenu setAutoenablesItems:NO];
for (id item in windows) {
NSString *labelText = #"some text";
NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:labelText
action:#selector(menuItemSelected:)
keyEquivalent:#""] autorelease];
[theMenuItem setTarget:self];
[theMenuItem setRepresentedObject:item];
[theMenuItem setEnabled:YES];
[theMenuItem setImage:icon];
[theMenu addItem:theMenuItem];
}
[theMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
}
The menu popsup perfectly but if I hover the items with the mouse cursor they don't highlight and I can't click them.
The menuItemSelected: method looks just like this:
-(IBAction)menuItemSelected:(id)sender {
}
Any idea what I'm doing wrong?
I suspect that the windowing system doesn't consider your application to be active, and thus doesn't send mouse events to the menu you've created.
As an experiment, try creating a dummy window before popping up the menu. I'd create an NSPanel, possibly with style NSNonActivatingPanelMask. makeKeyAndOrderFront: your window/panel, then pop up the menu and see what happens.
If this works, I'd stick with the approach and hide the window.

Is there an equivalent technique in Cocoa for the synchronous TrackPopupMenu in Windows?

In response to a rightMouse event I want to call a function that displays a context menu, runs it, and responds to the selected menu item. In Windows I can use TrackPopupMenu with the TPM_RETURNCMD flag.
What is the easiest way to implement this in Cocoa? It seems NSMenu:popUpContextMenu wants to post an event to the specified NSView. Must I create a dummy view and wait for the event before returning? If so, how do I "wait" or flush events given I am not returning to my main ?
The 'proper' way to do this in Cocoa is to have your menu item's target and action perform the required method. However, if you must do it within your initial call, you can use [NSView nextEventMatchingMask:] to continually fetch new events that interest you, handle them, and loop. Here's an example which just waits until the right mouse button is released. You'll probably want to use a more complex mask argument, and continually call [NSView nextEventMatchingMask:] until you get what you want.
NSEvent *localEvent = [[self window] nextEventMatchingMask: NSRightMouseUpMask];
I think you'll find the 'proper' way to go much easier.
It appears that popUpContextMenu is already synchronous. Since I didn't see a way to use NSMenu without having it send a notification to an NSView I came up with a scheme that instantiates a temporary NSView. The goal is to display a popup menu and return the selected item in the context of a single function call. Following is code snippets of my proposed solution:
// Dummy View class used to receive Menu Events
#interface DVFBaseView : NSView
{
NSMenuItem* nsMenuItem;
}
- (void) OnMenuSelection:(id)sender;
- (NSMenuItem*)MenuItem;
#end
#implementation DVFBaseView
- (NSMenuItem*)MenuItem
{
return nsMenuItem;
}
- (void)OnMenuSelection:(id)sender
{
nsMenuItem = sender;
}
#end
// Calling Code (in response to rightMouseDown event in my main NSView
void HandleRButtonDown (NSPoint pt)
{
NSRect graphicsRect; // contains an origin, width, height
graphicsRect = NSMakeRect(200, 200, 50, 100);
//-----------------------------
// Create Menu and Dummy View
//-----------------------------
nsMenu = [[[NSMenu alloc] initWithTitle:#"Contextual Menu"] autorelease];
nsView = [[[DVFBaseView alloc] initWithFrame:graphicsRect] autorelease];
NSMenuItem* item = [nsMenu addItemWithTitle:#"Menu Item# 1" action:#selector(OnMenuSelection:) keyEquivalent:#""];
[item setTag:ID_FIRST];
item = [nsMenu addItemWithTitle:#"Menu Item #2" action:#selector(OnMenuSelection:) keyEquivalent:#""];
[item setTag:ID_SECOND];
//---------------------------------------------------------------------------------------------
// Providing a valid windowNumber is key in getting the Menu to display in the proper location
//---------------------------------------------------------------------------------------------
int windowNumber = [(NSWindow*)myWindow windowNumber];
NSRect frame = [(NSWindow*)myWindow frame];
NSPoint wp = {pt.x, frame.size.height - pt.y}; // Origin in lower left
NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined
location:wp
modifierFlags:NSApplicationDefined
timestamp: (NSTimeInterval) 0
windowNumber: windowNumber
context: [NSGraphicsContext currentContext]
subtype:0
data1: 0
data2: 0];
[NSMenu popUpContextMenu:nsMenu withEvent:event forView:nsView];
NSMenuItem* MenuItem = [nsView MenuItem];
switch ([MenuItem tag])
{
case ID_FIRST: HandleFirstCommand(); break;
case ID_SECOND: HandleSecondCommand(); break;
}
}
There is no direct equivalent, except in Carbon, which is deprecated.
For detecting the right-click, follow these instructions. They ensure that you will properly detect right-clicks and right-holds and display the menu when you should and not display it when you shouldn't.
For following events, you might try [[NSRunLoop currentRunLoop] runMode:NSEventTrackingRunLoopMode untilDate:[NSDate distantFuture]]. You will need to call this repeatedly until the user has chosen one of the menu items.
Using nextEventMatchingMask:NSRightMouseUpMask will not work in all, or even most, cases. If the user right-clicks on your control, the right mouse button will go up immediately after it goes down, without selecting a menu item, and the menu item selection will probably (though not necessarily) happen through the left mouse button. Better to just run the run loop repeatedly until the user either selects something or dismisses the menu.
I don't know how to tell that the user has dismissed the menu without selecting anything.

Resources