Handling “Global” KeyEvents

So after much painful trial-and-error combined with months of Google-searching in an attempt to handle certain KeyEvents in a “global” way, I’ve FINALLY found a way to do it.  This sort of handling is needed in Java in order for operations like undo, redo, and save to work regardless of what component has keyboard focus.  As painful as it was to find the solution to this, the solution itself is remarkably simple:

KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
focusManager.addKeyEventDispatcher(new KeyEventDispatcher(){
	public boolean dispatchKeyEvent(KeyEvent e) {
		return handleCommonShortcuts(e);

handleCommonShortcuts(e) is a function I wrote to check if the KeyEvent is for something like undo, redo, or save, and if so, do it and return true, else return false.  I wish that this had been a bit easier to find, but I’m glad that it works now, and without having to add a KeyListener to every single one of the 100’s of components that might be present in an PwnIDE window at any given time.  Smile

Now I can get back to implementing the remaining header operations before alpha, since they are dependent on such a handler.

In other news, I’ve written the draft copy of a paper on PwnIDE alpha, and after it is reviewed by fellow students, I will post a copy of the final version.

~ by Neil Dickson on November 19, 2007.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: