Ich erstelle ein einfaches Tool zum Abrufen der aktuellen Mauskoordinaten, um mir bei der Programmierung zukünftiger Projekte zu helfen. Ich habe mich entschieden, keine herunterzuladen, da ich mein Wissen als Autodidakt erweitern möchte.JNativeHook (GlobalScreen) Ich arbeite nicht mit switch und if Anweisungen
Ich benutze JNativeHook als eine Bibliothek in NetBeans, um mir mit KeyListeners zu helfen, wenn der JFrame nicht fokussiert ist. Ich habe einige Stunden recherchiert und debuggt und herausgefunden, dass die KeyReleased-Methode nicht mit if- und switch-Anweisungen funktioniert. Es erkennt die KeyRelease, wie ich System.out.println();
in die Methode einfüge; es hat es ausgedruckt. Mein Code ist unten.
package Main;
import java.awt.MouseInfo;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.logging.LogManager;
import javax.swing.*;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.dispatcher.SwingDispatchService;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class MainClass implements NativeKeyListener {
static JFrame frame;
static JLabel label;
static JPanel panel;
static boolean run = true; //pause variable
private static void jframe(){ //JFrame code
frame = new JFrame("Mouse Coordinates");
label = new JLabel();
panel = new JPanel();
frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);
frame.setAlwaysOnTop(true);
panel.add(label);
frame.add(panel);
frame.pack();
}
private static void check(){ //updating label text
if(run){ //if not paused
new Thread(new Runnable() {
@Override
public void run() {
while(run){ //loop
label.setText(MouseInfo.getPointerInfo().getLocation().toString().replaceAll("java.awt.Point", ""));
frame.setSize(label.getWidth() + 5, label.getHeight() + 5); //Adapt frame size to fit label
}
}
}).start();
}
}
public void nativeKeyPressed(NativeKeyEvent e) {
}
public void nativeKeyReleased(NativeKeyEvent e) {
switch(e.getKeyCode()){
case 27: //close code (esc key)
System.exit(0);
case 80: //pause code (p key)
run = !run;
check();
case 67: //copy code (c key)
StringSelection ss = new StringSelection(MouseInfo.getPointerInfo().getLocation().toString().replaceAll("java.awt.Point", "").replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("=", "").replaceAll("x", "").replaceAll("y", "").replaceAll(",", ", ")); //get mouse coordinates and set them as a StringSelection
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); //get the clipboard
cb.setContents(ss, ss); //set what is copied to the current mouse coordinates
break;
}
}
public void nativeKeyTyped(NativeKeyEvent e) {
}
public static void main(String[] args){
LogManager.getLogManager().reset(); //stop the annoying constant logging of GlobalScreen
try {
GlobalScreen.setEventDispatcher(new SwingDispatchService());
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new MainClass());
jframe(); //Run JFrame code
check(); //Run check code
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainClass();
}
});
}
}
Jede Hilfe Verwendung von switch-Anweisungen mit JNativeHook Neueinstufung wird geschätzt!
Vielen Dank! Mein Programm funktioniert jetzt, danke nochmal für deine Hilfe! –