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

Thread: Unresponsive keybindings

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

    Default Unresponsive keybindings

    Hi.

    I'm making a typing tutor, that gives you sentences you have to type and highlights the buttons you need to click.

    I'm assigning this keybinding to action ShiftDown for writing capital letters

    Java Code:
    textField.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.SHIFT_DOWN_MASK, true), 
                    "shift");        
            textField.getActionMap().put( "shift", shiftKey = new ShiftDown());
    However this code doesn't register very fast touch to shift + keys, where the operating system registers that you typed both keys, but java doesn't

    here's an example:

    pic 1 is supposed to highlight the next button "o" as shown in pic 2 because F is the correct letter, however i typed it too fast for java to register but the operating system registers it, as you can see the F is a capital letter. The second picture i typed the f + shift slower and it worked.







    You guys got any idea how to make java keybindings as responsive as the os?
    Thanks.
    Last edited by Insomniac..; 07-20-2013 at 09:15 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,336
    Rep Power
    5

    Default Re: Unresponsive keybindings

    You haven't really shown enough code to answer this question. But I find it difficult to believe that Java can't keep up with the typing. I guess it depends on how you are processing the key events.

    Regards,
    Jim
    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: Unresponsive keybindings

    Sorry about that.

    I dumbed down the code i was having the problem with, but i still have the problem.
    Here's the dumbed down version:

    Java Code:
    package eloelo;
    
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;  
    import java.awt.event.*;
    
    public class Keybindings extends JFrame
    {	
    	private JPanel contentPane;
    	private JTextField textField;
    
    	public Keybindings()
    	{
    		contentPane = new JPanel(); 
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
            setContentPane(contentPane);    
            contentPane.setLayout(null); 
            
            textField = new JTextField();
            textField.setBounds(50, 50, 100, 100);
            contentPane.add(textField);
    		        
            textField.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_A, InputEvent.SHIFT_DOWN_MASK, true ), 
                    "capital a pressed"
                    );       
            textField.getActionMap().put( "capital a pressed", new AbstractAction() { 
                @Override
                
                public void actionPerformed(ActionEvent e)  
                {    
                	System.out.println("Capital A");
                } 
            } );  
            
            textField.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_A, 0), 
                    "a pressed"
                    );       
            textField.getActionMap().put( "a pressed", new AbstractAction() { 
                @Override
                
                public void actionPerformed(ActionEvent e)  
                {    
                	System.out.println("Lowercase a");
                } 
            } );
        }    
    
    
    
    	public static void main(String args[])
    	{
    	Keybindings frame = new Keybindings(); 
    		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		frame.setSize( 400, 400 );
    		frame.setVisible( true );
    		
    	}
    }
    If I click shift + a very briefly, it doesn't print "Uppercase A", i doesn't print anything even though in the textfield i see a capital A.

    I guess I'm using an ineffective approach of binding keys to actions
    Last edited by Insomniac..; 07-21-2013 at 12:49 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,336
    Rep Power
    5

    Default Re: Unresponsive keybindings

    Try the following instead. You will have to handle special characters yourself like backspace in the KeyListener handler. For more information on this check out KeyListeners in the tutorial link in my signature.

    Regards,
    Jim

    Java Code:
    public class Keybindings extends JFrame implements KeyListener
    {  
        private JPanel contentPane;
        private JTextField textField;
     
        public Keybindings()
        {
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);   
            contentPane.setLayout(null);
             
            textField = new JTextField();
            textField.setBounds(50, 50, 100, 100);
            contentPane.add(textField);
                     textField.addKeyListener(this);
            
        }   
     
     
     
        public static void main(String args[])
        {
        Keybindings frame = new Keybindings();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setSize( 400, 400 );
            frame.setVisible( true );
        }
       @Override
       public void keyTyped(KeyEvent e) {
          System.out.print(e.getKeyChar());
       }
    
       @Override
       public void keyPressed(KeyEvent e) {
       }
    
       @Override
       public void keyReleased(KeyEvent e) {
       }
    }
    Insomniac.. likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Unresponsive keybindings

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Unresponsive keybindings

    Yeah that worked, the program is 100% responsive now. Thanks for answering both of my questions!

Similar Threads

  1. Swing button unresponsive
    By Orlan of Solace in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2013, 03:16 AM
  2. keybindings
    By jsobel in forum New To Java
    Replies: 8
    Last Post: 04-03-2012, 04:41 PM
  3. Replies: 2
    Last Post: 11-06-2009, 02:56 PM
  4. [SOLVED] Gui becomes unresponsive!
    By FezKazi in forum AWT / Swing
    Replies: 10
    Last Post: 03-04-2009, 04:47 AM
  5. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 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
  •