Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » KeyEvent for Text
KeyEvent for Text [message #445149] Wed, 27 October 2004 07:42 Go to next message
Eclipse UserFriend
Originally posted by: nvragh.yahoo.co.uk

Hello,

In my application, KeyEvent is fired only for Text (that too only visible
text). Not for Label. (Pasted the demo code below)
Can anyone throw some light on this, pls. It's little bit urgent.

Also, can i add keylistener to a shell?

If possible, reply me @ nvragh@yahoo.co.uk.

Thanks

public class HelloWorld
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new HelloWorld().open (display);
while (!shell.isDisposed ())
{
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

public Shell open (Display display)
{
final Shell shell = new Shell (display);
Label label = new Label (shell, SWT.CENTER);
label.setText ("Hello_world");
label.pack();

Text text = new Text(shell, SWT.RIGHT);
text.setText ("Text");
text.pack();
text.setVisible(true);

shell.addListener(SWT.F2, new Listener()
{
public void handleEvent(final Event event)
{
System.out.println("F2 in addListener");
}
});

label.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
//This is not at all getting executed
int code = e.keyCode;
System.out.println("Label Key code : "+code);
System.out.println("Label Key character: "+e.character);

if(code == 16777227)
{
System.out.println("Label F2F2F2F2F2F2F2!!!!!!!!!!!!!");
shell.dispose();
}
}
});


text.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
int code = e.keyCode;
System.out.println("Text Key code : "+code);
System.out.println("Text Key character: "+e.character);

if(code == 16777227)
{
System.out.println("Text F2F2F2F2F2F2F2!!!!!!!!!!!!!");
shell.dispose();
}
}
});


