I'm currently developing an application in OSX and I want to open my application when the user press a keyboard combos.
Asked
Active
Viewed 3,094 times
5
-
Do you know of any applications that currently exhibit this behavior? The question is a bit vague as to the desired effect. – Brandon Bradley Jul 18 '17 at 03:36
-
1Have a look at https://github.com/shpakovski/MASShortcut, it might be what you are looking for. – mttrb Jul 18 '17 at 04:23
-
I believe what you're looking for is called "hotkey". Example: https://stackoverflow.com/q/8201338/2227743 (there must be Swift versions available somewhere). – Eric Aya Jul 18 '17 at 07:56
1 Answers
4
Yes It Is Possible
If your applications was already opened by the user, you need only implement the keyDown(with event: NSEvent) function:
func keyDown(with event: NSEvent) {
guard let e = event.characters else {
return
}
//Do something based on keyboard input
}
If you are trying to get access to keys when your application isn't open, you've essentially implemented a keylogger
But You Really Shouldn't
From your question, it would seem that you want the latter option. This is problematic for 3 reasons:
- It is ridiculously insecure. You are storing extremely sensitive data outside of your application's sandbox, easily visible to prying eyes.
- It is a serious breach of trust. No other third party applications perform this kind of behavior, and so, when your app does it, the user will definitely think twice about having it installed, primarily because:
- It is redundant. There are already ways in which users can automate the opening of various applications. Whether through
- Their own Automator Script
- A dedicated Launcher Application
- By using a Finder Service
If, however, you're absolutely certain that you need this functionality, there is a generally agreed upon solution (edited)
Some Friendly Advice
As I'm sure you've noticed, your question wasn't particularly well received. To improve the responses you get in the future, just read the Stack Overflow Question Guide which, tl;dr entails:
- Being specific about your question (what you've tried, context, etc.)
- Googling your question beforehand

Brandon Bradley
- 3,160
- 4
- 22
- 39
-
1I'm quite happy installing Alfred which I can launch using a hotkey combo. I haven't lost trust in the developer of Alfred or the security of my Mac. While I would't want all my apps to be launched through a hotkey I don't see the problem with certain utilities being launched this way. – mttrb Jul 18 '17 at 04:21
-
I'd agree that for some apps it is desirable, and I didn't say it shouldn't be done under any circumstances. That said, for the vast majority of apps, it is not only unnecessary, but detrimental. Many developers, being both technically inclined, and generally fond of their apps, are wont to enable this kind of feature, so the downsides of doing it should be made clear. Certainly there are times when deviating from the platform standards are the right course of action, but, for the most part, there is a reason for their being done that way. – Brandon Bradley Jul 18 '17 at 04:35
-
@EricAya While that is information I provide, I also link to a program which could be used to process hotkeys. – Brandon Bradley Jul 18 '17 at 12:12