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?
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!? still interested to know if usb joypads can be used as input though.
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
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?
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
Here's a screenshot of the program running: 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 PLEASE help if you can
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
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)
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