Skip to main content

Cant get keyListener to work

1 reply [Last post]
vartana
Offline
Joined: 2008-12-01

when i press a key the message does not change. Any ideas thanks!

<br />
package org.homebrew;</p>
<p>import java.io.*;<br />
import java.util.*;<br />
import java.awt.*;<br />
import java.net.*;<br />
import javax.media.*;<br />
import javax.tv.xlet.*;<br />
import org.dvb.event.EventManager;<br />
import org.dvb.event.UserEvent;<br />
import org.dvb.event.UserEventListener;<br />
import org.dvb.event.UserEventRepository;<br />
import org.bluray.ui.event.HRcEvent;<br />
import org.havi.ui.*;</p>
<p>public class MyXlet implements Xlet, UserEventListener, ControllerListener {<br />
	private HScene scene;<br />
	private Container gui;<br />
	private XletContext context;<br />
	private final ArrayList messages = new ArrayList();</p>
<p>	public void initXlet(XletContext context) {<br />
		this.context = context;<br />
		scene = HSceneFactory.getInstance().getDefaultHScene();</p>
<p>		try {<br />
			gui = new Screen(messages);<br />
			gui.setSize(1920, 1080); // BD screen size<br />
			scene.add(gui, BorderLayout.CENTER);<br />
			messages.add("Hello Cannery to BD-J!");</p>
<p>		} catch (Exception e) {<br />
			messages.add(e.getMessage());<br />
		}<br />
		scene.validate();</p>
<p>                UserEventRepository userEventRepo = new UserEventRepository("x");<br />
                userEventRepo.addAllArrowKeys();<br />
                userEventRepo.addAllColourKeys();<br />
                userEventRepo.addAllNumericKeys();<br />
                userEventRepo.addKey(HRcEvent.VK_ENTER);<br />
                userEventRepo.addKey(HRcEvent.VK_POPUP_MENU);<br />
                EventManager.getInstance().addUserEventListener(this, userEventRepo);<br />
	}</p>
<p>    public void userEventReceived(UserEvent e) {<br />
        messages.add("Button Pressed");<br />
        if (e.getType() == HRcEvent.KEY_PRESSED) {</p>
<p>        }<br />
    }	</p>
<p>	public void startXlet() {<br />
		gui.setVisible(true);<br />
		scene.setVisible(true);<br />
		gui.requestFocus();<br />
	}</p>
<p>	public void pauseXlet() {<br />
		gui.setVisible(false);<br />
	}</p>
<p>	public void destroyXlet(boolean unconditional) {<br />
		scene.remove(gui);<br />
		scene = null;<br />
	}</p>
<p>	public void controllerUpdate(ControllerEvent arg0) {<br />
		// TODO Auto-generated method stub<br />
		messages.add("Button Pressed");<br />
	}<br />
}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vinaykagarwal
Offline
Joined: 2006-01-14

Your code for handling keys works. The problem must be with your message part. Here's the code I just tested with TMT

/**
* @author Vinay Agarwal
*/
package keylistener;
import java.awt.*;
import org.bluray.ui.event.HRcEvent;
import org.dvb.event.*;
import org.havi.ui.*;

public class Main implements javax.tv.xlet.Xlet, UserEventListener {

private static Font font;
private HScene scene;
private HStaticText text;

public Main() {
}

public void initXlet(javax.tv.xlet.XletContext context)
throws javax.tv.xlet.XletStateChangeException {

font = new Font(null, Font.PLAIN, 48);
scene = HSceneFactory.getInstance().getDefaultHScene();
text = new HStaticText("Hello - just started", 0, 0, 1920, 1080);
text.setForeground(Color.white);
text.setTextLayoutManager(new HDefaultTextLayoutManager());
text.setFont(font);
scene.add(text);

UserEventRepository userEventRepo = new UserEventRepository("RepoName");
userEventRepo.addAllArrowKeys();
userEventRepo.addAllColourKeys();
userEventRepo.addAllNumericKeys();
userEventRepo.addKey(HRcEvent.VK_ENTER);
userEventRepo.addKey(HRcEvent.VK_POPUP_MENU);
EventManager.getInstance().addUserEventListener(this, userEventRepo);
scene.validate();
}

public void startXlet() throws javax.tv.xlet.XletStateChangeException {
scene.setVisible(true);
}

public void pauseXlet() {
scene.setVisible(false);
}

public void destroyXlet(boolean unconditional)
throws javax.tv.xlet.XletStateChangeException {
scene = null;
}

public void userEventReceived(UserEvent e) {
text.setTextContent("Pressed - " + e.getCode(), HState.NORMAL_STATE);
}
}