question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[cocoa] Keyboard shortcuts: copy, paste, etc

See original GitHub issue

Expected 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

  1. 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, setting autoenablesItems = True means that all user defined items will always be enabled (in the current app implementation).
  2. 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:closed
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
freakboy3742commented, Dec 28, 2020

@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.

0reactions
trankovcommented, Jun 27, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Macos – Cocoa Keyboard Shortcuts in Dialog without an Edit Menu ...
If the user right-clicks/control-clicks the text field, a menu appears that allows cut, copy, paste, etc. However, the standard Command-X, Command-C, ...
Read more >
Cocoa Keyboard Shortcuts in Dialog without an Edit Menu
Just add a new MenuItems in one of your menus and name them 'Copy', 'Cut' and 'Paste'. Add the correct shortcut keys to...
Read more >
Text Testimonials: Working with Text in OS X | Mac OS X Power Tools
Cocoa applications all have this ability built in, and you can even copy text ... share a set of keyboard shortcuts that are...
Read more >
Keyboard mappings using a PC keyboard on a Macintosh
System Shortcuts ; Open file. CONTROL+O · COMMAND+O ; Paste Clipboard content. CONTROL+V · COMMAND+V ; Print. CONTROL+P · COMMAND+P ; Exit/quit program....
Read more >
Customize the Cocoa text binding system - Mac OS X Hints
Hint #0: Default keyboard shortcuts. I've made a list of all the default keyboard shortcuts in Cocoa text boxes. Hint #1: Key bindings...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found