Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 25th, 2007, 2:23 PM   #1
sibelius7
Newbie
 
Join Date: Dec 2007
Location: Oregon, United States
Posts: 4
Rep Power: 0 sibelius7 is on a distinguished road
Keypress question from a somewhat new Java programmer

Hi there...

I believe that my question is somewhat simple, but as my thread title suggests, I'm a little new at Java programming.

My problem is that I need to know how register keypresses somehow with Java. I plan to use this as a component of part of a senior project (which is basically a very simple music composition program which saves files in .mid format) for my computer science class which. This is the only component of the program so far that I havent had any progress in, and seeing as it's winter break now... I might as well use this time wisely.

All I need is to figure out how to do the following... in code of course:

If the a-key is pressed, perform action x.

I have read a little and heard about keypress listening, and code that sits in an seemingly-infinite loop that only terminates upon the press of a certain key, but I just don't know how to implement it.

Any help at all would be appreciated

Thanks
sibelius7 is offline   Reply With Quote
Old Dec 25th, 2007, 2:54 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Re: Keypress question from a somewhat new Java programmer

This should help

http://java.sun.com/j2se/1.5.0/docs/...yListener.html

http://java.sun.com/docs/books/tutor...ylistener.html
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Dec 25th, 2007, 5:11 PM   #3
sibelius7
Newbie
 
Join Date: Dec 2007
Location: Oregon, United States
Posts: 4
Rep Power: 0 sibelius7 is on a distinguished road
Re: Keypress question from a somewhat new Java programmer

Well that's helpful... I swear I looked in .awt.

Thanks a lot andro, now I have something to go with!

(that doesnt mean I dont need any more advice haha)
sibelius7 is offline   Reply With Quote
Old Dec 25th, 2007, 7:04 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
Re: Keypress question from a somewhat new Java programmer

Some people also prefer to use the KeyAdaptor class as it doesn't require you to implement all the methods in the KeyListener interface.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Dec 26th, 2007, 9:58 AM   #5
null_ptr0
11 years old
 
Join Date: Nov 2007
Posts: 79
Rep Power: 1 null_ptr0 is on a distinguished road
Re: Keypress question from a somewhat new Java programmer

Here's some pseudocode I whipped up:
import java.io.InputStream;

public class Test {
   private InputStream stdin = System.in;

   public char get() {
      return (char) stdin.read();
   }

   public void loop() {
      while(true)
         if(get() == 'a')
            launchEvents();
   }

   /* Implement your code here */
   protected void launchEvents() { }

   public static void main(String[] argv) {
      loop();
   }
}
That is, if your using the console.
If you are using a GUI, then search KeyListener, addKeyListener, and KeyEvent.
__________________
iload_0 iconst_1 ishl or
iload_0 iconst_2 idiv or
iload_0 iconst_2 iconst_1 imul idiv
[1] & [2] use the smallest stack size
null_ptr0 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sun Certified Java Programmer - is it worth it? funkey_monkey Java 10 Feb 22nd, 2007 3:48 PM
Java import.* question Lich Java 4 Nov 2nd, 2005 9:24 AM
quick question about java labels thenewkid Java 1 Sep 8th, 2005 1:52 AM
simple java question. f15h Java 4 May 17th, 2005 2:40 PM
Question about java massive-war Coder's Corner Lounge 3 Apr 14th, 2005 9:18 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC