Key Event Demo : Key Event « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » J2ME » Key EventScreenshots 
Key Event Demo
Key Event Demo

/*
 * @(#)Tiles.java 1.6 00/05/24 Copyright (c) 2000 Sun Microsystems, Inc. All
 * Rights Reserved.
 
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Sun.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class Tiles extends MIDlet {

  Board b;

  public Tiles() {
    b = new Board(this);
  }

  public void startApp() {
    Display.getDisplay(this).setCurrent(b);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

}

/*
 * @(#)Board.java 1.14 00/05/23 Copyright (c) 2000 Sun Microsystems, Inc. All
 * Rights Reserved.
 
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with Sun.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

class Board extends Canvas implements CommandListener {

  MIDlet midlet;

  Command exitCommand;

  Font font;

  // Character Position
  int xPos, yPos;

  // Chracter Height and Width in pixels
  int charW, charH;

  public Board(MIDlet midlet_) {

    int i;

    midlet = midlet_;
    Display dpy = Display.getDisplay(midlet);
    int letterWidth = 4;

    font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
        Font.SIZE_MEDIUM);

    charW = font.charWidth('M'7;
    charH = font.getHeight() 1;

    xPos = (getWidth() (letterWidth * charW12;
    yPos = 1;

    exitCommand = new Command("Exit", Command.SCREEN, 2);
    addCommand(exitCommand);

    setCommandListener(this);
    repaint();

  }

  public void commandAction(Command c, Displayable d) {
    if (c == exitCommand) {
      midlet.notifyDestroyed();

    }
  }

  public void paint(Graphics g) {
    g.setColor(0);
    g.drawRect(44* charW + 2* charH + 2);
  }

  public void keyPressed(int code) {
    int game = getGameAction(code);

    switch (game) {
    case Canvas.UP:
      System.out.println("Canvas.UP");
      break;
    case Canvas.DOWN:
      System.out.println("Canvas.DOWN");
      break;
    case Canvas.LEFT:
      System.out.println("Canvas.LEFT");
      break;
    case Canvas.RIGHT:
      System.out.println("Canvas.RIGHT");
      break;
    }

    switch (code) {

    case Canvas.KEY_NUM0:
      System.out.println("Key 0");
      break;
    case Canvas.KEY_NUM1:
      System.out.println("Key 1");
      break;
    case Canvas.KEY_NUM2:
      System.out.println("Key 2");
      break;
    case Canvas.KEY_NUM3:
      System.out.println("Key 3");
      break;
    case Canvas.KEY_NUM4:
      System.out.println("Key 4");
      break;
    case Canvas.KEY_NUM5:
      System.out.println("Key 5");
      break;
    case Canvas.KEY_NUM6:
      System.out.println("Key 6");
      break;
    case Canvas.KEY_NUM7:
      System.out.println("Key 7");
      break;
    case Canvas.KEY_NUM8:
      System.out.println("Key 8");
      break;
    case Canvas.KEY_NUM9:
      System.out.println("Key 9");
      break;
    case Canvas.KEY_STAR:
      System.out.println("Star Key");
      break;

    case Canvas.KEY_POUND:
      System.out.println("Pound Key");
      break;

    //  default:

    //     System.out.println( "default" );
    //  return;
    }

  }

}


           
       
Related examples in the same category
1.Low-Level Display Canvas:Key Code Example Low-Level Display Canvas:Key Code Example
2.Event Example 1Event Example 1
3.Event Example 2Event Example 2
4.Game Key EventGame Key Event
5.Key MIDletKey MIDlet
6.Key Event with CanvasKey Event with Canvas
7.Canvas for processing key code and commandsCanvas for processing key code and commands
8.Canvas for processing game actionsCanvas for processing game actions
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.