×
Namespaces

Variants
Actions
Revision as of 14:41, 22 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to get KeyCode , Screen Size & Device platform in Java ME ?

From Nokia Developer Wiki
Jump to: navigation, search

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (27 Sep 2012)
This is a code dump, not an article. Explanation required of the key parts, and ideally a buildable example and links to associated reference material.

Article Metadata
Article
Created: senthilkumar05 (26 Dec 2007)
Last edited: hamishwillee (22 Jul 2013)
// bof: KeyDemo.java
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class KeyDemo extends MIDlet
{
public Display display = null;
public KeyCanvas keyCanvas = null;
 
public KeyDemo()
{
display = Display.getDisplay( this );
} // default constructor
 
 
protected void startApp()
{
keyCanvas = new KeyCanvas( this );
display.setCurrent( keyCanvas );
} // startApp
 
protected void pauseApp()
{
}
 
protected void destroyApp ( boolean destroy )
{
System.out.println ( "KeyDemo: destroyApp: destroy?: " + destroy );
 
}
 
public void exitMIDlet()
{
destroyApp( true );
notifyDestroyed();
}
 
} // class KeyDemo
 
// eof: KeyDemo.java


// KeyCanvas.java
 
 
import java.io.*;
import java.util.*;
 
import javax.microedition.midlet.*;
//import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
 
/** This Class is used to get the keycode values, Screen size, and device platform, and
 
 
displays in the canvas
/
 
public class KeyCanvas extends Canvas
{
 
boolean debug = true;
 
Font msgFont = Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL );
int msgFontHeight = msgFont.getHeight();
 
Font boldFont = Font.getFont( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL );
int boldFontHeight = boldFont.getHeight();
 
int WIDTH = 0;
int HEIGHT = 0;
KeyDemo midlet;
 
 
String KeyName = "";
String KeyCode = "";
String KeyAction = "";
String platform ="";
 
 
public KeyCanvas( KeyDemo midlet )
{
 
 
// super(false);
// setFullScreenMode(true);
this.midlet = midlet;
 
WIDTH = getWidth();
HEIGHT = getHeight();
 
platform= System.getProperty("microedition.platform");
System.out.println("platform :"+platform);
 
 
 
 
} // constructor KeyCanvas
 
void debug ( String msg )
{
if ( debug )
{
System.out.println ( msg );
}
} // debug (msg)
 
 
public void paint ( Graphics g )
{
g.setColor( 255, 255, 255 );
g.fillRect( 0, 0, WIDTH, HEIGHT );
 
g.setFont ( boldFont );
g.setColor ( 255, 0, 0 );
g.drawString( ""+WIDTH+"x"+HEIGHT, WIDTH/2, 2, g.HCENTER|g.TOP );
 
g.setFont ( boldFont );
g.setColor ( 0, 0, 0 );
g.drawString( "Press any Key", WIDTH/2, 1 * ( 2 + boldFontHeight ), g.HCENTER|g.TOP );
 
g.setFont ( msgFont );
g.setColor( 0, 0, 0 );
g.drawString( "Key Name:", 6, 3 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
g.setColor( 0, 0, 255 );
g.drawString( KeyName, 6 + msgFont.stringWidth("Key Name: "), 3 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
 
g.drawString( "Platform: ", 6, 6 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
g.setColor( 0, 0, 255 );
g.drawString(" "+platform, 6 + msgFont.stringWidth("Platform: "), 6 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
 
g.setColor( 0, 0, 0 );
g.drawString( "Key Code:", 6, 4 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
g.setColor( 0, 0, 255 );
g.drawString( KeyCode, 6 + msgFont.stringWidth("Key Code: "), 4 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
 
g.setColor( 0, 0, 0 );
g.drawString( "Key Action:", 6, 5 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
g.setColor( 0, 0, 255 );
g.drawString( KeyAction, 6 + msgFont.stringWidth("Key Action: "), 5 * ( 2 + msgFontHeight ), g.LEFT|g.TOP );
 
} // End of paint(g)
 
protected void keyRepeated ( int keyCode )
{
} // keyRepeated
 
protected void keyPressed ( int keyCode )
{
 
KeyName = getKeyName ( keyCode );
KeyCode = "" + keyCode;
KeyAction = "" + getGameAction( keyCode );
 
debug ( " KeyCanvas: keyPressed:\n Key Name: '" + KeyName + "', Key Code: '" + KeyCode + "', KeyAction: '" + KeyAction + "'" ) ;
 
/* if ( keyCode == -11 )
{
midlet.exitMIDlet();
}
else
{
repaint();
}*/

 
repaint();
} // End of keyPressed ( keyCode )
 
protected void keyReleased ( int keyCode )
{
}
 
 
 
} // eof: KeyCanvas.java
This page was last modified on 22 July 2013, at 14:41.
28 page views in the last 30 days.