java - Key listener for composite? -
i want add key listener composite. code follows:
@override protected control createdialogarea(composite parent) { //add swt text box , combo etc parent }
the composite a: org.eclipse.swt.widgets.composite
want add key listener composite parent.
when ever user presses ctrl or escape, user should notified.
if focus on 1 text or combo field , parent listener should notified. help.
ok, here go: add filter
display
. within listener
check if parent of current focus control shell
of composite
. if so, check key code.
in conclusion, handle key event, if focus "within" composite
, ignore if "outside" composite
.
public static void main(string[] args) { display display = new display(); final shell shell = new shell(display); shell.setlayout(new gridlayout(1, false)); final composite content = new composite(shell, swt.none); content.setlayout(new gridlayout(2, false)); text text = new text(content, swt.border); button button = new button(content, swt.push); button.settext("button"); display.addfilter(swt.keyup, new listener() { @override public void handleevent(event e) { if (e.widget instanceof control && ischild(content, (control) e.widget)) { if ((e.statemask & swt.ctrl) == swt.ctrl) { system.out.println("ctrl pressed"); } else if(e.keycode == swt.esc) { system.out.println("esc pressed"); } } } }); text outsidetext = new text(shell, swt.border); shell.pack(); shell.open(); while (!shell.isdisposed()) { if (!display.readanddispatch()) { display.sleep(); } } display.dispose(); } private static boolean ischild(control parent, control child) { if (child.equals(parent)) return true; composite p = child.getparent(); if (p != null) return ischild(parent, p); else return false; }
Comments
Post a Comment