【问题标题】:altgr doesn't work with jnativehook?altgr 不适用于 jnativehook?
【发布时间】:2016-12-22 21:30:40
【问题描述】:

这只是为了测试目的。

我尝试更改代码的某些部分并阅读 api-docs。 开始时我添加了“typedC += e.getKeyChar();”它看起来像记录 altgr-keys,但后来不再。

如何更改它以让 altgr-keys 工作?

谢谢

   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();
     }   
    }

【问题讨论】:

    标签: java key libraries


    【解决方案1】:

    什么操作系统? Linux 上的 2.0 存在一些已知问题,但它们将在 2.1 中修复。如果还有其他问题,您可能需要报告错误。请包括您的确切键盘布局、型号和语言。还提供了有关按下的键、观察到的行为和预期行为的详细信息。我只知道如何使用美式键盘,所以我不确定所有 Alt-Gr 语言/地区特定的键、掩码和锁应该如何工作。

    【讨论】:

    • 在 windows 7 专业版上,键盘布局和语言是瑞士德语。我试图只使用 getKeyChar(),它记录了除 altgr 之外的所有键。当我使用 getRawCode() 或 getKeyCode() 它记录数字或几个?或未知键码。
    • 唯一受 alt-gr 影响的键可能是键类型字符。原始代码是本地密钥代码,在 alt-gr 之间也可能相同。
    • 您可以通过电子邮件将您的键盘图片发给我吗?请指出 alt-gr 键。
    • 键盘K200罗技型号。 support.logitech.com/de_ch/product/media-keyboard-k200# altgr 键正好与空格键对应。希望你能看到它,如果没有,我会给你发电子邮件。
    • 我忘了补充,我也试过 NativeKeyEvent.getKeyText(e.getRawCode()); NativeKeyEvent.getKeyText(e.getKeyCode());并且似乎在虚拟机内部它也没有记录,不知何故逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多