Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Command-c/Command-v shortcut in Mac to copy/paste text?

I have a Java Swing application that i want to run on Mac OS X. I want to use the normal Mac copy/paste shortcuts to copy/paste text to a text field in my Java application.

Ctrl+c & Ctrl+v does the trick but i want to use Command+c & Command+v instead. How can i do that?

like image 548
Brad Avatar asked Aug 31 '11 05:08

Brad


People also ask

Is Command c copy on Mac?

Press Command-C. Or choose Edit > Copy from the menu bar. Or Control-click (or right-click) the item you selected, then choose Copy from the shortcut menu.

How do I press Ctrl C on a Mac?

For example, to use Command-C (copy), press and hold the Command key, then the C key, then release both keys. Mac menus and keyboards often use symbols for certain keys, including modifier keys: Command (or Cmd) ⌘

What are the commands for copy and paste on a Mac?

Command-C Copy the selected item to the Clipboard. This also works for files in the Finder. Command-V Paste the contents of the Clipboard into the current document or app. This also works for files in the Finder.


3 Answers

If you're using a 3rd-party L&F implementation it probably doesn't support the Mac's native keyboard shortcuts. The following code should reinstate the Mac's keyboard shortcuts for JTextFields after setting the L&F:

InputMap im = (InputMap) UIManager.get("TextField.focusInputMap");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), DefaultEditorKit.copyAction);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_DOWN_MASK), DefaultEditorKit.pasteAction);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.META_DOWN_MASK), DefaultEditorKit.cutAction);

Of course you'll only need to do this if you detect that the application is running on a Mac so that you don't affect the keyboard mappings for other OS's.

like image 70
cgull Avatar answered Oct 14 '22 05:10

cgull


Have you seen, know of or are using the SWT (Standard Widget Toolkit) maintained by Eclipse? That has a key press (SWT.COMMAND) for command.

like image 26
Psyrus Avatar answered Oct 14 '22 03:10

Psyrus


Are you running pure Swing? If so it should do that automatically(note it may not make the little menu animation if you dont use an Application bundle). If not then you will have to consult the documentation for whatever API you are using.

Just tested it and it works fine on Snow Leopard.

like image 33
user439407 Avatar answered Oct 14 '22 05:10

user439407