Use NamedKeyStrokes in a Custom Code Class

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

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.

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

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

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

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

1 Like

Just some further clarification,

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

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.