Skip to content

Commit

Permalink
Use old hotkey system (except for linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjabrain1 committed Mar 27, 2024
1 parent 920bd5a commit 9bda62a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.sun.jna.Platform;
import ninjabrainbot.gui.buttons.FlatButton;
import ninjabrainbot.gui.components.labels.ThemedLabel;
import ninjabrainbot.gui.components.panels.ThemedPanel;
Expand Down Expand Up @@ -67,15 +70,14 @@ private void clicked() {
if (!editing) {
editing = true;
button.setText("...");
KeyboardListener.instance.setConsumer((code, modifier) -> {
if (code == -1) {
KeyboardListener.instance.setConsumer(nativeKeyEvent -> {
if (nativeKeyEvent == null) {
// Canceled, dont change anything
} else if (code == NativeKeyEvent.VC_ESCAPE) {
} else if (nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
preference.setCode(-1);
preference.setModifier(-1);
} else {
preference.setCode(code);
preference.setModifier(modifier);
preference.setHotkey(nativeKeyEvent);
}
String s = getKeyText();
SwingUtilities.invokeLater(() -> {
Expand All @@ -89,7 +91,7 @@ private void clicked() {
private String getKeyText() {
if (preference.getCode() == -1)
return I18n.get("settings.not_in_use");
String k = NativeKeyEvent.getKeyText(preference.getCode());
String k = Platform.isLinux() ? NativeKeyEvent.getKeyText(preference.getCode()) : KeyEvent.getKeyText(preference.getCode());
if (k.startsWith("Unknown")) {
k = k.substring(17);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ninjabrainbot/io/KeyConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import java.awt.event.KeyEvent;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.SwingKeyAdapter;

public class KeyConverter {
public class KeyConverter extends SwingKeyAdapter {

public int convertNativeKeyCodeToKeyCode(int nativeKeyCode){
NativeKeyEvent nativeKeyEvent = new NativeKeyEvent(0, 0, 0, nativeKeyCode, (char)0);
return getJavaKeyEvent(nativeKeyEvent).getKeyCode();
}

public static int convertKeyCodeToNativeKeyCode(int keyCode) {
switch (keyCode) {
Expand Down
42 changes: 21 additions & 21 deletions src/main/java/ninjabrainbot/io/KeyboardListener.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ninjabrainbot.io;

import java.awt.event.KeyEvent;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
Expand All @@ -17,8 +17,8 @@ public class KeyboardListener implements NativeKeyListener {

private final BooleanPreference useAltClipboardReader;

BiConsumer<Integer, Integer> consumer;
final ClipboardReader clr;
Consumer<NativeKeyEvent> consumer;
final ClipboardReader clipboardReader;
boolean f3Held = false;

public static void preInit() {
Expand All @@ -44,54 +44,55 @@ public void run() {
});
}

public static void init(ClipboardReader clr, BooleanPreference useAltClipboardReader) {
public static void init(ClipboardReader clipboardReader, BooleanPreference useAltClipboardReader) {
if (registered) {
instance = new KeyboardListener(clr, useAltClipboardReader);
instance = new KeyboardListener(clipboardReader, useAltClipboardReader);
GlobalScreen.addNativeKeyListener(instance);
}
}

KeyboardListener(ClipboardReader clr, BooleanPreference useAltClipboardReader) {
KeyboardListener(ClipboardReader clipboardReader, BooleanPreference useAltClipboardReader) {
super();
this.clr = clr;
this.clipboardReader = clipboardReader;
this.useAltClipboardReader = useAltClipboardReader;
}

public synchronized void setConsumer(BiConsumer<Integer, Integer> consumer) {
public synchronized void setConsumer(Consumer<NativeKeyEvent> consumer) {
if (this.consumer != null) {
this.consumer.accept(-1, -1);
this.consumer.accept(null);
}
this.consumer = consumer;
}

public synchronized void cancelConsumer() {
if (this.consumer != null) {
this.consumer.accept(-1, -1);
this.consumer.accept(null);
this.consumer = null;
}
}

@Override
public void nativeKeyPressed(NativeKeyEvent e) {
int c = e.getKeyCode();
if (c == NativeKeyEvent.VC_SHIFT || c == NativeKeyEvent.VC_CONTROL || c == NativeKeyEvent.VC_ALT)
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
int keyCode = nativeKeyEvent.getKeyCode();
if (keyCode == NativeKeyEvent.VC_SHIFT || keyCode == NativeKeyEvent.VC_CONTROL || keyCode == NativeKeyEvent.VC_ALT)
return;

if (consumer != null) {
consumer.accept(c, e.getModifiers());
consumer.accept(nativeKeyEvent);
consumer = null;
return;
}
for (HotkeyPreference h : HotkeyPreference.hotkeys) {
if (h.getCode() == c && (h.getModifier() & e.getModifiers()) == h.getModifier()) {
h.execute();
for (HotkeyPreference hotkeyPreference : HotkeyPreference.hotkeys) {
if (hotkeyPreference.isKeyEventMatching(nativeKeyEvent)) {
hotkeyPreference.execute();
}
}
// Alt clipboard reader
if (useAltClipboardReader.get()) {
if (e.getRawCode() == KeyEvent.VK_F3) {
if (nativeKeyEvent.getRawCode() == KeyEvent.VK_F3) {
f3Held = true;
} else if (f3Held && e.getRawCode() == KeyEvent.VK_C) {
clr.forceRead();
} else if (f3Held && nativeKeyEvent.getRawCode() == KeyEvent.VK_C) {
clipboardReader.forceRead();
}
}
}
Expand All @@ -102,5 +103,4 @@ public void nativeKeyReleased(NativeKeyEvent e) {
f3Held = false;
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/ninjabrainbot/io/preferences/HotkeyPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.sun.jna.Platform;
import ninjabrainbot.event.ISubscribable;
import ninjabrainbot.event.ObservableProperty;

Expand Down Expand Up @@ -32,6 +34,11 @@ public int getModifier() {
return modifier.get();
}

public boolean isKeyEventMatching(NativeKeyEvent nativeKeyEvent){
int code = getPlatformSpecificKeyCode(nativeKeyEvent);
return getCode() == code && (getModifier() & nativeKeyEvent.getModifiers()) == getModifier();
}

public synchronized void setCode(int value) {
code.set(value);
}
Expand All @@ -40,6 +47,11 @@ public synchronized void setModifier(int value) {
modifier.set(value);
}

public synchronized void setHotkey(NativeKeyEvent nativeKeyEvent){
setCode(getPlatformSpecificKeyCode(nativeKeyEvent));
setModifier(nativeKeyEvent.getModifiers());
}

public final void execute() {
whenTriggered.notifySubscribers(this);
}
Expand All @@ -48,4 +60,7 @@ public ISubscribable<HotkeyPreference> whenTriggered() {
return whenTriggered;
}

private static int getPlatformSpecificKeyCode(NativeKeyEvent nativeKeyEvent){
return Platform.isLinux() ? nativeKeyEvent.getKeyCode() : nativeKeyEvent.getRawCode();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ninjabrainbot.io.preferences;

import java.awt.event.KeyEvent;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.SwingKeyAdapter;
import com.sun.jna.Platform;
import ninjabrainbot.io.KeyConverter;
import ninjabrainbot.io.preferences.enums.AllAdvancementsToggleType;
import ninjabrainbot.io.preferences.enums.MainViewType;
Expand Down Expand Up @@ -130,10 +134,12 @@ public NinjabrainBotPreferences(IPreferenceSource source) {
allAdvancementsToggleType = new MultipleChoicePreference<>("aa_toggle_type", AllAdvancementsToggleType.Automatic, new int[] { 0, 1 }, new AllAdvancementsToggleType[] { AllAdvancementsToggleType.Automatic, AllAdvancementsToggleType.Hotkey }, source);

// Upgrade if necessary
if (settingsVersion.get() == 0) {
if (settingsVersion.get() == 0)
upgradeSettings_From_0_To_1();
}
Assert.isEqual(settingsVersion.get(), 1);
if (settingsVersion.get() == 1)
upgradeSettings_From_1_To_2();

Assert.isTrue(settingsVersion.get() >= 2); // Do >= to allow users to downgrade to an earlier version, in case newer version has issues
}

private void upgradeSettings_From_0_To_1(){
Expand All @@ -146,4 +152,17 @@ private void upgradeSettings_From_0_To_1(){
settingsVersion.set(1);
}

private void upgradeSettings_From_1_To_2(){
if (!Platform.isLinux()){
KeyConverter keyConverter = new KeyConverter();
for (HotkeyPreference hotkeyPreference : HotkeyPreference.hotkeys){
if (hotkeyPreference.getCode() == -1)
continue;
int keyCode = keyConverter.convertNativeKeyCodeToKeyCode(hotkeyPreference.getCode());
hotkeyPreference.setCode(keyCode);
}
}
settingsVersion.set(2);
}

}

0 comments on commit 9bda62a

Please sign in to comment.