Results 1 to 12 of 12

Thread: Key Detection

  1. #1
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default 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?

  2. #2
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Is this your whole program?
    You haven't associated the keylistner to any component here.

  3. #3
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    no that's just a class i made for the keylisteners. How do i associate a keyListener with a button?

  4. #4
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    if the button is in this class, then write....

    button1.addKeyListener(this);

    where button1 is the button name

  5. #5
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    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?

  6. #6
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    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

    Java 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);
        }   
      }
    }
    Either you can do this and write each associated keys to perform the function of the specified button

    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.

  7. #7
    tintin is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by dilpreet28 View Post
    Hey

    i wrote a keylistener class

    Java Code:
    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?
    It is easy... Do like this

    Java Code:
    button.addKeyListener(new Keys());
    Don't forget to +1 my REP

    Good luck :)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }
    Why not have all the methods use println() to see what is happening?

  9. #9
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    The component receiving key strokes has to have the focus? Read the KeyEvent doc.

  11. #11
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

Similar Threads

  1. Java keypress detection help
    By pghazanfari in forum Advanced Java
    Replies: 1
    Last Post: 05-29-2010, 08:30 PM
  2. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 06:13 PM
  3. help on motion detection
    By MarkWilson in forum Advanced Java
    Replies: 4
    Last Post: 12-07-2009, 06:45 AM
  4. request detection
    By mtyoung in forum Advanced Java
    Replies: 6
    Last Post: 02-05-2009, 03:46 AM
  5. USB Detection
    By alanixu in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •