[cocoa] Keyboard shortcuts: copy, paste, etc
See original GitHub issueExpected Behavior
One would typically expect from an app on macOS that common keyboard shortcuts such as CMD+C, CMD+V, etc “just work”. However, they don’t on widgets such as TextInput
.
Current Behavior
Keyboard events handled by a widget itself do work (e.g., arrow keys in a TreeView
, backspace in a TextInput
). However, globally defined shortcuts such as copy and paste do not work.
Possible fixes
-
This is typically handled in a NSApplication by adding the respective menu bar items with selectors (e.g.,
SEL('copy:')
for CMD+C). This is what users on macOS expect, but it has two disadvantages:- It requires always having those menu entries. But some use cases work better without a menu bar, such as a system tray app which occasionally shows a window.
- When we add those items to the menu by default, they will be always enabled, unless the menu has set
autoenablesItems = True
. In the latter case, they will be automatically enabled/disabled when the action becomes available, e.g., ‘Minimise’ (CMD+M) will only be enabled when the window supports being minimised. However, settingautoenablesItems = True
means that all user defined items will always be enabled (in the current app implementation).
-
One could intercepting the key events in the NSApplication delegate. This has the disadvantage that the default menu bar items will not be visible unless explicitly added by the user. However, the commands will always work, regardless of the chosen menu bar configuration.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
@whateverforever This is a volunteer-driven project; we can’t really offer any estimates for when a any given bug will be fixed, or feature implemented, because it’s entirely dependent on contributions provided by the community.
You’re correct in your understanding about using
SEL('copy:')
though - if you import and use that call, the code will become macOS specific, and won’t run on Windows and Linux.Well, but finally, please, where can I find information about how to implement Copy/Paste to
TextInput
? All I want just when user press Cmd-V in focused text field the clipboard text has been pasted to it. But I cannot find the description. Does it support ‘on key press’ event or something like this? I don’t know where to read this. Please, can somebody help me and answer? Thanks in advance!