Вам нужно использовать keylistener, он позволяет получить ключ
Example
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication7;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
*
* @author wilso
*/
public class List implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Если вы не хотите использовать awt, тогда вы можете использовать сканер или Event
@Override
public void nativeKeyPressed(NativeKeyEvent e) {
String temp = NativeKeyEvent.getKeyText(e.getKeyCode());
if (validHotKeys.contains(temp)) {
if (!pressedHotKeys.contains(temp)) {
pressedHotKeys.add(temp);
System.out.println(pressedHotKeys.toString());
}
}
if (validAlphaKeys.contains(temp)) {
if (!pressedAlphaKeys.contains(temp)) {
pressedAlphaKeys.add(temp);
System.out.println(pressedAlphaKeys.toString());
}
}
}