-1

I want to add a KeyListener and have it only be attached to the program itself so that my program can take input without having any sort of window. I would like to have it run in the backround and change some of the functions of the keys. Any suggestion?

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
  • 1
    Your question makes no sense. A KeyListener needs to run in a Swing/AWT program. This program requires a container ("window") – Paul Samsotha Dec 22 '13 at 02:28
  • are you sure there is no way around? should i use something different then a keylistener? – user3126405 Dec 22 '13 at 02:35
  • Not exactly sure what you're trying to do. Do you mean you want the listener to be registered to a non GUI program? – Paul Samsotha Dec 22 '13 at 02:39
  • i want to have a java program running that adds function to the keyboard. I dont want it to be visible and i dont want it to ever lose focus, so that the added functions will work as long as the program is running. – user3126405 Dec 22 '13 at 02:43
  • possible duplicate of [React on global hotkey in a Java program on Windows/Linux/Mac?](http://stackoverflow.com/questions/79658/react-on-global-hotkey-in-a-java-program-on-windows-linux-mac) – Robin Green Dec 22 '13 at 12:55

2 Answers2

0

Try this out. I think you want to use key bindings and not a KeyListener. See the code beloww. I've binded the four arrow keys to different actions. Take a look at How to use Key Bindings

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class KeyBindings extends JPanel {
    public KeyBindings(){
        Action upAction = new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Down Arrow Pressed");
            }
        };
        Action downAction = new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e) {
               System.out.println("Down Arrow Pressed");
            }
        };
        Action leftAction = new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Left Arrow Pressed");
            }
        };
        Action rightAction = new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Right Arrow Pressed");
            }
        };


        getInputMap().put(KeyStroke.getKeyStroke("UP"), "upAction");
        getActionMap().put("upAction", upAction);
        getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "downAction");
        getActionMap().put("downAction", downAction);
        getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
        getActionMap().put("leftAction", leftAction);
        getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
        getActionMap().put("rightAction", rightAction);
    }

    public Dimension getPreferredSize(){
        return new Dimension(300, 300);
    }

    public static void createAndShowGui(){
        JFrame frame = new JFrame();
        frame.add(new KeyBindings());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                createAndShowGui();
            }
        });
    }
}
Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
0

You will have to make your App a Service which will run in the background automatically, click here for help

Community
  • 1
  • 1
Muhammad
  • 4,673
  • 3
  • 34
  • 48