System Keyboard Shortcut

I’ve had a look around the forum search for this and can’t find exactly what I want - so apologies if I’ve missed something glaringly obvious.

Is there a way to set a system wide keyboard shortcut with Xojo? On Mac only, so I don’t mind too much if it’s with declares.

What I’m trying to achieve is:

• App is open, but the window1 is not open.
• A keyboard shortcut is pressed. For example: cmd+shift+o
• This basically calls window1.Show

Is this possible with Xojo?

MainMenuBar is the MenuEditor to open.

Check MenuBar, MenuItem in the LR.

[quote=379029:@Dave W]
Is there a way to set a system wide keyboard shortcut with Xojo? On Mac only, so I don’t mind too much if it’s with declares.[/quote]

I have a program that sits in the background and pops up when the user makes a special key combination.

I did it with a 20 ms multiple timer that monitors keyboard.asyncKeydown and Keyboard.asyncControlkey ; when I detect Ctrl-K, I show the app with this declare from Axel Schneider which brings the app to the top :

declare function NSClassFromString lib "Cocoa" ( inName as CFStringRef ) as Ptr declare function sharedApplication lib "Cocoa" selector "sharedApplication" ( classRef as Ptr ) as Ptr Dim myApplication as Ptr = sharedApplication( NSClassFromString( "NSApplication" ) ) declare sub activateIgnoringOtherApps lib "Cocoa" selector "activateIgnoringOtherApps:" ( appRef as Ptr, flag as boolean ) activateIgnoringOtherApps( myApplication, True ) me.AcceptFocus = True me.UseFocusRing = True me.SetFocus

http://documentation.xojo.com/index.php/Keyboard

This will work from any place on the Mac. The only part you may need to address is to inhibit App Nap, which will happen if the app is in the background too long. Search the forum for “app nap”, there have been quite a few threads about it.

If you are not averse to using plugins, I have used http://www.monkeybreadsoftware.net/class-hotkeymbs.shtml from MBS for quite a few applications.

It’s simple to use and doesn’t come with the overhead of running a timer.

[quote=379030:@Emile Schwarz]MainMenuBar is the MenuEditor to open.

Check MenuBar, MenuItem in the LR.[/quote]

Will this work if the app is in the background? The user opened the app and then went into Firefox - would the built in shortcut still call the app? Presuming the same keyboard shortcut wasn’t defined within Firefox that is.

[quote=379038:@Michel Bujardet]I have a program that sits in the background and pops up when the user makes a special key combination.

I did it with a 20 ms multiple timer that monitors keyboard.asyncKeydown and Keyboard.asyncControlkey ; when I detect Ctrl-K, I show the app with this declare from Axel Schneider which brings the app to the top :
[/quote]

I don’t fully understand the code but I believe the gist is the declare deals with bringing the app to the top. So as such, doubling up with above, would the built in functionality of keyboard shortcuts and Window1.show be enough for the basic functionality? Again, presuming app nap was turned off.

To be honest I was expecting a declare for the system wide shortcut to register rather than the showing of the window.

Thank you all for your replies.

No, and what was described is not the functionality you are looking for. That post is plain old wrong and unrelated to your request, and there’s no nice way to say it.

You have understood Michel’s declare perfectly. His suggestion is to use a constantly running timer to detect the key presses. This will technically work, however it will waste a lot of power (bad for battery life if you’re on a laptop). Michel’s suggestion does not have anything to do with the completely wrong post.

The plugin that Jared linked to is the system wide shortcut thing, but it requires the plugin. You’ll have to figure out the declare on your own as I don’t think anyone’s shared one for this function with Cocoa yet. I have seen a Carbon one floating around, but I wouldn’t expect it to work for long as Apple kills off 32 bit.

I will add that Michel’s suggestion isn’t necessarily a bad one in terms of getting the functionality working without much implementation. It’s probably the least costly for you as a developer since there’s no plugin cost and no time cost investigating the declare.

The plugin/declare way is just more efficient on the end user’s system.

On the other hand, creating a 4MB application to implement an OS Keyboard shortcut… you better try a different development system for this kind of things.

[quote=379215:@Dave W]
I don’t fully understand the code but I believe the gist is the declare deals with bringing the app to the top. So as such, doubling up with above, would the built in functionality of keyboard shortcuts and Window1.show be enough for the basic functionality? Again, presuming app nap was turned off.

To be honest I was expecting a declare for the system wide shortcut to register rather than the showing of the window.[/quote]

It seems what you want to do is fairly similar. Including showing a window !

[quote=379029:@Dave W]
• App is open, but the window1 is not open.
• A keyboard shortcut is pressed. For example: cmd+shift+o
• This basically calls window1.Show[/quote]

Window1.show won’t suffice if your app is in the background. You need to bring the app to the front.

Note that if you can afford MBS Plugin complete, all that can be done with even less code.

Now it’s up to you. I was just trying to help, not to sell you anything :stuck_out_tongue: