[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-2d-dev
Subject:    Re: how to get find out the keyboard shortcut for the paste action?
From:       "Jeremy Wood" <mickleness () gmail ! com>
Date:       2024-02-27 14:21:51
Message-ID: em455be664-76d1-43e4-ba85-729678b34dc6 () 76ca4640 ! com
[Download RAW message or body]

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@barclays.com
To client-libs-dev@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.
>__________________________________________________________________________________
>
[Attachment #3 (text/html)]

<html><head>


<style type="text/css"><!--#xc63ee9ebb21948e p.MsoNormal
{margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;}
#xc63ee9ebb21948e a:link
{color: rgb(5, 99, 193); text-decoration: underline;}
#xc63ee9ebb21948e div.WordSection1
{page: WordSection1;}
--></style><style id="css_styles" type="text/css"><!--blockquote.cite { margin-left: \
5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid \
#cccccc } blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; \
padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; \
} a img { border: 0px; }
table { border-collapse: collapse; }
li[style='text-align: center;'], li[style='text-align: center; '], \
li[style='text-align: right;'], li[style='text-align: right; '] {  \
list-style-position: inside;} body { font-family: Helvetica; font-size: 9pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; \
                padding-left: 0.3em; }
--></style>
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: \
after-white-space;"><div><div id="xd3c8c473fce94e65b3ca247f91ae7ad2"><div><span \
style="font-family: Calibri, sans-serif; font-size: 14.666667px;">Sorry this went so \
long without a reply.</span></div><div><span \
style="font-family:Calibri,sans-serif;font-size:14.666667px;"><br \
/></span></div></div></div><div>&gt;  <span style="font-family: Calibri, sans-serif; \
font-size: 14.666667px;">Is there a way to ask Java would the right keyboard shortcut \
should be?</span></div><div></div> <div style="clear:both"><font face="Calibri, \
sans-serif"><span style="font-size: 14.666667px;"><br /></span></font></div><div \
style="clear:both"><font face="Calibri, sans-serif"><span style="font-size: \
14.666667px;">Yes. Try:</span></font></div><div style="clear:both"><font \
face="Calibri, sans-serif"><span style="font-size: 14.666667px;"><br \
/></span></font></div><div style="clear:both"><font face="Calibri, sans-serif"><span \
style="font-size: 14.666667px;">int modifier = \
Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()</span></font></div><div \
style="clear:both"><br /></div><div style="clear:both"><font face="Calibri, \
sans-serif"><span style="font-size: 14.666667px;">A sample usage is below. \
</span></font></div><div style="clear:both"><font face="Calibri, sans-serif"><span \
style="font-size: 14.666667px;"><br /></span></font></div><div \
style="clear:both"><div style="color: rgb(8, 8, 8); font-family: &quot;JetBrains \
Mono&quot;, monospace; font-size: 9.8pt;"><pre><span style="color:#0033b3;">import \
</span><span style="color:#000000;">javax.swing.</span>*;<br /><span \
style="color:#0033b3;">import </span><span \
style="color:#000000;">javax.swing.border.EmptyBorder</span>;<br /><span \
style="color:#0033b3;">import </span><span \
style="color:#000000;">java.awt.</span>*;<br /><span style="color:#0033b3;">import \
</span><span style="color:#000000;">java.awt.event.KeyEvent</span>;<br /><br /><span \
style="color:#0033b3;">public class </span><span style="color:#000000;">ShortcutDemo \
</span><span style="color:#0033b3;">extends </span><span \
style="color:#000000;">JFrame </span>{<br />    <span style="color:#0033b3;">public \
static void </span><span style="color:#00627a;">main</span>(<span \
style="color:#000000;">String</span>[] args) {<br />        <span \
style="color:#000000;">EventQueue</span>.invokeLater(<span style="color:#0033b3;">new \
</span><span style="color:#000000;">Runnable</span>() {<br />            <span \
style="color:#9e880d;">@Override<br /></span><span style="color:#9e880d;">            \
</span><span style="color:#0033b3;">public void </span><span \
style="color:#00627a;">run</span>() {<br />                <span \
style="color:#000000;">ShortcutDemo d </span>= <span style="color:#0033b3;">new \
</span>ShortcutDemo();<br />                <span \
style="color:#000000;">d</span>.pack();<br />                <span \
style="color:#000000;">d</span>.setVisible(<span \
style="color:#0033b3;">true</span>);<br />            }<br />        });<br />    \
}<br /><br />    <span style="color:#0033b3;">public </span><span \
style="color:#00627a;">ShortcutDemo</span>() {<br />        <span \
style="color:#000000;">JMenuBar menuBar </span>= <span style="color:#0033b3;">new \
</span>JMenuBar();<br />        <span style="color:#000000;">JMenu editMenu </span>= \
<span style="color:#0033b3;">new </span>JMenu(<span \
style="color:#067d17;">"Edit"</span>);<br />        <span \
style="color:#000000;">JMenuItem copyMenuItem </span>= <span \
style="color:#0033b3;">new </span>JMenuItem(<span \
style="color:#067d17;">"Copy"</span>);<br />        <span \
style="color:#000000;">copyMenuItem</span>.setAccelerator(<span \
style="color:#000000;">KeyStroke</span>.getKeyStroke(<span \
style="color:#000000;">KeyEvent</span>.<span \
style="color:#871094;font-style:italic;">VK_C</span>, <br />                <span \
style="background-color: rgb(255, 255, 0);"><span \
style="color:#000000;">Toolkit</span>.<span \
style="font-style:italic;">getDefaultToolkit</span>().getMenuShortcutKeyMaskEx())</span>);<br \
/><br />        <span style="color:#000000;">menuBar</span>.add(<span \
style="color:#000000;">editMenu</span>);<br />        <span \
style="color:#000000;">editMenu</span>.add(<span \
style="color:#000000;">copyMenuItem</span>);<br />        setJMenuBar(<span \
style="color:#000000;">menuBar</span>);<br /><br />        <span \
style="color:#000000;">JLabel label </span>= <span style="color:#0033b3;">new \
</span>JLabel(<span style="color:#067d17;">"Placeholder Label"</span>);<br />        \
<span style="color:#000000;">label</span>.setBorder(<span style="color:#0033b3;">new \
</span>EmptyBorder(<span style="color:#1750eb;">20</span>,<span \
style="color:#1750eb;">20</span>,<span style="color:#1750eb;">20</span>,<span \
style="color:#1750eb;">20</span>));<br />        getContentPane().add(<span \
style="color:#000000;">label</span>);<br />    }<br />}<br /></pre></div></div> \
<div>Does this answer your question?</div><div \
id="xc80addbc881845b0b5f2ac79f58483f8"><span style="font-family: Calibri, sans-serif; \
font-size: 14.666667px; margin: 0px;"><br /></span></div><div \
id="xc80addbc881845b0b5f2ac79f58483f8"><span style="font-family: Calibri, sans-serif; \
font-size: 14.666667px; margin: 0px;">Regards,</span></div><div \
id="xc80addbc881845b0b5f2ac79f58483f8"><span style="font-family: Calibri, sans-serif; \
font-size: 14.666667px; margin: 0px;">  - Jeremy</span></div><div><br /></div> <div>
<div>------ Original Message ------</div>
<div>From <a href="mailto:mark.yagnatinsky@barclays.com">mark.yagnatinsky@barclays.com</a></div>
 <div>To <a href="mailto:client-libs-dev@openjdk.org">client-libs-dev@openjdk.org</a></div>
 <div>Date 2/7/24, 2:47:55 PM</div>
<div>Subject how to get find out the keyboard shortcut for the paste \
action?</div></div><div><br /></div> <div id="xc63ee9ebb21948e" \
style="word-wrap:break-word"><blockquote \
cite="CH3PR12MB76198C4F8B2300D806EDCCF0F9452@CH3PR12MB7619.namprd12.prod.outlook.com" \
type="cite" class="cite2">

<div class="WordSection1">
<p class="MsoNormal">Some swing components such as text fields have built in support \
for copy and paste.<o:p xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">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.<o:p \
xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">Luckily, there's a tutorial for \
doing this kind of thing, let's see how they do it:<o:p xmlns:o="#unknown"></o:p></p> \
<p class="MsoNormal"><a \
href="https://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html">https://docs.oracle.com/javase/tutorial/uiswing/dnd/listpaste.html</a><o:p \
xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">Uh oh…<o:p \
xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">getKeyStroke("ctrl V")<o:p \
xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">The tutorial simply hardcodes the \
relevant shortcuts.<o:p xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">I suspect \
that this works fine on windows, and will lead Mac users to grumble.<o:p \
xmlns:o="#unknown"></o:p></p> <p class="MsoNormal">Is there a way to ask Java would \
the right keyboard shortcut should be?<o:p xmlns:o="#unknown"></o:p></p> <p \
class="MsoNormal">I suppose one could always create a scratch text field just to look \
through its input map, but seems a bit hacky.<o:p xmlns:o="#unknown"></o:p></p> \
</div>

<p><span lang="EN-US" style="mso-ansi-language: EN-US">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: 
<a href="https://www.cib.barclays/disclosures/web-and-email-disclaimer.html">https://www.cib.barclays/disclosures/web-and-email-disclaimer.html</a>. \
 </span></p>
<p><span lang="EN-US" style="mso-ansi-language: EN-US">For important disclosures, 
please see: 
<a href="https://www.cib.barclays/disclosures/sales-and-trading-disclaimer.html">https://www.cib.barclays/disclosures/sales-and-trading-disclaimer.html</a> \
 regarding marketing commentary from Barclays Sales and/or Trading desks, who are 
active market participants; 
<a href="https://www.cib.barclays/disclosures/barclays-global-markets-disclosures.html \
">https://www.cib.barclays/disclosures/barclays-global-markets-disclosures.html</a>  \
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:  <a href="http://publicresearch.barclays.com">http://publicresearch.barclays.com</a>.<br \
/>__________________________________________________________________________________  \
                <br />If you are incorporated or operating in Australia, read these \
                important 
disclosures: 
<a href="https://www.cib.barclays/disclosures/important-disclosures-asia-pacific.html" \
>https://www.cib.barclays/disclosures/important-disclosures-asia-pacific.html</a>.<br \
> />__________________________________________________________________________________<br \
> />For 
more details about how we use personal information, see our privacy notice: 
<a href="https://www.cib.barclays/disclosures/personal-information-use.html">https://www.cib.barclays/disclosures/personal-information-use.html</a>. \
 <br />__________________________________________________________________________________<br \
/></span></p> </blockquote></div>


</body></html>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic