CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2010
    Posts
    121

    Key binding - listening for 2 keys

    How do I develop my code to listen for F5 and F10, so if F5 is pressed run 'runSQL(conn, sqlStatement.getText().trim());' otherwise if F10 is pressed run 'runUpdateQuery(conn, sqlStatement.getText().trim());'

    Thanks

    Code:
    		AbstractAction action = new AbstractAction() {
    			public void actionPerformed(ActionEvent e) {
    				runSQL(conn, sqlStatement.getText().trim());
    			}
    		};
    
    		// F5 key bind to run the SQL
    		String keyStrokeAndKey = "F5";
    		KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
    		InputMap im = pnlMain
    				.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    		im.put(keyStroke, keyStrokeAndKey);
    		pnlMain.getActionMap().put(keyStrokeAndKey, action);

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Key binding - listening for 2 keys

    Here's a tutorial:

    How to Write a Key Listener

  3. #3
    Join Date
    Feb 2010
    Posts
    121

    Re: Key binding - listening for 2 keys

    Thank you - that's perfect!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured