Development Java : keyboard listener and opening exe's

Discussion in 'Software' started by MrDT, 6 Aug 2004.

  1. MrDT

    MrDT fapfap

    Joined:
    14 Jun 2002
    Posts:
    2,557
    Likes Received:
    0
    Ok, so this is a bit lazy of me, considering i haven't really scoured the api or anything yet, but i'm having a couple of problems with a program i'm writing.

    Basically, i want to 'listen' to keypresses constantly. This must be fairly easy to do as pretty much every game uses such a system. For example if the 'a' key is pressed i would like to fire an event, without waiting for the return key to be pressed.

    The second problem i'm having is how to 'shortcut' from a program. For example, if i press the 'a' key, i want 'c:/programs/a.exe' to run. Can this be done through java?

    Thanks in advance for any pearls of wisdom you guys may well bestow upon me :)


    EDIT: Nearly forgot, if you find the keyboard listener a little too easy, how about input from a usb gamepad? Could my java program react to say pressing 'left' on the joypad?
     
    Last edited: 6 Aug 2004
  2. MrDT

    MrDT fapfap

    Joined:
    14 Jun 2002
    Posts:
    2,557
    Likes Received:
    0
    ok, i've solved the keylistener problem myself.

    edit: i've also worked out how to open exe's. the code follows if anyone's interested:
    Code:
    import java.io.*;
    
    public class Shortcut
    {
    	public static void main (String args[]) throws IOException
    	{
    		Process p = Runtime.getRuntime().exec("M:/putty.exe");
    	}
    }
    how bloody simple is that!? :worried: :lol:



    still interested to know if usb joypads can be used as input though.
     
    Last edited: 6 Aug 2004
  3. woodshop

    woodshop UnSeenly

    Joined:
    14 Oct 2003
    Posts:
    1,408
    Likes Received:
    8
    well if you used the keypressed method to get your key presses i'm assuming you did.
    Then it's up to the joypad softwear since most of the software just does keyboard key emulation i see know reasion whay it wouldn't work.
    Heck i once configed my P3000 or control my mouse in windows. but thats another story
     
  4. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    If you're making your JAVA windows VM specific (which it looks like you have) then you could go the whole hog and use the USB APIs.

    Its all in MSDN.

    However i seam to recall there been a joystick object?
     
  5. MrDT

    MrDT fapfap

    Joined:
    14 Jun 2002
    Posts:
    2,557
    Likes Received:
    0
    I've decided to stick to keyboard only but thanks for your help :)

    I've all but finished the program, but have hit upon a stupid little problem.

    The program is basically going to be a frontend program for a home entertainment type thing i'm building. Custom controls will be interfaced to the keyboard inputs w,a,s,d,1 (up down left right and select) meaning that a real keyboard and mouse won't be required to operate it.

    This program will be launched at startup. The background image will be a 1024x768 image split into quarters each of which will have an image associated with the app it will launch. Everything is working fine, but i'm having trouble with the 'highlight' aspect.

    Whichever app is selected should be shown as such to the user. My original idea was to use rectangle2d to draw a thick red box around the selected app (1/2/3/4). I'm having trouble getting this to work though, as drawing a panel on top of my existing background panel messes it all up :(

    Here's my existing code:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class Frontend implements KeyListener
    {
    	public int selectedItem = 1;
    	String programPath;
    	// *** global variables ***
    
    	public static void main (String[] args)
    	{
    		Frontend frontend = new Frontend();
    		// *** instantiate Frontend object ***
    		frontend.drawMenu();
    	}
    	
    	public void drawMenu()
    	{
    		JFrame frame = new JFrame("Select the application you wish to use:");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		// *** create the window ***
    
    		ImageIcon image = new ImageIcon("image.jpg");
    		// *** specify image file for background ***
    
    		JLabel backgroundImage = new JLabel(image);
    		backgroundImage.setPreferredSize(new Dimension(1024, 768));
    		frame.getContentPane().add(backgroundImage);
    		// *** draw background ***
    
    		backgroundImage.setFocusable(true);
    		backgroundImage.addKeyListener(this);
    		// *** attach key listener ***
    
    		frame.pack();
    		frame.setVisible(true);
    		// *** make the window visible ***
    	}
    
    	public void keyTyped(KeyEvent e)
    	{
    		// ***take appropriate action when directional/select buttons (w,a,s,d and 1) are pressed
    
    		char c = e.getKeyChar();
    		
    		if (c == 'd')
    		{
    			moveHorizontal();
    		}
    		else if (c == 'a')
    		{
    			moveHorizontal();
    		}
    		else if (c == 'w')
    		{
    			moveVertical();
    		}
    		else if (c == 's')
    		{
    			moveVertical();
    		}
    		else if (c == '1')
    		{
    			if (selectedItem == 1)
    			{
    				programPath = "C:/1";
    			}
    			else if (selectedItem == 2)
    			{
    				programPath = "C:/2";
    			}
    			else if (selectedItem == 3)
    			{
    				programPath = "C:/3";
    			}
    			else
    			{
    				programPath = "C:/4";
    			}
    			openApplication();
    		}
    		else
    		{
    			// *** do nothing if an invalid input is received ***
    		}
    	}
    
    
    	public void keyPressed(KeyEvent e)
    	{
    		// *** do nothing ***
    	}
    
    	public void keyReleased(KeyEvent e)
    	{
    		// *** do nothing ***
    	}
    
    	public void moveHorizontal()
    	{
    		switch (selectedItem)
    		{
    			case 1:  selectedItem = 2; break;
    			case 2:  selectedItem = 1; break;
    			case 3:  selectedItem = 4; break;
    			case 4:  selectedItem = 3; break;
    		}
    		// *** move focus to new selection if a horizontal keypress is detected ***
    	}
    
    	public void moveVertical()
    	{
    		switch (selectedItem)
    		{
    			case 1:  selectedItem = 3; break;
    			case 2:  selectedItem = 4; break;
    			case 3:  selectedItem = 1; break;
    			case 4:  selectedItem = 2; break;
    		}
    		// *** move focus to new selection if a vertical keypress is detected ***
    	}
    
    	public void openApplication()
    	{
    		try
    		{
    			System.out.println("Open "+programPath);
    			Process p = Runtime.getRuntime().exec(programPath);
    		}
    		catch(IOException e)
    		{
    			// *** this exception will never happen if paths are defined correctly ***
    		}
    	}
    }
    If anyone would be so kind as to compile and run this and maybe suggest someway to implement the highlighting i'd be very grateful. code snippets would be even better ;)

    Thanks in advance

    NB: 'EXIT_ON_CLOS E' should be 'EXIT_ON_CLOSE' vbulletin seems to muck it up for some reason?? Thanks to woodshop for pointing that out ;)
     
    Last edited: 9 Aug 2004
  6. MrDT

    MrDT fapfap

    Joined:
    14 Jun 2002
    Posts:
    2,557
    Likes Received:
    0
    Here's a screenshot of the program running:

    [​IMG]

    The green rectangle is actually drawn onto the backround image atm to indicate how it should look.

    Pressing w,a,s,d should move the rectangle to the appropriate option. I know how i'd go about this if only i could actually draw the rectangle in the first place :grr:

    PLEASE help if you can :wallbash:
     
  7. MrDT

    MrDT fapfap

    Joined:
    14 Jun 2002
    Posts:
    2,557
    Likes Received:
    0
    Hrmph, one last bump before i scrap my program completely and start again from scratch.

    Does ANYONE know how i could draw a rectangle on top of the already displayed jpg (which is an ImagIcon attached to a JLabel)!?

    It can't be that difficult surely?

    HAAAAAAAAAAAYYYYYYYYYYYYLLLLLLLP!!!!!!!!!!

    //breaks down in uncontrollable sobbing fit
    :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :waah: :wallbash:
     
  8. woodshop

    woodshop UnSeenly

    Joined:
    14 Oct 2003
    Posts:
    1,408
    Likes Received:
    8
    why not try making the triangle in say photoshop as a gif or png with transparent center and then jut move that picture arountd on top of the other pictures

    [edit err i mean Rectangle.[/edit]
    [edit2] where are you?? not on IRC i know i'v got an example progie for you :)[/edit] (i think)
     
  9. MrDT

    MrDT fapfap

    Joined:
    14 Jun 2002
    Posts:
    2,557
    Likes Received:
    0
    I have 2 hours 49 mins to complete my lab resit paper, so downloading mirc onto the library computers mightn't be the brightest of ideas right now ;)

    Maybe you could cut and paste the code into this thread so i can see?

    //i can see light at the end of the tunnel :)
     

Share This Page