Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Java KeyListener problem(KeyListener Method in java doesn't work on my PC)
Java KeyListener problem [message #721788] Fri, 02 September 2011 21:32 Go to next message
codytheking93 is currently offline codytheking93Friend
Messages: 4
Registered: September 2011
Junior Member
Hello,
I've coded on macs and everything works, but when I use the code on my PC my KeyListener doesn't work. I know the code is correct, I think its something with my computer maybe, I'm not sure. Please help.

Thanks,
Cody
Re: Java KeyListener problem [message #721850 is a reply to message #721788] Sat, 03 September 2011 06:04 Go to previous messageGo to next message
John Steele is currently offline John SteeleFriend
Messages: 33
Registered: June 2010
Location: Seattle, WA
Member
Cody, you need to be a little more specific. Perhaps show the snippet of code where the keylistener does not work.
Re: Java KeyListener problem [message #722846 is a reply to message #721850] Tue, 06 September 2011 22:40 Go to previous messageGo to next message
codytheking93 is currently offline codytheking93Friend
Messages: 4
Registered: September 2011
Junior Member
This is a little bit of the code. It worked on my friends computer with eclipse when I copy and pasted it, but it did not work on mine.

screen is a global int variable and when screen = 0 it shows the title screen and when screen = 1 the game screen shows, but when I press 's' the screen still shows the title screen.

public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
if(screen == 0 && e.getKeyChar() == 's') //changes title screen to game screen
screen++;
}
Re: Java KeyListener problem [message #722894 is a reply to message #722846] Wed, 07 September 2011 05:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

On 2011-09-07 00:40, codytheking93 wrote:
> This is a little bit of the code. It worked on my friends computer with
> eclipse when I copy and pasted it, but it did not work on mine.
>
> screen is a global int variable and when screen = 0 it shows the title
> screen and when screen = 1 the game screen shows, but when I press 's'
> the screen still shows the title screen.
>
> public void keyPressed(KeyEvent e){}
> public void keyReleased(KeyEvent e){}
> public void keyTyped(KeyEvent e){
> if(screen == 0 && e.getKeyChar() == 's') //changes title screen to game
> screen
> screen++;
> }

Please provide a minimum working example, otherwise we are completely in
the dust here. For example, from your example it is unclear, *where* you
registered the KeyListener. Minimum programs should be easy to write,
just look at the snippets given on

http://www.eclipse.org/swt/snippets/

Another example can be found by googling for "SWT example KeyListener":

http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/KeyListenerExample.htm

Greetings from Bremen,

Daniel Krügler
Re: Java KeyListener problem [message #723156 is a reply to message #722894] Wed, 07 September 2011 21:11 Go to previous messageGo to next message
codytheking93 is currently offline codytheking93Friend
Messages: 4
Registered: September 2011
Junior Member
ok here is code. The mouselistener works, but the keylistener does not. When I click the screen goes red, which is what it is supposed to do. I press 'c' and nothing happens. Even if I do it before I click.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class key_work extends JApplet implements KeyListener, MouseListener{

int screen = 0;
int sw = 500, sh = 500;

public void init(){
resize(sw, sh);
addKeyListener(this);
addMouseListener(this);
}

public void paint(Graphics g){
if(screen == 0)
{
g.clearRect(0, 0, sw, sh);
g.setColor(Color.black);
g.fillRect(0, 0, sw, sh);
}
else if(screen == 1)
{
g.clearRect(0, 0, sw, sh);
g.setColor(Color.red);
g.fillRect(0, 0, sw, sh);
}

else if(screen > 2)
{
g.clearRect(0, 0, sw, sh);
g.setColor(Color.blue);
g.fillRect(0, 0, sw, sh);
}
}

public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(screen == 0)
screen = 1;

repaint();
}

public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){
if(screen == 0 && e.getKeyChar() == 'c')
screen = 2;

repaint();
}
}
Re: Java KeyListener problem [message #723158 is a reply to message #723156] Wed, 07 September 2011 21:26 Go to previous messageGo to next message
John Steele is currently offline John SteeleFriend
Messages: 33
Registered: June 2010
Location: Seattle, WA
Member
This is not SWT, and it does not look like this has anything to do with Eclipse RCP.
Re: Java KeyListener problem [message #723159 is a reply to message #723158] Wed, 07 September 2011 21:32 Go to previous messageGo to next message
John Steele is currently offline John SteeleFriend
Messages: 33
Registered: June 2010
Location: Seattle, WA
Member
Try creating an untyped listener, implement handleEvent (KeyEvent e), check for e.getId() == KeyEvent.KEY_TYPED, if so, then char c = e.getKeyChar(), and then
do if (c == 'c'). My Swing is a bit rusty. Are you using SWT_AWT Bridge?
Re: Java KeyListener problem [message #723916 is a reply to message #723159] Fri, 09 September 2011 21:02 Go to previous message
codytheking93 is currently offline codytheking93Friend
Messages: 4
Registered: September 2011
Junior Member
Thanks for your help, I have it working now.
Previous Topic:Adding programmatically a MenuItem to a pop-up ContextMenu
Next Topic:Require suggestion in RCP
Goto Forum:
  


Current Time: Thu May 09 15:08:58 GMT 2024

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

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

Back to the top