I could figure out only two way to intercept keys in java.
If you want to do it for a standalone java application then you will have to do it natively, using JNI.You can google to find out the C/C++ libraries to intercept keys . And then you can load the dlls in java to help you with your requirement.
If you are writing a SWING based GUI then you can add keypress lisenter to your components. You may have to add it to all the components recursively to intercept the key at any level of your UI. Here is a sample code for that:
Add key listener to a components and its children using a method:
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]);
}
}
}
Method to do get the key press event:
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
}
Hope it helps!