how to get find out the keyboard shortcut for the paste action?
Jeremy Wood
mickleness at gmail.com
Tue Feb 27 14:21:51 UTC 2024
Sorry this went so long without a reply.
> Is there a way to ask Java would the right keyboard shortcut should
be?
Yes. Try:
int modifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()
A sample usage is below.
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.KeyEvent;
public class ShortcutDemo extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ShortcutDemo d = new ShortcutDemo();
d.pack();
d.setVisible(true);
}
});
}
public ShortcutDemo() {
JMenuBar menuBar = new JMenuBar();
JMenu editMenu = new JMenu("Edit");
JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
menuBar.add(editMenu);
editMenu.add(copyMenuItem);
setJMenuBar(menuBar);
JLabel label = new JLabel("Placeholder Label");
label.setBorder(new EmptyBorder(20,20,20,20));
getContentPane().add(label);
}
}
Does this answer your question?
Regards,
- Jeremy
------ Original Message ------
>From mark.yagnatinsky at barclays.com
To client-libs-dev at openjdk.org
Date 2/7/24, 2:47:55 PM
Subject how to get find out the keyboard shortcut for the paste action?
>Some swing components such as text fields have built in support for
>copy and paste.
>
>But if you want to add such support for a component that doesn’t, then
>one of the things you might want to do is support standard keyboard
>shortcuts.
>
>Luckily, there’s a tutorial for doing this kind of thing, let’s see how
>they do it:
>
>https://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html
>
>Uh oh…
>
>getKeyStroke("ctrl V")
>
>The tutorial simply hardcodes the relevant shortcuts.
>
>I suspect that this works fine on windows, and will lead Mac users to
>grumble.
>
>Is there a way to ask Java would the right keyboard shortcut should be?
>
>I suppose one could always create a scratch text field just to look
>through its input map, but seems a bit hacky.
>
>This message is for information purposes only. It is not a
>recommendation, advice, offer or solicitation to buy or sell a product
>or service, nor an official confirmation of any transaction. It is
>directed at persons who are professionals and is intended for the
>recipient(s) only. It is not directed at retail customers. This message
>is subject to the terms at:
>https://www.cib.barclays/disclosures/web-and-email-disclaimer.html.
>
>For important disclosures, please see:
>https://www.cib.barclays/disclosures/sales-and-trading-disclaimer.html
>regarding marketing commentary from Barclays Sales and/or Trading
>desks, who are active market participants;
>https://www.cib.barclays/disclosures/barclays-global-markets-disclosures.html
>regarding our standard terms for Barclays Corporate and Investment Bank
>where we trade with you in principal-to-principal wholesale markets
>transactions; and in respect to Barclays Research, including
>disclosures relating to specific issuers, see:
>http://publicresearch.barclays.com.
>__________________________________________________________________________________
>If you are incorporated or operating in Australia, read these important
>disclosures:
>https://www.cib.barclays/disclosures/important-disclosures-asia-pacific.html.
>__________________________________________________________________________________
>For more details about how we use personal information, see our privacy
>notice:
>https://www.cib.barclays/disclosures/personal-information-use.html.
>__________________________________________________________________________________
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/client-libs-dev/attachments/20240227/7883b78d/attachment.htm>
More information about the client-libs-dev
mailing list