Key Event Demo : Key Event « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
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
ww__w___.ja_va___2__s___._c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.