简体   繁体   中英

How can i get unique key code for character key

The default listner has only character value for character keys, and code for all of them is VK_UNDEFINED , but this make difference between characters and system keys processing.

How to handle all keys with one method, independently of its type?

This is a problem, because I try to save key in a text file, so I need to check if there is a code or a character to parse this file back.

It works for me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM