Skip to content

Commit

Permalink
屏蔽其他软件全局快捷键
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyListen committed Feb 3, 2020
1 parent 9238f0a commit 8e8c583
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/luooqi/ocr/MainFm.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.luooqi.ocr.utils.CommUtils;
import com.luooqi.ocr.utils.GlobalKeyListener;
import com.luooqi.ocr.utils.OcrUtils;
import com.luooqi.ocr.utils.VoidDispatchService;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
Expand All @@ -31,8 +32,12 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -227,6 +232,7 @@ private static void initKeyHook(){
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.WARNING);
logger.setUseParentHandlers(false);
GlobalScreen.setEventDispatcher(new VoidDispatchService());
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/luooqi/ocr/utils/GlobalKeyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.hutool.log.StaticLog;
import com.luooqi.ocr.MainFm;
import com.luooqi.ocr.snap.ScreenCapture;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeInputEvent;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
Expand Down Expand Up @@ -30,8 +31,11 @@ else if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE){
}

@Override
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {

public void nativeKeyReleased(NativeKeyEvent e) {
// if (e.getKeyCode() == NativeKeyEvent.VC_F4){
// preventEvent(e);
// }
// GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
}

private void preventEvent(NativeKeyEvent e){
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/luooqi/ocr/utils/VoidDispatchService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.luooqi.ocr.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;

public class VoidDispatchService extends AbstractExecutorService {
private boolean running = false;

public VoidDispatchService() {
running = true;
}

public void shutdown() {
running = false;
}

public List<Runnable> shutdownNow() {
running = false;
return new ArrayList<Runnable>(0);
}

public boolean isShutdown() {
return !running;
}

public boolean isTerminated() {
return !running;
}

public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return true;
}

public void execute(Runnable r) {
r.run();
}
}

0 comments on commit 8e8c583

Please sign in to comment.