Skip to content

Commit

Permalink
#94, mess of incomplete/erroneous additions that attempt to handle us…
Browse files Browse the repository at this point in the history
…er keypresses and translate between JavaFX KeyEvents and JDK/(Swing?) KeyEvents
  • Loading branch information
RVRX committed Aug 11, 2021
1 parent 7594f82 commit 0c3816c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GlobalHotKeys {
* with updated values will lead to errors
*/
public static void updateHotKeys() {
System.out.println("App.updateHotKeys");
System.out.println("GlobalHotKeys.updateHotKeys");
Provider provider = Provider.getCurrentProvider(false);
System.out.println("Provider has been reset");

Expand All @@ -32,13 +32,18 @@ public static void updateHotKeys() {
//if switch hotkey is set
String timerSwitchHotKeyString = prefs.get(BindHotkeyPane.HOTKEY_TIMER_SWITCH, null);
if (timerSwitchHotKeyString != null && !timerSwitchHotKeyString.equals("")) {
System.out.println("Registering new global listener: " + timerSwitchHotKeyString);

//add listener with that keybinding
// KeyCode.getKeyCode(timerSwitchHotKeyString);
provider.register(KeyStroke.getKeyStroke(timerSwitchHotKeyString), getHotKeySwitchListener());
}

//if play/pause hotkey is set
String timerPlayPauseHotKeyString = prefs.get(BindHotkeyPane.HOTKEY_PLAY_PAUSE, null);
if (timerPlayPauseHotKeyString != null && !timerPlayPauseHotKeyString.equals("")) {
System.out.println("Registering new global listener: " + timerPlayPauseHotKeyString);

//add listener with that keybinding
provider.register(KeyStroke.getKeyStroke(timerPlayPauseHotKeyString), getHotKeyPlayPauseListener());
}
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/goistreamtoolredux/controller/BindHotkeyPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import com.jfoenix.controls.JFXToggleButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;

import javax.swing.*;
import java.net.URL;
import java.util.LinkedList;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

Expand Down Expand Up @@ -40,6 +44,10 @@ public class BindHotkeyPane {
public static final String HOTKEY_PLAY_PAUSE = "hotkey_play_pause";
public static final String HOTKEY_TIMER_SWITCH = "hotkey_timer_switch";

private LinkedList<KeyEvent> playPauseHotKey = new LinkedList<>();
private LinkedList<String> foo = new LinkedList<String>();
private String hotkeyString = "";


@FXML
void enableHotkeyTogglerAction(ActionEvent event) {
Expand All @@ -56,15 +64,96 @@ void enableHotkeyTogglerAction(ActionEvent event) {
Duration.millis(2500), null));
}

/**
* todo
*
* Called by FXML/JavaFX thread when 'enter' key is pressed
* while the {@link #timerPlayPauseField} is focused.
* @param event
*/
@FXML
void timerPlayPauseFieldAction(ActionEvent event) {
//update prefs
getKeyStrokeAndSaveToPrefs(timerPlayPauseField, HOTKEY_PLAY_PAUSE);
}

/**
* todo
*
* Called by FXML/JavaFX thread when 'enter' key is pressed
* while the {@link #timerSwitchField} is focused.
* @param event
*/
@FXML
void timerSwitchFieldAction(ActionEvent event) {
System.out.println("putting '" + hotkeyString + "' into timer switch preference");
prefs.put(HOTKEY_TIMER_SWITCH, hotkeyString);
getKeyStrokeAndSaveToPrefs(timerSwitchField, HOTKEY_TIMER_SWITCH);
//todo prompt restart!
}

/**
* todo
* Adds the pressed key - if not 'enter' key - into a linkedList of keyEvents.
*
* Called by FXML/JavaFX thread when any key is pressed
* while the {@link #timerPlayPauseField} is focused.
* @param event
*/
@FXML
void playPauseKeyPressed(KeyEvent event) {
// if (prefs.getBoolean(IS_HOTKEYS_ENABLED, false)) {
// System.out.println(event);
// foo.add(event.getCode());
// playPauseHotKey.add(event);
// }
}

/**
* todo
* Adds the pressed key - if not 'enter' key - into a linkedList of keyEvents.
*
* Called by FXML/JavaFX thread when any key is pressed
* while the {@link #timerPlayPauseField} is focused.
* @param event
*/
@FXML
void switchKeyPressed(KeyEvent event) {
//todo KeyEvent to KeyStroke
KeyCode pressedKeyCode = event.getCode();
KeyStroke.getAWTKeyStrokeForEvent(event);
KeyCodeCombination.keyCombination(event.getCode().toString());

if (!pressedKeyCode.equals(KeyCode.ENTER)) { //if not 'enter' key
if (!pressedKeyCode.equals(KeyCode.BACK_SPACE)) {
System.out.println("NOT ENTER");
System.out.println(event);
// event

//add to list of keys
// String currentHotkeyString = prefs.get(HOTKEY_TIMER_SWITCH, "");
// String newHotkeyString = currentHotkeyString + " " + event.getCode().toString();
// prefs.put(HOTKEY_TIMER_SWITCH, newHotkeyString);
// hotkeyString += " " + event.getCode()
if (event.getCode().equals(KeyCode.SHIFT)) {
hotkeyString += " " + "SHIFT";
hotkeyString += " " + event.getCode().toString();
} else if (event.getCode().equals(KeyCode.ALPHANUMERIC)) {

}
hotkeyString += " " + event.getCode().toString();
// foo.add(event.getCode().toString());

// foo.add(event.getCode());

//add to field
timerSwitchField.setText(timerSwitchField.getText() + " " + event.getCode().toString());

} else { //key pressed is backspace
hotkeyString = "";
timerSwitchField.setText("");
}
} else System.err.println(event);
}

/**
Expand Down Expand Up @@ -112,8 +201,14 @@ void initialize() {
timerPlayPauseField.setDisable(!prefs.getBoolean(IS_HOTKEYS_ENABLED, false));
timerSwitchField.setDisable(!prefs.getBoolean(IS_HOTKEYS_ENABLED, false));

//dont allow direct typing into field
timerSwitchField.setEditable(false);
timerPlayPauseField.setEditable(false);

//init field values
timerPlayPauseField.setText(prefs.get(HOTKEY_PLAY_PAUSE, null));
timerSwitchField.setText(prefs.get(HOTKEY_TIMER_SWITCH, null));

//init key thingy todo
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="goistreamtoolredux.controller.BindHotkeyPane">
<children>
<JFXTextField fx:id="timerPlayPauseField" layoutX="39.0" layoutY="96.0" onAction="#timerPlayPauseFieldAction" promptText="Timer Play/Pause" />
<JFXTextField fx:id="timerPlayPauseField" layoutX="39.0" layoutY="96.0" onAction="#timerPlayPauseFieldAction" onKeyPressed="#playPauseKeyPressed" promptText="Timer Play/Pause" />
<JFXToggleButton fx:id="enableHotkeyToggle" layoutX="14.0" layoutY="14.0" onAction="#enableHotkeyTogglerAction" text="Enable Global Hotkeys" />
<JFXTextField fx:id="timerSwitchField" layoutX="39.0" layoutY="143.0" onAction="#timerSwitchFieldAction" promptText="Timer Switch" />
<JFXTextField fx:id="timerSwitchField" layoutX="39.0" layoutY="143.0" onAction="#timerSwitchFieldAction" onKeyPressed="#switchKeyPressed" promptText="Timer Switch" />
</children>
</AnchorPane>

0 comments on commit 0c3816c

Please sign in to comment.