Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to use KeyStroke class?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
Member
 
Join Date: Dec 2009
Posts: 59
How to use KeyStroke class?
  

Hi friend,

I have lots of queried regarding the classes of java programming language. One of them is 'KeyStroke class'. I am unable to understand the concept of 'KeyStroke class'. Even I don't know where to make use of 'KeyStroke class'. I have referred many advanced java book, but nothing was helpful. If you have knowledge about the 'KeyStroke class', then please let me aware about the same. Your help would be greatly appreciated.

Reply With Quote
  #2  
Old 12-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How to use KeyStroke class?

Hi,

A KeyStroke class represents a key action on the keyboard, or equivalent input device. KeyStrokes can correspond to only a press or release of a particular key, just as KEY_PRESSED and KEY_RELEASED KeyEvents do; alternately, they can correspond to typing a specific Java character, just as KEY_TYPED KeyEvents do. In all cases, KeyStrokes can specify modifiers (alt, shift, control, meta, or a combination thereof) which must be present during the action for an exact match.
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
Re: How to use KeyStroke class?

Hi friend, Pleas refer below example of 'KeyStroke class'. Which will show you the use of KeyStroke class methods:
Code:
import javax.swing.KeyStroke;

public class KeyTesterDemo {
  static class MyActionListener extends AbstractAction {
    MyActionListener(String s) {
      super(s);
    }

    public void actionPerformed(ActionEvent e) {
      System.out.println(getValue(Action.NAME));
    }
  }

  public static void main(String args[]) {
    String actionKey = "theAction";
    JFrame ft = new JFrame("Key Tester");
    JButton jb1 = new JButton("<html><center>B<br>Focused/Typed");
    Container panet = f.getContentPane();
    panet.add(jb1, BorderLayout.NORTH);
  
    stroke = KeyStroke.getKeyStroke("ctrl C");
    action = new MyActionListener("Action Didn't Happen");
    inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap1.put(stroke, actionKey);
    actionMap1 = jb2.getActionMap();
    actionMap1.put(actionKey, action);

    ft.setSize(400, 200);
    ft.show();
  }
}
Reply With Quote
  #4  
Old 12-02-2010
Member
 
Join Date: May 2008
Posts: 2,007
Re: How to use KeyStroke class?

Following are the methods of KeyStroke class, these methods have same name but with different parameters:
  • getKeyStrokeForEvent(KeyEvent anEvt)
  • getKeyStroke(String st)
  • getKeyStroke(int keyCde, int modfr, boolean onKeyRelease)
  • getKeyStroke(char keyChr, boolean onKeyRelease)
  • getKeyStroke(Character keyChr, int modfr)
Reply With Quote
  #5  
Old 12-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
Re: How to use KeyStroke class?

Below is the some part of java code. In this code I used the class 'KeyStroke' to find out the key pressed by user. I suggest you to study following code:
Code:
public static void main(String args[]) {
    String actionKey = "theAction";
    JFrame fv = new JFrame("Key Tester");
    JButton jb01 = new JButton("<html><center>B<br>Focused/Typed");
    JButton jb02 = new JButton("<html><center>Ctrl-C<br>Window/Pressed");
    JButton jb03 = new JButton("<html><center>Shift-D<br>Ancestor/Released");
    Container pane = f.getContentPane();
    pane.add(jb01, BorderLayout.NORTH);
    
    KeyStroke stroke = KeyStroke.getKeyStroke("typed G");
    
    Action action = new MyActionListener("Action Happened");
    inputMap.put(stroke, actionKey);
    ActionMap actionMap = jb01.getActionMap();
    actionMap.put(actionKey, action);
    fv.setSize(300, 200);
    fv.show();
  }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use KeyStroke class?"
Thread Thread Starter Forum Replies Last Post
Getting strange keystroke sound from Dell desktop computer Nabhoj Hardware Peripherals 1 06-02-2012 11:31 AM
Upgrade Sony Ericsson Xperia Play Micro SD card from class 2 8GB to class 4 32GB Aaryan2011 Portable Devices 8 08-10-2011 11:06 PM
keystroke errors in Sony Vaio VGN-AR190G GaganjyotTechie Portable Devices 4 30-05-2011 10:06 AM
Severe missing keystroke on internet explorer Qamar Technology & Internet 5 18-04-2011 08:14 AM
Unable to find Internet Explorer 8 keystroke Raju S Technology & Internet 4 06-02-2011 09:20 AM


All times are GMT +5.5. The time now is 08:00 PM.