Results 1 to 12 of 12
Thread: Key Detection
- 06-16-2010, 05:59 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 23
- Rep Power
- 0
Key Detection
Hey
i wrote a keylistener class
public class Keys implements KeyListener {
public static void main(String[] args) {
}
public void keyPressed(KeyEvent e) {
System.out.println("FUNC RUNS");
if (e.getKeyCode() == KeyEvent.VK_NUMPAD1){
System.out.println("1");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
but when i press a key 1 on the numpad it doesn't print out anything, i am making a calculator app, any idea?
- 06-16-2010, 09:38 AM #2
Is this your whole program?
You haven't associated the keylistner to any component here.
- 06-16-2010, 09:49 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 23
- Rep Power
- 0
no that's just a class i made for the keylisteners. How do i associate a keyListener with a button?
- 06-16-2010, 09:51 AM #4
if the button is in this class, then write....
button1.addKeyListener(this);
where button1 is the button name
- 06-16-2010, 10:00 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 23
- Rep Power
- 0
i have around 15 buttons and they are all in the main class (not this class), how would i reference each button with each key and also when the key is pressed make the same function run as assigned to the button?
- 06-16-2010, 10:16 AM #6
you can make an instance of this class in your main class and then add Keylisteners to all the buttons.....
or you can totally scrape this class and implement the keylistener to the main class itself like this
Either you can do this and write each associated keys to perform the function of the specified buttonJava Code:public class MainClass { public static void main(String args[]) throws Exception { JButton[] but=new JButton[15]; for(int i=0;i<15;i++){ but[i]=new JButton(); } // KeyListener KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { printIt("Pressed", keyEvent); } public void keyReleased(KeyEvent keyEvent) { printIt("Released", keyEvent); } public void keyTyped(KeyEvent keyEvent) { printIt("Typed", keyEvent); } private void printIt(String title, KeyEvent keyEvent) { int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode); System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar()); } }; for(int i=0;i<but.length;i++){ but[i].addKeyListener(keyListener); } } }
Or, you can implement keyBindings using InputMap for this..... These tutorials might help you :
InputMap (Java Platform SE 6)
How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
Hope that helps.
- 06-16-2010, 07:39 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
- 06-16-2010, 09:51 PM #8
Why not have all the methods use println() to see what is happening?public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
- 06-16-2010, 10:23 PM #9
Wouldn't it make more sense to implement the KeyListener to a textfield instead of a button since buttons can't type?
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-17-2010, 12:12 AM #10
The component receiving key strokes has to have the focus? Read the KeyEvent doc.
- 06-17-2010, 02:34 AM #11
So clicking a button releases a KeyListener action?
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-17-2010, 02:38 AM #12
Similar Threads
-
Java keypress detection help
By pghazanfari in forum Advanced JavaReplies: 1Last Post: 05-29-2010, 08:30 PM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM -
help on motion detection
By MarkWilson in forum Advanced JavaReplies: 4Last Post: 12-07-2009, 06:45 AM -
request detection
By mtyoung in forum Advanced JavaReplies: 6Last Post: 02-05-2009, 03:46 AM -
USB Detection
By alanixu in forum New To JavaReplies: 3Last Post: 11-12-2008, 04:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks