altgr doesn't work with jnativehook?

203 Views Asked by At

It's just for testing purposes.

I tried to change some parts of the code and read also the api-docs. On begin I added "typedC += e.getKeyChar();" and it looked like to log the altgr-keys, but later not anymore.

How to change it to let work altgr-keys ?

Thanks

   import java.io.BufferedWriter;
   import java.io.File;
   import java.io.FileNotFoundException;
   import java.io.FileWriter;
   import java.io.IOException;
   import java.util.logging.Level;
   import java.util.logging.LogManager;
   import java.util.logging.Logger;
   import javax.swing.JFileChooser;
   import org.jnativehook.GlobalScreen;
   import org.jnativehook.NativeHookException;
   import org.jnativehook.keyboard.NativeKeyEvent;
   import org.jnativehook.keyboard.NativeKeyListener;

   public class JkL implements NativeKeyListener {
    private String typedC = "";
    private String typedC1 = "";
    private final JFileChooser fc = new JFileChooser();
    private File direc;

    private void openFchooser1() throws FileNotFoundException,   
    InterruptedException, IOException, Exception {
     Thread.sleep(2000);
     int returnVal = fc.showDialog(null, "Choose a Logfile");
     if(returnVal == JFileChooser.APPROVE_OPTION) {
      direc = fc.getSelectedFile();
    }
 /* Construct the example object and initialze native hook. */
    GlobalScreen.addNativeKeyListener(new Kl());
    try {
        /* Register jNativeHook */
        GlobalScreen.registerNativeHook();
    } catch (NativeHookException ex) {
        /* Its error */
        System.err.println("There was a problem registering the native 
        hook.");
        System.err.println(ex.getMessage());
        System.exit(1);
    }

    // Clear previous logging configurations.
      LogManager.getLogManager().reset();

   // Get the logger for "org.jnativehook" and set the level to off. 
      Logger logger = 
      Logger.getLogger(GlobalScreen.class.getPackage().getName());
      logger.setLevel(Level.OFF);
 }

@Override
public void nativeKeyPressed(NativeKeyEvent nke) {
    throw new UnsupportedOperationException("Not supported yet."); //To 
   change body of generated methods, choose Tools | Templates.
}

@Override
public void nativeKeyReleased(NativeKeyEvent nke) {
    throw new UnsupportedOperationException("Not supported yet."); //To 
       change body of generated methods, choose Tools | Templates.
}

@Override
public void nativeKeyTyped(NativeKeyEvent nke) {
    throw new UnsupportedOperationException("Not supported yet."); //To  
        change body of generated methods, choose Tools | Templates.
}
 class Kl extends JkL {     
  /* Key Pressed */
  @Override
  public void nativeKeyPressed(NativeKeyEvent e) {
    //typedC += NativeKeyEvent.getKeyText(e.getKeyCode());
    typedC += e.getRawCode();
    typedC += e.getKeyChar();
    typedC += e.getKeyCode();
    try {
        writeToFile(typedC);
    } catch (IOException ex) {
        Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null, 
           ex);
    }
    /* Terminate program when one press ESCAPE */
    if (e.getKeyCode() == NativeKeyEvent.VC_F12) {
        try {
            GlobalScreen.unregisterNativeHook();
        } catch (NativeHookException ex) {
            Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, 
            null, ex);
          }
         }
        }

  @Override 
  public void nativeKeyReleased(NativeKeyEvent e) {
    //System.out.println("Key Released: " +   
       //NativeKeyEvent.getKeyText(e.getKeyCode()));
  }

/* I can't find any output from this call */
@Override
public void nativeKeyTyped(NativeKeyEvent e) {
  //typedC1 += NativeKeyEvent.getKeyText(e.getKeyCode());
  typedC1 += e.getRawCode();
  typedC1 += e.getKeyChar();
  typedC1 += e.getKeyCode();
    try {
        writeToFile(typedC1);  
    } catch (IOException ex) {
        Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null, 
            ex); 
       }
      }
     }
    private void writeToFile(String ln) throws IOException {
     //System.out.println(direc);
     FileWriter fw = new FileWriter(direc); 
     try (BufferedWriter bw = new BufferedWriter(fw)) {
     bw.write(ln);
     bw.newLine();
     bw.flush();
    }
   } 
      public static void main(String[] args) throws 
          IOException,InterruptedException, Exception {  
      new JkL().openFchooser1();
     }   
    }
1

There are 1 best solutions below

6
On

What operating system? There are some known issues with 2.0 on Linux but they will be fixed in 2.1. If there are any other issues, you will probably need to report a bug. Please include your exact keyboard layout, model and language. Also provided detailed information about what key is pressed, the observed behavior and the expected behavior. I only know how to use a US keyboard, so I am not sure how all of the Alt-Gr language/region specific keys, masks and locks are suppose to work.