Results 1 to 5 of 5

Thread: How to use KeyStroke class?

  1. #1
    Join Date
    Dec 2009

    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.

  2. #2
    Join Date
    Jan 2008

    Re: How to use KeyStroke class?


    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.

  3. #3
    Join Date
    Apr 2008

    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:
    import javax.swing.KeyStroke;
    public class KeyTesterDemo {
      static class MyActionListener extends AbstractAction {
        MyActionListener(String s) {
        public void actionPerformed(ActionEvent e) {
      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);;

  4. #4
    Join Date
    May 2008

    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)

  5. #5
    Join Date
    Apr 2008

    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:
    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);;

Similar Threads

  1. Getting strange keystroke sound from Dell desktop computer
    By Nabhoj in forum Hardware Peripherals
    Replies: 1
    Last Post: 06-02-2012, 11:31 AM
  2. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  3. keystroke errors in Sony Vaio VGN-AR190G
    By GaganjyotTechie in forum Portable Devices
    Replies: 4
    Last Post: 30-05-2011, 10:06 AM
  4. Severe missing keystroke on internet explorer
    By Qamar in forum Technology & Internet
    Replies: 5
    Last Post: 18-04-2011, 08:14 AM
  5. Unable to find Internet Explorer 8 keystroke
    By Raju S in forum Technology & Internet
    Replies: 4
    Last Post: 06-02-2011, 09:20 AM

Posting Permissions

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