I would like to invoke a thread within my program that constantly watches the keyboard and if a certain key is pressed then tell me (system.out).
The problem is that the KeyListener interface must have focus for it to return keystate.
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Is there a different way to determine the state of the keys on the keyboard?
My aim is to see that the user pressed ctrl+n, if so, update the system clipboard text to something.
CODE:
class KeyboardWatcher extends Thread
{
boolean flag = false;
public void run()
{
while(!flag)
{
if (shift & n are pressed)
{
StringSelection stringSelection = new StringSelection("Clip Text");
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
flag = true;
}
}
return;
}
}
This is not possible without
Java Native Interface. You will need a keyboard hook at the OS API level. There are many available solutions you could reuse. Look at this for one such example.