shell.pack();
shell.open ();
return shell;
}
}
Re: KeyEvent for Text [message #445183 is a reply to message #445149] Wed, 27 October 2004 15:23 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Key events are only fired for the control that has focus. Labels don't take
focus. Key events are not inherited in SWT so a key listener in the shell
will only be fired when the shell itself has focus, not when a child has
focus.

"nvragh" <nvragh@yahoo.co.uk> wrote in message
news:clnji0$ho4$1@eclipse.org...
> Hello,
>
> In my application, KeyEvent is fired only for Text (that too only visible
> text). Not for Label. (Pasted the demo code below)
> Can anyone throw some light on this, pls. It's little bit urgent.
>
> Also, can i add keylistener to a shell?
>
> If possible, reply me @ nvragh@yahoo.co.uk.
>
> Thanks
>
> public class HelloWorld
> {
> public static void main (String [] args)
> {
> Display display = new Display ();
> Shell shell = new HelloWorld().open (display);
> while (!shell.isDisposed ())
> {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
> public Shell open (Display display)
> {
> final Shell shell = new Shell (display);
> Label label = new Label (shell, SWT.CENTER);
> label.setText ("Hello_world");
> label.pack();
>
> Text text = new Text(shell, SWT.RIGHT);
> text.setText ("Text");
> text.pack();
> text.setVisible(true);
>
> shell.addListener(SWT.F2, new Listener()
> {
> public void handleEvent(final Event event)
> {
> System.out.println("F2 in addListener");
> }
> });
>
> label.addKeyListener(new KeyAdapter(){
> public void keyPressed(KeyEvent e)
> {
> //This is not at all getting executed
> int code = e.keyCode;
> System.out.println("Label Key code : "+code);
> System.out.println("Label Key character: "+e.character);
>
> if(code == 16777227)
> {
> System.out.println("Label F2F2F2F2F2F2F2!!!!!!!!!!!!!");
> shell.dispose();
> }
> }
> });
>
>
> text.addKeyListener(new KeyAdapter(){
> public void keyPressed(KeyEvent e)
> {
> int code = e.keyCode;
> System.out.println("Text Key code : "+code);
> System.out.println("Text Key character: "+e.character);
>
> if(code == 16777227)
> {
> System.out.println("Text F2F2F2F2F2F2F2!!!!!!!!!!!!!");
> shell.dispose();
> }
> }
> });
>
>
> shell.pack();
> shell.open ();
> return shell;
> }
> }
>
Re: KeyEvent for Text [message #445206 is a reply to message #445183] Thu, 28 October 2004 12:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nvragh.yahoo.co.uk

Thanks a lot for your reply. One small clarification. As per the
information i collected after searching this forum, I came to know that i
can grab the keyevent globally (irrespective of which item has focus) if i
use Display.addFilter(event,listener). But, the handleEvent method of my
addFilter listener is not getting called.

I have two text's in my shell. I added keylistener for only one text and
added filter for the display as follows. If the focus is in other text,
the event is not fired. Anything i am doing wrong?

Code:
=====

getShell().getDisplay().addFilter(SWT.F2,new Listener(){
public void handleEvent(Event e)
{
System.out.println("11111111");
}
}
);

text1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
int code = e.keyCode;
if(code == SWT.F2)
{
System.out.println("2222222");
}
}
});



Steve Northover wrote:

> Key events are only fired for the control that has focus. Labels don't take
> focus. Key events are not inherited in SWT so a key listener in the shell
> will only be fired when the shell itself has focus, not when a child has
> focus.

> "nvragh" <nvragh@yahoo.co.uk> wrote in message
> news:clnji0$ho4$1@eclipse.org...
> > Hello,
> >
> > In my application, KeyEvent is fired only for Text (that too only visible
> > text). Not for Label. (Pasted the demo code below)
> > Can anyone throw some light on this, pls. It's little bit urgent.
> >
> > Also, can i add keylistener to a shell?
> >
> > If possible, reply me @ nvragh@yahoo.co.uk.
> >
> > Thanks
> >
> > public class HelloWorld
> > {
> > public static void main (String [] args)
> > {
> > Display display = new Display ();
> > Shell shell = new HelloWorld().open (display);
> > while (!shell.isDisposed ())
> > {
> > if (!display.readAndDispatch ()) display.sleep ();
> > }
> > display.dispose ();
> > }
> >
> > public Shell open (Display display)
> > {
> > final Shell shell = new Shell (display);
> > Label label = new Label (shell, SWT.CENTER);
> > label.setText ("Hello_world");
> > label.pack();
> >
> > Text text = new Text(shell, SWT.RIGHT);
> > text.setText ("Text");
> > text.pack();
> > text.setVisible(true);
> >
> > shell.addListener(SWT.F2, new Listener()
> > {
> > public void handleEvent(final Event event)
> > {
> > System.out.println("F2 in addListener");
> > }
> > });
> >
> > label.addKeyListener(new KeyAdapter(){
> > public void keyPressed(KeyEvent e)
> > {
> > //This is not at all getting executed
> > int code = e.keyCode;
> > System.out.println("Label Key code : "+code);
> > System.out.println("Label Key character: "+e.character);
> >
> > if(code == 16777227)
> > {
> > System.out.println("Label F2F2F2F2F2F2F2!!!!!!!!!!!!!");
> > shell.dispose();
> > }
> > }
> > });
> >
> >
> > text.addKeyListener(new KeyAdapter(){
> > public void keyPressed(KeyEvent e)
> > {
> > int code = e.keyCode;
> > System.out.println("Text Key code : "+code);
> > System.out.println("Text Key character: "+e.character);
> >
> > if(code == 16777227)
> > {
> > System.out.println("Text F2F2F2F2F2F2F2!!!!!!!!!!!!!");
> > shell.dispose();
> > }
> > }
> > });
> >
> >
> > shell.pack();
> > shell.open ();
> > return shell;
> > }
> > }
> >
Re: KeyEvent for Text [message #445212 is a reply to message #445206] Thu, 28 October 2004 13:49 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Try "addFilter(SWT.KeyDown, ..." rather than "addFilter(SWT.F2, ...". That
should work but be warned that your filter will be called for every key that
is pressed in every shell on the display.

"nvragh" <nvragh@yahoo.co.uk> wrote in message
news:clqn51$j8f$1@eclipse.org...
> Thanks a lot for your reply. One small clarification. As per the
> information i collected after searching this forum, I came to know that i
> can grab the keyevent globally (irrespective of which item has focus) if i
> use Display.addFilter(event,listener). But, the handleEvent method of my
> addFilter listener is not getting called.
>
> I have two text's in my shell. I added keylistener for only one text and
> added filter for the display as follows. If the focus is in other text,
> the event is not fired. Anything i am doing wrong?
>
> Code:
> =====
>
> getShell().getDisplay().addFilter(SWT.F2,new Listener(){
> public void handleEvent(Event e)
> {
> System.out.println("11111111");
> }
> }
> );
>
> text1.addKeyListener(new KeyAdapter(){
> public void keyPressed(KeyEvent e)
> {
> int code = e.keyCode;
> if(code == SWT.F2)
> {
> System.out.println("2222222");
> }
> }
> });
>
>
>
> Steve Northover wrote:
>
> > Key events are only fired for the control that has focus. Labels don't
take
> > focus. Key events are not inherited in SWT so a key listener in the
shell
> > will only be fired when the shell itself has focus, not when a child has
> > focus.
>
> > "nvragh" <nvragh@yahoo.co.uk> wrote in message
> > news:clnji0$ho4$1@eclipse.org...
> > > Hello,
> > >
> > > In my application, KeyEvent is fired only for Text (that too only
visible
> > > text). Not for Label. (Pasted the demo code below)
> > > Can anyone throw some light on this, pls. It's little bit urgent.
> > >
> > > Also, can i add keylistener to a shell?
> > >
> > > If possible, reply me @ nvragh@yahoo.co.uk.
> > >
> > > Thanks
> > >
> > > public class HelloWorld
> > > {
> > > public static void main (String [] args)
> > > {
> > > Display display = new Display ();
> > > Shell shell = new HelloWorld().open (display);
> > > while (!shell.isDisposed ())
> > > {
> > > if (!display.readAndDispatch ()) display.sleep ();
> > > }
> > > display.dispose ();
> > > }
> > >
> > > public Shell open (Display display)
> > > {
> > > final Shell shell = new Shell (display);
> > > Label label = new Label (shell, SWT.CENTER);
> > > label.setText ("Hello_world");
> > > label.pack();
> > >
> > > Text text = new Text(shell, SWT.RIGHT);
> > > text.setText ("Text");
> > > text.pack();
> > > text.setVisible(true);
> > >
> > > shell.addListener(SWT.F2, new Listener()
> > > {
> > > public void handleEvent(final Event event)
> > > {
> > > System.out.println("F2 in addListener");
> > > }
> > > });
> > >
> > > label.addKeyListener(new KeyAdapter(){
> > > public void keyPressed(KeyEvent e)
> > > {
> > > //This is not at all getting executed
> > > int code = e.keyCode;
> > > System.out.println("Label Key code : "+code);
> > > System.out.println("Label Key character: "+e.character);
> > >
> > > if(code == 16777227)
> > > {
> > > System.out.println("Label F2F2F2F2F2F2F2!!!!!!!!!!!!!");
> > > shell.dispose();
> > > }
> > > }
> > > });
> > >
> > >
> > > text.addKeyListener(new KeyAdapter(){
> > > public void keyPressed(KeyEvent e)
> > > {
> > > int code = e.keyCode;
> > > System.out.println("Text Key code : "+code);
> > > System.out.println("Text Key character: "+e.character);
> > >
> > > if(code == 16777227)
> > > {
> > > System.out.println("Text F2F2F2F2F2F2F2!!!!!!!!!!!!!");
> > > shell.dispose();
> > > }
> > > }
> > > });
> > >
> > >
> > > shell.pack();
> > > shell.open ();
> > > return shell;
> > > }
> > > }
> > >
>
>
Previous Topic:Why is the SWT Browser not currently available on the Solaris Motif plateform?
Next Topic:How to invoke clipboard actions in a portable way
Goto Forum:
  


Current Time: Tue Oct 20 04:00:39 GMT 2020

Powered by FUDForum. Page generated in 0.04632 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top