![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Location: Oregon, United States
Posts: 4
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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/ |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2007
Location: Oregon, United States
Posts: 4
Rep Power: 0
![]() |
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) |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 747
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#5 |
|
11 years old
Join Date: Nov 2007
Posts: 79
Rep Power: 1
![]() |
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();
}
}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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |