Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: array of keyevents

  1. #1
    Insomniac.. is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default array of keyevents

    Hi.

    I'm wondering how to put these KeyEvents(2nd code example) combination into an array so that i don't have to declare them one by one
    like I did with these single key keyevents(1st code example)

    Java Code:
     for (index1 = 0; index1 < keyboardLetterStroke.length; index1++) 
            {                        
                textField.getInputMap().put( KeyStroke.getKeyStroke( keyboardLetterStroke[index1].toUpperCase() ), 
                        keyboardLetterStroke[index1] 
                                ); 
                textField.getActionMap().put( keyboardLetterStroke[index1], new AbstractAction() { 
                    @Override
                      
                    public void actionPerformed(ActionEvent e)  
                    {    
                        for(index2 = 0; index2 < letterString.length; index2++) 
                            if(e.getActionCommand().equals(letterString[index2])) 
                                break; 
                          
                        letters[index2].doClick(); 
                    } 
                } ); 
            }

    Java Code:
     textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey); 
              
            textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey); 
              
            textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey); 
              
            textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey); 
              
            textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey);
    Thanks.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: array of keyevents

    I'm not certain I follow you. Do you mean something like this?
    Java Code:
    int KeyEventsArray[] = {KeyEvent.VK_B, KeyEvent.VK_C, ...};
    //
    Regards,
    Jim
    Insomniac.. likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Insomniac.. is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: array of keyevents

    Yeah that's it, thanks!
    Java Code:
    shiftKey = new ShiftDown();
            for(int i = 0; i < KeyEventsArray.length; i++)
            textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEventsArray[i], InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey );

Similar Threads

  1. registering KeyEvents on a JComponent
    By yemista in forum AWT / Swing
    Replies: 2
    Last Post: 11-26-2012, 05:20 PM
  2. JScrollPane keyevents
    By Alkor in forum AWT / Swing
    Replies: 4
    Last Post: 03-27-2012, 03:09 AM
  3. JFrames and KeyEvents
    By CuddlyKittens11 in forum AWT / Swing
    Replies: 1
    Last Post: 05-05-2011, 12:58 AM
  4. Replies: 3
    Last Post: 02-02-2011, 10:53 AM
  5. Question about java keyevents?
    By Godsent in forum AWT / Swing
    Replies: 2
    Last Post: 10-26-2009, 09:38 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
  •