# Use NamedKeyStrokes in a Custom Code Class

**URL:** <https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882>\
**Category:** Technical Support & Bugs\
**Created:** [October 26, 2021, 8:22pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882 "2021-10-26T20:22:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drimmer](https://forum.vassalengine.org/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@drimmer](https://forum.vassalengine.org/u/drimmer)\
**Post date:** [October 26, 2021, 8:22pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/1 "2021-10-26T20:22:04Z")

</div>

I want to implement a custom code class that uses NamedKeyStrokes. I am trying to use an existing class in my module (VASL) as a model but I cannot understand how it works.

The buildFile entry for the model class (PieceLinker.java) is as follows:

\<VASL.build.module.map.PieceLinker Color=“255,0,0” LinkKey=“76,650” Name=“Piece Linker” ThreadWidth=“2” UnlinkKey=“85,650”/\>

The class itself, contains the following:

public class PieceLinker extends AbstractConfigurable implements KeyListener, CommandEncoder, GameComponent, Drawable {

```
[various declarations omitteed]

private NamedKeyStroke linkKey = new NamedKeyStroke("71dd9846"); // CTL+ALT+L
private NamedKeyStroke unlinkKey = new NamedKeyStroke("b385611"); // CTL+ALT+U 

```

The net effect of this (and the rest of the class obviously) is that when CTRL+ALT+L is pressed and two counters are selected, those two counters are linked by a line drawn between them. Pressing CTRL+ALT+U removes the line.

What I cannot understand is how the various inputs are determined and how they link to the desired keystrokes. More specifically:

1. in the buildFile, LinkKey=“76,650”, what do 76 and 650 represent and how would I know what to use if I want to used CTRL+ALT+S instead of CTRL+ALT+L? Or say SHIFT+L?

2. in the class code, new NamedKeyStroke(“71dd9846”), what does the 71dd9846 represent and how would I know what to use for a different keystroke?

I can see how the NamedKeyStrokes are used by the class to actually manage the linking and unlinking. I just can’t figure out how the above lines work to create something that responds to the specific CTRL+ALT+L and CTRL+ALT+U keystrokes.

TIA.

Doug

---

<div class="post-metadata">

**Author:** ![uckelman](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/uckelman/32/10020_2.png) [@uckelman](https://forum.vassalengine.org/u/uckelman)\
**Post date:** [October 26, 2021, 9:01pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/2 "2021-10-26T21:01:17Z")

</div>

> [@drimmer](#):
>
> in the buildFile, LinkKey=“76,650”, what do 76 and 650 represent and how would I know what to use if I want to used CTRL+ALT+S instead of CTRL+ALT+L? Or say SHIFT+L?

Those are the key code and modifiers, respectively. The key codes are defined in `java.awt.event.KeyEvent` and modifiers are defined in `java.awt.event.InputEvent`.

---

<div class="post-metadata">

**Author:** ![uckelman](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/uckelman/32/10020_2.png) [@uckelman](https://forum.vassalengine.org/u/uckelman)\
**Post date:** [October 26, 2021, 9:02pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/3 "2021-10-26T21:02:55Z")

</div>

> [@drimmer](#):
>
> in the class code, new NamedKeyStroke(“71dd9846”), what does the 71dd9846 represent and how would I know what to use for a different keystroke?

The string argument is just the name of the `NamedKeyStroke`. You can make that whatever you want for identifying it.

---

<div class="post-metadata">

**Author:** ![Cattlesquat](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/cattlesquat/32/241_2.png) [@Cattlesquat](https://forum.vassalengine.org/u/Cattlesquat)\
**Post date:** [October 26, 2021, 9:08pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/4 "2021-10-26T21:08:14Z")

</div>

You can do this for constructing a NamedKeyStroke for a specific key sequence:

> NamedKeyStroke.of(KeyEvent.VK\_S, InputEvent.ALT\_DOWN\_MASK)

If you wanted multiple modifier keys you’d “or them together” in the second parameter.

Brian

---

<div class="post-metadata">

**Author:** ![drimmer](https://forum.vassalengine.org/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@drimmer](https://forum.vassalengine.org/u/drimmer)\
**Post date:** [October 30, 2021, 4:08pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/5 "2021-10-30T16:08:30Z")

</div>

Thanks to you both. I will give this a go.

---

<div class="post-metadata">

**Author:** ![drimmer](https://forum.vassalengine.org/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@drimmer](https://forum.vassalengine.org/u/drimmer)\
**Post date:** [March 6, 2022, 3:46pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/6 "2022-03-06T15:46:53Z")

</div>

I was able to get this to work using Brian’s reference. Thanks for the continued help.

---

<div class="post-metadata">

**Author:** ![Brent\_Easton](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/brent_easton/32/10_2.png) [@Brent\_Easton](https://forum.vassalengine.org/u/Brent_Easton)\
**Post date:** [March 10, 2022, 9:55pm UTC](https://forum.vassalengine.org/t/use-namedkeystrokes-in-a-custom-code-class/72882/7 "2022-03-10T21:55:20Z")

</div>

Just some further clarification,

```auto
private NamedKeyStroke linkKey = new NamedKeyStroke("71dd9846"); // CTL+ALT+L

```

That comment at the end is meaningless, it bears no relationship at all to what that piece of code is doing. A NamedKeyStroke can be EITHER a specified real (can be typed)l keystroke OR a virtual named Keystroke. It can’t be both.

So the above fragment is creating a virtual keystroke with the name “71dd9846”. Internally, in Vassal, this will be allocated an invisible Keystroke from a pool that is used by Vassal for all references to the Keystroke named “71dd9846”. This can never respond to CTRL+ALT+L or any other other type-able keystroke.

The fragment alluded to by Brain

```auto
private NamedKeyStroke linkKey = new NamedKeyStrokeKeyEvent.VK_L, InputEvent.ALT_DOWN_MASK + InputEvent.CTRL_DOWN_MASK); // CTL+ALT+L

```

Creates a Keystroke that responds to Ctrl-Alt-L being typed.
