Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Getting Keyboard to work Posted by TAMBY on 2 Dec 2008 at 9:22 PM
I'm using Textpad and basically i just want to get the keyboard to work, nothing to crazy. This is what I have so far and it comes out with two errors.

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

public class ComputerClub

{

KeyBoard Handler = new KeyBoard();

public void init()

{

this.addKeyListener(Handler);

}

public static void main ( String [] args )

{

	System.out.println ("Sample text");

}

public class Keyboard implements KeyListener

{

public void keyTyped(KeyEvent event)

{

char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
char e = 'e';
char f = 'f';
char g = 'g';
char h = 'h';
char i = 'i';
char j = 'j';
char k = 'k';
char l = 'l';
char m = 'm';
char n = 'n';
char o = 'o';
char p = 'p';
char q = 'q';
char r = 'r';
char s = 's';
char t = 't';
char u = 'u';
char v = 'v';
char w = 'w';
char x = 'x';
char y = 'y';
char z = 'z';

if(a == event.getKeyChar())

System.out.println("This is the A key");

if(b == event.getKeyChar())
	
System.out.println("This is the B key");

if(c == event.getKeyChar())

System.out.println("This is the C key");

if(d == event.getKeyChar())

System.out.println("This is the D key");

if(e == event.getKeyChar())

System.out.println("This is the E key");

if(f == event.getKeyChar())

System.out.println("This is the F key");

if(g == event.getKeyChar())

System.out.println("This is the G key");

if(h == event.getKeyChar())

System.out.println("This is the H key");

if(i == event.getKeyChar())

System.out.println("This is the I key");

if(j == event.getKeyChar())

System.out.println("This is the J key");

if(k == event.getKeyChar())

System.out.println("This is the K key");

if(l == event.getKeyChar())

System.out.println("This is the L key");

if(m == event.getKeyChar())

System.out.println("This is the M key");

if(n == event.getKeyChar())

System.out.println("This is the N key");

if(o == event.getKeyChar())

System.out.println("This is the O key");

if(p == event.getKeyChar())

System.out.println("This is the P key");

if(q == event.getKeyChar())

System.out.println("This is the Q key");

if(r == event.getKeyChar())

System.out.println("This is the R key");

if(s == event.getKeyChar())

System.out.println("This is the S key");

if(t == event.getKeyChar())

System.out.println("This is the T key");

if(u == event.getKeyChar())

System.out.println("This is the U key");

if(v == event.getKeyChar())

System.out.println("This is the V key");

if(w == event.getKeyChar())

System.out.println("This is the W key");

if(x == event.getKeyChar())

System.out.println("This is the X key");

if(y == event.getKeyChar())

System.out.println("This is the Y key");

if(z == event.getKeyChar())

System.out.println("This is the Z key");

}

public void keyPressed(KeyEvent event)

{


}

public void keyReleased(KeyEvent event)

{

}

}

}


And the two errors I get are...


F:\Documents\School\Period 3 - Java\Applets\Computer Club\ComputerClub.java:11: cannot find symbol
symbol : class KeyBoard
location: class ComputerClub
KeyBoard Handler = new KeyBoard();

F:\Documents\School\Period 3 - Java\Applets\Computer Club\ComputerClub.java:11: cannot find symbol
symbol : class KeyBoard
location: class ComputerClub
KeyBoard Handler = new KeyBoard();


Report
Re: Getting Keyboard to work Posted by XLoom on 3 Dec 2008 at 1:02 AM
Uhh, long... Find a working example on how to access keyboard in Java (example in http://forums.sun.com/thread.jspa?threadID=698156 should work) and modify it to fit your needs. At the moment the error means that such class as Keyboard does not exists, but I am not a regular Java user so I can't tell you precisely what you should do instead of using it. But I can tell you that don't have to so long code for doing just what you want. Something like this instead of the long definitions of char a='a' etc is sufficient.
  if(event.getKeyChar()>='a' && event.getKeyChar()<='z') {
    System.out.println("This is the "+event.getKeyChar()+" key");    
  }





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.