macOS Keyboard Shortcuts in Swift - macos

I am creating a macOS app and would like to create a keyboard shortcut for when the user taps the space bar for one of my buttons.
I am not sure where I should start and even where the documentation is for this? What should I be looking for?

You can now add keyboard shortcuts for many of your UI elements directly in a storyboard, by clicking on the Attribute inspector and scrolling down to "Key Equivalent". You just click in the Key Equivalent text field and press the key equivalent on your keyboard.

Related

Cocoa application Enter Full Screen no keyboard shortcut shown

I have a Cocoa application. In the View menu, the system adds an Enter Full Screen menu item. In other applications I see that this menu item has the keyboard shortcut ^⌘F. However in my application this keyboard shortcut is not shown in the menu and unsurprisingly this key combination does not enter full screen.
Interestingly, this keyboard shortcut, although shown in other applications, does not work.
How do I debug and/or fix this?
Any new Xcode application should get the View menu with Enter Full Screen already setup.
You can add the Key command in the Attributes Inspector.
If the key command doesn't work in ANY application, then that's likely a non-programming problem: most likely a conflict with a duplicate key command that's been configured.
Check System Preferences > Keyboard > Shortcuts > App Shortcuts.

Opening onscreen keyboard wxpython

I'm developing an app using wxpython for use specifically with a Microsoft surface, which requires text input. Is there a way to automatically bring up the onscreen keyboard when an input box is selected?
You can create an onscreen keyboard with wxPython for your applications. If you want keyboard to appear when you click your app's wx.TextCtrl, you just need to bind wx.EVT_LEFT_DOWN and/or wx.EVT_LEFT_UP events to it. However, if you want keyboard to appear when any app's input is clicked then it is really hard to achieve, instead you can assign a function key to popup.

How to unbind Command-Control-Space key from Mac OS X 10.9?

How can I unbind Command-Control-Space from Mac OS X 10.9?
This shortcut shows Special Characters table and conflicts with my Emacs key binding, and I couldn't disable it from System Preference->Keyboard->Shortcuts.
Thanks.
At least on macOS Sierra to macOS Big Sur ⌃Space is the default binding for Select the previous input source which is on by default (even if only one input source is activated).
You can free it by:
Open System Preferences
Go to Keyboard > Shortcuts > Input Sources
Untick "Select the previous input source"
Afterwards, you should be able to bind it as expected.
You can create custom keyboard shortcuts for most app's menubar choices in System Preferences. If a desired key combination is losing precedence to a default shortcut that you don't use and can't easily disable, simply override it with a new, unobtrusive shortcut.
Open System Prefs / Keyboard / Shortcuts. Select App Shortcuts from the left pane. Toggle the All Applications category's triangle in the main window to point downward (if it's not open already).
If there's an item named Emoji & Symbols* shown there, then click its shortcut combination and enter a new shortcut (such as option-shift-command-t, in this case).
If there's not an item named Emoji & Symbols under All Applications, click the + button at the bottom, type or copy-paste Emoji & Symbols, and then enter a new keyboard shortcut (option-shift-command-t, or anything really). This will free the control-command-space combination for you to use as a specialized shortcut elsewhere.
To remove your custom shortcut, just click to highlight it in the main window of this preference pane, and click the – button at the bottom. The custom shortcut will disappear and the default action will resume.
*Note: On versions older than Mac OS 10.10.3, the menu item is called Special Characters… instead of Emoji & Symbols.
I don't know of any way to disable this, but an alternative option might be to create a shortcut for the app you want to use that in. I created a Command-Control-Space shortcut for Chrome and now Command-Control-Space doesn't bring up the special character palette anymore in Chrome.
failing that you may be better off asking in Apple Stackexchange

How can I make another button highlighten on popup window on Mac by using Keyboard

On the above shown asking popup window on Mac, how can I select another button (left button) by using keyboard.
Without clicking mouse button, I want to make left button highlighten.
Is there any shortcuts?
Go to Preferences -> Keyboard. At the bottom, turn on "All controls" under "Full Keyboard Access".
The alternate option will be highlighted with a blue ring. Hitting space will activate this. If there are multiple options, hitting tab will alternate between them.
For English/Mac OSX 10.10:
Go to Keyboard in System Preferences, and then select 'All controls'. Space will select the alternate option if two options. If more than two options then tab will alternate between them.
PS: I would much rather the option of using arrow keys and enter. Interested to know if anyone knows how to hack this?
After reading Tricon's answer, I got the way!!!
Just see the following shortcuts.
Preferences -> Keyboard -> Keyboard & keyboard input (I don't know the correct English menu, I'm using Korean "탭이 초점을 이동하는 방식 변경 (^F7) )
Once you do ^F7 (In case of mac book, Control + fn + F7) on a popup window, you can travel over buttons on any popup windw!!!
Thank you Tricon for giving me clue :)
In Catalina in Keyboard -> Shortcuts press Use keyboard navigation to mve focus between controls. Then you can use Tab to highlight another button and use Space to actually press it.

Programmatically Activate NSMenuItem

When you press the keys for an NSMenuItem keyboard shortcut on Mac, the menu itself highlights to indicate that an action in that menu has been activated.
If you are not familiar with the effect try it now by selecting some text and while pressing CMD-C, watch the Edit menu. It will flash blue to indicate you activated a shortcut for an item in that menu.
I want to achieve the same effect programmatically, preferably without faking the keyboard entry. Thanks for your time.
Use the Accessibility framework. Find the menu item and send it an AXPress action.

Resources