• 1

JAVA Swing: Binding Function Keys to a JButton


Question

6 answers to this question

Recommended Posts

  • 0
  MrStaticVoid said:
It's been a while since I've done Swing, but I suspect you'll want to add some sort of key listener to the root window to listen for certain key presses.  Upon the correct key press perform the correct handling code.

586414086[/snapback]

Thanks for your suggestion. I have tried using keyReleased on the JFrame but the event never seems to fire. Any other ideas?

  • 0

how about this:

import javax.swing.*;
import java.awt.event.*;

class TestFrame extends JFrame implements KeyListener
 ?{
  public TestFrame()
    {
    :    //other stuff here

    this.addKeyListener(this);    //need to make the Form listen for keys being typed, pressed, and released.

    :    //other stuff here
    }//end of default constructor
 ?public void keyPressed(KeyEvent e) 
 ? ?{
 ? ?switch(KeyEvent.getKeyText(e.getKeyCode()))
 ? ? ?{
 ? ? ?case 'F': case 'f': /*do stuff here */ break;
 ? ? ?:
 ? ? ?:    //add more cases as needed
 ? ? ?}//end of switch
 ? ?}//end of keyPressed()

 ?public void keyReleased(KeyEvent e) 
 ? ?{
 ? ?}//end of keyReleased()

 public void keyTyped(KeyEvent e) ?
 ? ?{
 ? ?}//end of keyTyped()

 ?}//end of class

hope this helps,

STV

Edited by STV
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.