я мог понять, только два способа перехвата ключей в Java.
Если вы хотите сделать это для отдельного приложения Java, то вам придется сделать это изначально, используя JNI.You может Google, чтобы узнать C библиотеки/C++ для перехвата клавиш. И затем вы можете загрузить DLL в java, чтобы помочь вам с вашим требованием.
Если вы пишете GUI на основе SWING, вы можете добавить keypress lisenter в свои компоненты. Возможно, вам придется добавить его ко всем компонентам рекурсивно, чтобы перехватить ключ на любом уровне вашего пользовательского интерфейса. Вот пример кода, который:
Добавить ключ слушателя до компонентов и его детей с помощью метода:
private void addKeyAndContainerListenerRecursively(Component c)
{
//Add KeyListener to the Component passed as an argument
c.addKeyListener(this);
//Check if the Component is a Container
if(c instanceof Container) {
//Component c is a Container. The following cast is safe.
Container cont = (Container)c;
//Add ContainerListener to the Container.
cont.addContainerListener(this);
//Get the Container's array of children Components.
Component[] children = cont.getComponents();
//For every child repeat the above operation.
for(int i = 0; i < children.length; i++){
addKeyAndContainerListenerRecursively(children[i]);
}
}
}
метод, чтобы сделать получить ключ пресс-конференцию:
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_ESCAPE){
//Key pressed is the Escape key. Hide this Dialog.
setVisible(false);
}
else if(code == KeyEvent.VK_ENTER){
//Key pressed is the Enter key. Redefine performEnterAction() in subclasses
to respond to pressing the Enter key.
performEnterAction(e);
}
//Insert code to process other keys here
}
Надеюсь, это поможет!
Если вы хотите обрабатывать отдельные нажатия клавиш, создайте графический интерфейс Swing. В Интернете есть отличные учебные пособия, которые Google может вам помочь. –
Посмотрите это http://stackoverflow.com/questions/16381879/how-to-process-the-backspace-terminal-control-character-in-java –