Skip to content

Commit

Permalink
feat: Change shortcut key hook library for fixing scheme error (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
hizumiaoba authored Mar 15, 2024
1 parent a0c655f commit 63390bf
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 247 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ dependencies {
implementation("org.projectlombok:lombok:${lombokVersion}")
// https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api
implementation("jakarta.annotation:jakarta.annotation-api:${jakartaVersion}")
implementation("com.github.kwhat:jnativehook:2.2.2")
// https://mvnrepository.com/artifact/com.melloware/jintellitype
implementation("com.melloware:jintellitype:1.4.1")


// test dependencies
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.github.hizumiaoba.mctimemachine;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import io.github.hizumiaoba.mctimemachine.MainController.GlobalShortcutKeyListener.Shortcut;
import io.github.hizumiaoba.mctimemachine.api.Config;
import io.github.hizumiaoba.mctimemachine.api.ExceptionPopup;
import io.github.hizumiaoba.mctimemachine.internal.ApplicationConfig;
import io.github.hizumiaoba.mctimemachine.internal.concurrent.ConcurrentThreadFactory;
import io.github.hizumiaoba.mctimemachine.internal.fs.BackupUtils;
import io.github.hizumiaoba.mctimemachine.internal.keyhook.GlobalNativeKeyListenerExecutor;
import io.github.hizumiaoba.mctimemachine.internal.listener.NormalBackupKeyShortcutListener;
import io.github.hizumiaoba.mctimemachine.internal.listener.SpecialBackupKeyShortcutListener;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
Expand All @@ -34,6 +33,7 @@
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -144,27 +144,17 @@ void initialize() {
backupSchedulerExecutors.shutdownNow();
}));

GlobalNativeKeyListenerExecutor globalNativeKeyListenerExecutor = new GlobalNativeKeyListenerExecutor(
new NormalBackupKeyShortcutListener(() -> {
if (this.backupNowWithShortcutChkbox.isSelected()) {
onBackupNowBtnClick();
}
}),
new SpecialBackupKeyShortcutListener(() -> {
if (this.specialBackupNowWithShortcutChkbox.isSelected()) {
onSpecialBackupNowBtnClick();
}
})
);

try {
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(globalNativeKeyListenerExecutor);
} catch (Exception e) {
ExceptionPopup popup = new ExceptionPopup(e, "ショートカットフックを登録できませんでした。",
"MainController#initialize()$lambda");
popup.pop();
}
int MOD_CTRL_SHIFT = JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT;

JIntellitype.getInstance().registerHotKey(
Shortcut.BACKUP_NORMAL.id,
MOD_CTRL_SHIFT,
'B');
JIntellitype.getInstance().registerHotKey(
Shortcut.BACKUP_SPECIAL.id,
MOD_CTRL_SHIFT,
'Z');
JIntellitype.getInstance().addHotKeyListener(new GlobalShortcutKeyListener(this));

savesFolderPathField.setText(mainConfig.load("saves_folder_path"));
backupSavingFolderPathField.setText(mainConfig.load("backup_saving_folder_path"));
Expand Down Expand Up @@ -404,4 +394,63 @@ void onSpecialBackupNowBtnClick() {
private void runConcurrentTask(ExecutorService service, Runnable task) {
service.execute(task);
}

@RequiredArgsConstructor
static class GlobalShortcutKeyListener implements HotkeyListener {

private final MainController mc;
private static final ScheduledExecutorService oneshotExecutor = Executors.newSingleThreadScheduledExecutor(
new ConcurrentThreadFactory("MainController", "Shortcut Key Listener", true));

@Override
public void onHotKey(int identifier) {
switch (Shortcut.fromId(identifier)) {
case BACKUP_NORMAL -> {
if (!mc.backupNowWithShortcutChkbox.isSelected()) {
log.trace("Abort backup shortcut because the checkbox is not selected.");
return;
}
log.debug("Normal backup shortcut is pressed.");
oneshotExecutor.schedule(() -> Platform.runLater(mc::onBackupNowBtnClick), 10,
TimeUnit.SECONDS);
}
case BACKUP_SPECIAL -> {
if (!mc.specialBackupNowWithShortcutChkbox.isSelected()) {
log.trace("Abort special backup shortcut because the checkbox is not selected.");
return;
}
log.debug("Special backup shortcut is pressed.");
oneshotExecutor.schedule(() -> Platform.runLater(mc::onSpecialBackupNowBtnClick), 10,
TimeUnit.SECONDS);
}
case OPEN_LAUNCHER -> {
log.debug("Launcher shortcut is pressed.");
Platform.runLater(mc::onOpenLauncherBtnClick);
}
case UNKNOWN -> log.warn("Unknown shortcut is pressed.");
}
}

public enum Shortcut {
BACKUP_NORMAL(101),
BACKUP_SPECIAL(102),
OPEN_LAUNCHER(901),
UNKNOWN(-1);

private final int id;

Shortcut(int id) {
this.id = id;
}

public static Shortcut fromId(int id) {
for (Shortcut s : values()) {
if (s.id == id) {
return s;
}
}
return UNKNOWN;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.hizumiaoba.mctimemachine;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import io.github.hizumiaoba.mctimemachine.api.ExceptionPopup;
import com.melloware.jintellitype.JIntellitype;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
Expand All @@ -17,22 +15,17 @@ public class MineCraftTimeMachineApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(MineCraftTimeMachineApplication.class.getResource("main.fxml"));
stage.setOnCloseRequest(this::onClose);
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("MCTM - BackUp & Restore your precious world!");
stage.setScene(scene);
stage.setOnCloseRequest(this::onClose);
stage.show();
}

private void onClose(WindowEvent e) {
log.trace("Release key hooks via close event: {}", e);
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException ex) {
ExceptionPopup popup = new ExceptionPopup(ex, "キーボードショートカットの解除に失敗しました。",
"MineCraftTimeMachineApplication#onClose");
popup.pop();
}
log.info("Releasing Global shortcut key hooks...");
log.info("Bye!: {}", e);
JIntellitype.getInstance().cleanUp();
}

public static void main(String[] args) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
requires com.google.common;
requires lombok;
requires annotations;
requires com.github.kwhat.jnativehook;
requires jintellitype;

opens io.github.hizumiaoba.mctimemachine to javafx.fxml;
exports io.github.hizumiaoba.mctimemachine;
Expand Down

0 comments on commit 63390bf

Please sign in to comment.