Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37343 > unrolled thread
| Started by | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| First post | 2017-08-25 10:59 -0700 |
| Last post | 2017-08-29 07:41 +0200 |
| Articles | 6 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Can you share an action? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-25 10:59 -0700
Re: Can you share an action? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-25 11:08 -0700
Re: Can you share an action? Joerg Meier <joergmmeier@arcor.de> - 2017-08-26 13:30 +0200
Re: Can you share an action? Joerg Meier <joergmmeier@arcor.de> - 2017-08-26 13:34 +0200
Re: Can you share an action? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-28 04:52 -0700
Re: Can you share an action? Joerg Meier <joergmmeier@arcor.de> - 2017-08-29 07:41 +0200
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2017-08-25 10:59 -0700 |
| Subject | Can you share an action? |
| Message-ID | <4045d8a5-752d-40ac-aaae-9684cd594c77@googlegroups.com> |
InputMap focusedMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "vkDown");
getActionMap().put("vkDown", new AbstractAction() {
@Override
public void actionPerformed(final ActionEvent e) {
myCustomAction();
}
});
Do you have to do all this code for every key, replacing each name ("vkDown"), or is there an easy way for multiple keys to call one custom process which has to know which key was pressed?
[toc] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2017-08-25 11:08 -0700 |
| Message-ID | <70632187-656b-4f54-ba6f-8e04c9ef553e@googlegroups.com> |
| In reply to | #37343 |
On Friday, August 25, 2017 at 1:59:43 PM UTC-4, Eric Douglas wrote:
>
> Do you have to do all this code for every key, replacing each name ("vkDown"), or is there an easy way for multiple keys to call one custom process which has to know which key was pressed?
Best I can see is a loop through a defined list like this guy.
https://stackoverflow.com/a/16123589/6927352
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2017-08-26 13:30 +0200 |
| Message-ID | <gvdzyqowg2u8$.10w9t16r9ubtd.dlg@40tude.net> |
| In reply to | #37343 |
On Fri, 25 Aug 2017 10:59:12 -0700 (PDT), Eric Douglas wrote:
> InputMap focusedMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
> focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "vkDown");
> getActionMap().put("vkDown", new AbstractAction() {
> @Override
> public void actionPerformed(final ActionEvent e) {
> myCustomAction();
> }
> });
> Do you have to do all this code for every key, replacing each name ("vkDown"), or is there an easy way for multiple keys to call one custom process which has to know which key was pressed?
You seem to have misunderstood how focusedMap works. The provided String is
merely a key to look up the Action you want later resolved through and
called from the actionMap. So you can just do this:
getActionMap().put("lollipops", new AbstractAction() {
@Override
public void actionPerformed(final ActionEvent e) {
myCustomAction();
}
});
focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "lollipops");
focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "lollipops");
focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "lollipops");
focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "lollipops");
// ...
Furthermore, since it's 2017, and Java 1.7 has been EOLd for a long time
now, you could improve on this by doing:
getActionMap().put("lollipops", new AbstractAction() {
@Override
public void actionPerformed(final ActionEvent e) {
myCustomAction();
}
});
IntStream.of(KeyEvent.VK_DOWN, KeyEvent.VK_UP, KeyEvent.VK_LEFT,
KeyEvent.VK_RIGHT /* ... */)
.forEach(ke -> focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
"lollipops"));
Liebe Gruesse,
Joerg
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2017-08-26 13:34 +0200 |
| Message-ID | <1pe39fv6if6k5$.wrjhul8b3rmh.dlg@40tude.net> |
| In reply to | #37345 |
Sorry, slight c/p error, that should have been: IntStream.of(KeyEvent.VK_DOWN, KeyEvent.VK_UP, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT /* ... */) .forEach(ke -> focusedMap.put(KeyStroke.getKeyStroke(ke, 0), "lollipops")); Though I would recommend a static import of KeyEvent.* for a more readable: IntStream.of(VK_DOWN, VK_UP, VK_LEFT, VK_RIGHT /* ... */) .forEach(ke -> focusedMap.put(KeyStroke.getKeyStroke(ke, 0), "lollipops")); Liebe Gruesse, Joerg
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2017-08-28 04:52 -0700 |
| Message-ID | <b35929c0-9ab7-4172-a82a-48416e482627@googlegroups.com> |
| In reply to | #37345 |
On Saturday, August 26, 2017 at 7:30:42 AM UTC-4, Joerg Meier wrote: > > IntStream.of(KeyEvent.VK_DOWN, KeyEvent.VK_UP, KeyEvent.VK_LEFT, > KeyEvent.VK_RIGHT /* ... */) > .forEach(ke -> focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), > "lollipops")); > > Liebe Gruesse, > Joerg A little unclear, does that answer the question? I'm trying to get multiple keystrokes to perform the same action, and for that custom action method to know which key was pressed.
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2017-08-29 07:41 +0200 |
| Message-ID | <2kgbacwe62cm$.shzjk35k5h1p$.dlg@40tude.net> |
| In reply to | #37347 |
On Mon, 28 Aug 2017 04:52:14 -0700 (PDT), Eric Douglas wrote: > On Saturday, August 26, 2017 at 7:30:42 AM UTC-4, Joerg Meier wrote: >> IntStream.of(KeyEvent.VK_DOWN, KeyEvent.VK_UP, KeyEvent.VK_LEFT, >> KeyEvent.VK_RIGHT /* ... */) >> .forEach(ke -> focusedMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), >> "lollipops")); > A little unclear, does that answer the question? I'm trying to get multiple keystrokes to perform the same action, and for that custom action method to know which key was pressed. I missed that detail of your question, but I would expect that information to be available from the event passed to the method. Liebe Gruesse, Joerg
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web