Results 1 to 7 of 7
  1. #1
    pronetin is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default get user input from keyborad

    I want to create some short-cuts for my system. For example:

    If user press Ctrl+B and active windows on screen is not browser(for example user is in MyDocument folder or Home directory), browser will open.

    For doing it, I need to read keyboard input always and check it for short-cuts.

    How can i read keyboard input?
    Last edited by pronetin; 06-02-2010 at 11:59 AM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Go for Scanner and see the below doc
    Scanner (Java 2 Platform SE 5.0)
    Ramya:cool:

  3. #3
    pronetin is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Exclamation Scanner is not suitable!

    Scanner couldn't do that.
    I don't want to read System.in or Standard Input. I want to read keys that user enters via Keyboard.

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    then go with KeyListener and associated events
    Ramya:cool:

  5. #5
    pronetin is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    RamyaSivakanth, you don't understand my intention.
    KeyListener is not practical.

    I should can read keyboard IO and work with keyboard API.

    All java programmer, Is it possible?

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have written small program.
    In this program you can change Panel color to blue on two ways:
    1. Click on the ToolBar's button
    2. Press CTRL + B

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    
    import javax.swing.Action;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.ImageIcon;
    import javax.swing.InputMap;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    
    import javax.swing.JToolBar;
    
    public class ActionsExample {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				ActionFrame frame = new ActionFrame();
    				frame.showUI();
    			}
    		});
    	}
    }
    
    class ActionFrame extends JFrame {
    	private static final long serialVersionUID = 1L;
    	ActionPanel panel;
    	public ActionFrame() {
    		panel = new ActionPanel();
    		add(panel);
    	}
    	
    	public void showUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    class ActionPanel extends JPanel {
    	private static final long serialVersionUID = 1L;
    
    	Action colorAction;
    	JButton colorButton;
    	JToolBar colorToolBar;
    	
    	public ActionPanel() {
    		colorAction = new ColorAction("Color Action", null, "Change color", Color.BLUE);
    
    		colorButton = new JButton("Color Button");
    		colorButton.addActionListener(colorAction);
    		
    		colorToolBar = new JToolBar();
    		colorToolBar.add(colorButton);
    
    		add(colorToolBar, BorderLayout.PAGE_START);
    		
    	     InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    	     
    	     imap.put(KeyStroke.getKeyStroke("ctrl B"),"set Panel color: Blue");
    	     ActionMap amap = getActionMap();
    	     amap.put("set Panel color: Blue", colorAction);
    
    	}
    
    class ColorAction extends AbstractAction {
    	private static final long serialVersionUID = 1L;
    
    	public ColorAction(String text, ImageIcon icon,
                String desc, Color color) {
    		super(text, icon);
    		putValue(SHORT_DESCRIPTION, desc);
    		putValue("color", color);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		Color color = (Color)getValue("color");
    		setBackground(color);
    	}
    }
    }
    Last edited by cselic; 06-02-2010 at 02:12 PM. Reason: typos

Similar Threads

  1. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  2. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  3. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  4. who to take user input ,not by console...
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 10:09 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Tags for this Thread

Posting Permissions

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