diff --git a/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java b/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java index 04124d1861..50b0535b65 100644 --- a/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java +++ b/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java @@ -514,11 +514,15 @@ public class ConfigManager { return Instant.parse(str); } - if (type == Keybind.class) + if (type == Keybind.class || type == ModifierlessKeybind.class) { String[] splitStr = str.split(":"); int code = Integer.parseInt(splitStr[0]); int mods = Integer.parseInt(splitStr[1]); + if (type == ModifierlessKeybind.class) + { + return new ModifierlessKeybind(code, mods); + } return new Keybind(code, mods); } return str; diff --git a/runelite-client/src/main/java/net/runelite/client/config/Keybind.java b/runelite-client/src/main/java/net/runelite/client/config/Keybind.java index 23696022d9..b0fb4dffcf 100644 --- a/runelite-client/src/main/java/net/runelite/client/config/Keybind.java +++ b/runelite-client/src/main/java/net/runelite/client/config/Keybind.java @@ -60,7 +60,7 @@ public class Keybind private final int keyCode; private final int modifiers; - public Keybind(int keyCode, int modifiers) + protected Keybind(int keyCode, int modifiers, boolean ignoreModifiers) { modifiers &= KEYBOARD_MODIFIER_MASK; @@ -73,10 +73,20 @@ public class Keybind keyCode = KeyEvent.VK_UNDEFINED; } + if (ignoreModifiers && keyCode != KeyEvent.VK_UNDEFINED) + { + modifiers = 0; + } + this.keyCode = keyCode; this.modifiers = modifiers; } + public Keybind(int keyCode, int modifiers) + { + this(keyCode, modifiers, false); + } + /** * Constructs a keybind with that matches the passed KeyEvent */ @@ -94,6 +104,11 @@ public class Keybind * released */ public boolean matches(KeyEvent e) + { + return matches(e, false); + } + + protected boolean matches(KeyEvent e, boolean ignoreModifiers) { if (NOT_SET.equals(this)) { @@ -115,6 +130,11 @@ public class Keybind return this.keyCode == keyCode; } + if (ignoreModifiers && keyCode != KeyEvent.VK_UNDEFINED) + { + return this.keyCode == keyCode; + } + return this.keyCode == keyCode && this.modifiers == modifiers; } diff --git a/runelite-client/src/main/java/net/runelite/client/config/ModifierlessKeybind.java b/runelite-client/src/main/java/net/runelite/client/config/ModifierlessKeybind.java new file mode 100644 index 0000000000..6e7a53a43e --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/config/ModifierlessKeybind.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 Abex + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.config; + +import java.awt.event.KeyEvent; + +public class ModifierlessKeybind extends Keybind +{ + public ModifierlessKeybind(int keyCode, int modifiers) + { + super(keyCode, modifiers, true); + } + + /** + * Constructs a keybind with that matches the passed KeyEvent + */ + public ModifierlessKeybind(KeyEvent e) + { + this(e.getExtendedKeyCode(), e.getModifiersEx()); + + assert matches(e); + } + + /** + * If the KeyEvent is from a KeyPressed event this returns if the + * Event is this hotkey being pressed. If the KeyEvent is a + * KeyReleased event this returns if the event is this hotkey being + * released + */ + public boolean matches(KeyEvent e) + { + return matches(e, true); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java index c98a03f1b1..05c2cdecf8 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java @@ -78,6 +78,7 @@ import net.runelite.client.config.ConfigItem; import net.runelite.client.config.ConfigItemDescriptor; import net.runelite.client.config.ConfigManager; import net.runelite.client.config.Keybind; +import net.runelite.client.config.ModifierlessKeybind; import net.runelite.client.config.Range; import net.runelite.client.config.RuneLiteConfig; import net.runelite.client.plugins.Plugin; @@ -508,11 +509,13 @@ public class ConfigPanel extends PluginPanel item.add(box, BorderLayout.EAST); } - if (cid.getType() == Keybind.class) + if (cid.getType() == Keybind.class || cid.getType() == ModifierlessKeybind.class) { - Keybind startingValue = configManager.getConfiguration(cd.getGroup().value(), cid.getItem().keyName(), Keybind.class); + Keybind startingValue = configManager.getConfiguration(cd.getGroup().value(), + cid.getItem().keyName(), + (Class) cid.getType()); - HotkeyButton button = new HotkeyButton(startingValue); + HotkeyButton button = new HotkeyButton(startingValue, cid.getType() == ModifierlessKeybind.class); button.addFocusListener(new FocusAdapter() { diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/config/HotkeyButton.java b/runelite-client/src/main/java/net/runelite/client/plugins/config/HotkeyButton.java index 4d2fd0dc84..585ade5ea6 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/config/HotkeyButton.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/config/HotkeyButton.java @@ -29,13 +29,14 @@ import java.awt.event.KeyEvent; import javax.swing.JButton; import lombok.Getter; import net.runelite.client.config.Keybind; +import net.runelite.client.config.ModifierlessKeybind; public class HotkeyButton extends JButton { @Getter private Keybind value; - public HotkeyButton(Keybind value) + public HotkeyButton(Keybind value, boolean modifierless) { setValue(value); addActionListener(e -> @@ -47,7 +48,14 @@ public class HotkeyButton extends JButton @Override public void keyPressed(KeyEvent e) { - setValue(new Keybind(e)); + if (modifierless) + { + setValue(new ModifierlessKeybind(e)); + } + else + { + setValue(new Keybind(e)); + } } }); } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/wasdcamera/WASDCameraConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/wasdcamera/WASDCameraConfig.java index 16e7c2c25a..e84b31a17d 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/wasdcamera/WASDCameraConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/wasdcamera/WASDCameraConfig.java @@ -28,7 +28,7 @@ import java.awt.event.KeyEvent; import net.runelite.client.config.Config; import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigItem; -import net.runelite.client.config.Keybind; +import net.runelite.client.config.ModifierlessKeybind; @ConfigGroup("wasdcamera") public interface WASDCameraConfig extends Config @@ -39,9 +39,9 @@ public interface WASDCameraConfig extends Config name = "Up key", description = "The key which will replace up." ) - default Keybind up() + default ModifierlessKeybind up() { - return new Keybind(KeyEvent.VK_W, 0); + return new ModifierlessKeybind(KeyEvent.VK_W, 0); } @ConfigItem( @@ -50,9 +50,9 @@ public interface WASDCameraConfig extends Config name = "Down key", description = "The key which will replace down." ) - default Keybind down() + default ModifierlessKeybind down() { - return new Keybind(KeyEvent.VK_S, 0); + return new ModifierlessKeybind(KeyEvent.VK_S, 0); } @ConfigItem( @@ -61,9 +61,9 @@ public interface WASDCameraConfig extends Config name = "Left key", description = "The key which will replace left." ) - default Keybind left() + default ModifierlessKeybind left() { - return new Keybind(KeyEvent.VK_A, 0); + return new ModifierlessKeybind(KeyEvent.VK_A, 0); } @ConfigItem( @@ -72,8 +72,8 @@ public interface WASDCameraConfig extends Config name = "Right key", description = "The key which will replace right." ) - default Keybind right() + default ModifierlessKeybind right() { - return new Keybind(KeyEvent.VK_D, 0); + return new ModifierlessKeybind(KeyEvent.VK_D, 0); } }