【问题标题】:How can i get unique key code for character key如何获得字符键的唯一键码
【发布时间】:2014-09-26 08:50:36
【问题描述】:

默认的listner只有字符键的字符值,所有字符的code都是VK_UNDEFINED,但这对字符和系统键的处理有所不同。

如何用一种方法独立处理所有键?

这是一个问题,因为我尝试将密钥保存在一个文本文件中,所以我需要检查是否有代码或字符来解析这个文件。

【问题讨论】:

    标签: java keycode key-events


    【解决方案1】:

    它对我有用:

    import java.awt.event.*;
    import javax.swing.*;
    
    class TestKeyCode implements  KeyListener {
    
        public void keyPressed(KeyEvent e)
        {
            System.out.println("keyPressed(KeyEvent e)");
            int code= e.getKeyCode();
            System.out.println("code = " + code);
        }
    
        public void keyReleased(KeyEvent e) {
        }
    
        public void keyTyped(KeyEvent e) {
        }
    
        public static void main(String[] args) {
            JFrame jf = new JFrame();
            jf.setSize(800, 800);
            TestKeyCode tkc = new TestKeyCode();
            jf.addKeyListener(tkc);
            jf.setVisible(true);
        }
    }
    

    【讨论】:

    • 这很奇怪:oracle 文档说,字符确实 NOT 调用 keyPressed 事件。但它有效,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2017-02-14
    • 2022-12-02
    • 2014-01-12
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多