2017-06-16 1 views
2

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!

Antwort

3

Dieser Code löst Ihr Problem.

  1. denke ich, das Hauptproblem auf dem e.getKeyCode() ist, weil es das gleiche auf nativeKeyPressed nicht zurückkommt und nativeKeyReleased Also habe ich Ihren Code in der Methode public void nativeKeyPressed(NativeKeyEvent e)

  2. Ich habe auch einige Kommentare visuell anzeigt welche Taste drücken Sie?

  3. Um die "magischen Zahlen" zu vermeiden (27, 87, 67 wer kann guees was bedeutet?) Ich konvertiere in seine String-Form, ich denke, dass die Lesbarkeit des Codes verbessert wird.

  4. Ich legte die break; Anweisung am Ende jedes Fallblocks, wenn Sie nicht setzen, wird der Code durch alle Fälle passieren.

    public void nativeKeyPressed(NativeKeyEvent e) { 
        String keyText = NativeKeyEvent.getKeyText(e.getKeyCode()); 
        switch(keyText){ 
        case "Escape": //close code (esc key) 
         System.out.println("Pressed esc"); 
         System.exit(0); 
         break; 
        case "P": //pause code (p key) 
         System.out.println("Pressed p"); 
         run = !run; 
         check(); 
         break; 
        case "C": //copy code (c key) 
         System.out.println("Pressed c"); 
         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 nativeKeyReleased(NativeKeyEvent e) { 
    
    } 
    
+0

Vielen Dank! Mein Programm funktioniert jetzt, danke nochmal für deine Hilfe! –