Computer Science Canada

KeyListen (outputs keycodes&keytext)

Author:  the_short1 [ Thu Jun 01, 2006 10:42 am ]
Post subject:  KeyListen (outputs keycodes&keytext)

A simple program i made to give the keycode and keytext values (output sent to terminal window), as you type in the gui window.

I used it to give me the values for all of my Menu shortcut keys (eg. pressing ctrl+s to save, keycode=83, keyText=S), and special ones like keyCode=8 and keyText="Backspace" to backup a move in my game.

If you have any questions/comments just pm/reply.

-kevin

Author:  cool dude [ Thu Jun 01, 2006 4:38 pm ]
Post subject: 

this will probably sound stupid but i can't exit the program unless i go to task manager and end the task. how would i exit the program?

Author:  HellblazerX [ Thu Jun 01, 2006 4:45 pm ]
Post subject: 

Hmmmmm, looks like you forgot this:
code:

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Otherwise people would have to close whatever they're using to run the program, rather than the JFrame itself.

Author:  cool dude [ Thu Jun 01, 2006 4:53 pm ]
Post subject: 

for some reason its not working for me. it says cannot resolve symbol varaible jframe

Author:  HellblazerX [ Thu Jun 01, 2006 4:57 pm ]
Post subject: 

Ooooo, I see. He's using the old awt containers, not the new Swing ones. And it doesn't seem like the old containers have such a command. Looks like you'll have to add a WindowListener to close the program, but I can't be sure, seeing as I have never used them before.

Author:  [Gandalf] [ Thu Jun 01, 2006 5:09 pm ]
Post subject: 

Something like so should do:
Java:
addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

Though really, there is no reason for you not to be using JFrame instead... It only takes a few keystrokes to change.

Author:  the_short1 [ Thu Jun 01, 2006 6:30 pm ]
Post subject: 

ty gandalf for informing them how to close the awt window, but seing as i wanted to keep it simple i didnt include that.

To close the program, close the DOS window, and it will automatically close the GUI window Smile... (and this works for most if not all programs)

Author:  cool dude [ Thu Jun 01, 2006 7:42 pm ]
Post subject: 

but i'm not in DOS when i run it. i run it through Blue J and it won't close! thats why i posted for help on this question because at times i make an endless loop and i can't close the window.

Author:  the_short1 [ Thu Jun 01, 2006 9:53 pm ]
Post subject: 

ohh.. i use JCreator.. . what is blueJ? ive never heard of it.
hmm.. . well for my current game im working on i send a lot of debugging to the terminal (dos) window, why dont you see it ? what happens to all your System.out.println's?

Author:  cool dude [ Fri Jun 02, 2006 7:26 pm ]
Post subject: 

since i'm kinda new i don't know wat JCreator is and where to get it, but Blue J is wat most schools use (i think). it's really easy because all i have to do is write the code and then press the button compile to compile it and then i just run it by pressing a button. simple as that. instead of going to java typing the path and all that stuff. the system.out.println's just display in the terminal window and i never said i don't see them. your program works perfectly fine and i see it its just that i can't exit it but gandalf fixed that issue

Author:  the_short1 [ Sat Jun 03, 2006 10:58 am ]
Post subject: 

ahh just a misunderstanding then Smile .. our class commonly calls the terminal window the dos window.... JCreator is used by the whole algoma district school board, and it is probally really similar to BlueJ, http://www.jcreator.com/..

Im still curious: does closing the 'terminal' window in bluej close this program as well?

-kevin

Author:  cool dude [ Sat Jun 03, 2006 5:01 pm ]
Post subject: 

u can't close the program in Blue J by clicking on the "x" because it doesn't work! the only way is to end the program by having a command in your code i.e. type 0 to exit and in your code make it so that it exits. then u can click on the close button and it will close the running program.

Author:  cool dude [ Sat Jun 03, 2006 8:45 pm ]
Post subject: 

wow JCreator is so much better! thanks for the link Smile
just a quick question wat is the difference when u go to file and it says new file or new project?

Author:  the_short1 [ Sun Jun 04, 2006 12:15 pm ]
Post subject: 

well .. if your just making small programs.. go new file.. best feature of jcreator is the auto indent and auto brackets (which can be turned off in options), and nice syntax highlighting. ohh and the debugger is pretty good.

projects is for manipulating multiple source code files within one program.. eg. my pegged game, i have a main.java menu.java and verify.java, when i add all 3 to a project i can access methods from one another as if it was one big file.

no problem Smile

Author:  cool dude [ Sun Jun 04, 2006 12:21 pm ]
Post subject: 

also when i open a new file it doesn't automatically put the class name i.e.
code:

public class example


is there any way to make that automatic or do u have to type it every time because in BlueJ it was automatic. also when u say you could access the methods from one another as it was one big file do u have to write something in your code or it automatically accesses it?

Author:  wtd [ Sun Jun 04, 2006 12:27 pm ]
Post subject: 

cool dude wrote:
code:
public class example


Class names should always be capitalized (note: not the same as all-caps). Get into the habit early on and you'll save yourself a lot of grief.

Author:  the_short1 [ Sun Jun 04, 2006 9:25 pm ]
Post subject: 

well i think you can set up the templates somewhere in options to auto add it .. . but personally i have a file called template.java which has everything needed for a standard gui window and i just edit that each time.

As for the projects... example: i added Main.java and MenuTest.java to a project. I have a method called createMenu (), inside menutest, and im currently coding in main.java.. .this is what i write:

MenuTest.createMenu ();

and if i remember correctly, if you call methods this way, it is static, so method createMenu must be declared static (and all your variable inside createMenu as well).. optionally, you can create a object of the other class.. eg:
MenuTest Menu = new MenuTest ();
then just call Menu.createMenu (); .... but if you use a contructor for your GUI then initializing Menu = new MenuTest (); will open a new window..

Author:  wtd [ Sun Jun 04, 2006 11:13 pm ]
Post subject: 

Hmmm... "createMenu" sounds like something taken from Sun's tutorials.

I strongly urge you to write your own programs from scratch, rather than modifying existing source code. Copy and paste does not contribute to a fundamental understanding of the subject.

Author:  the_short1 [ Mon Jun 05, 2006 9:26 am ]
Post subject: 

[quote="wtd"]Hmmm... "createMenu" sounds like something taken from Sun's tutorials.[quote]

LOL! ... more like it sounds like something from my current program... i didnt copy and paste from anywhere... lol
[code="java"]
//snippet ..
setSize (maxx,maxy); // sets size of window
setTitle ("<Pegged> Level: "+pegLevel.substring(0,(pegLevel.length()-4))); // sets title of the window
setLayout (null); // sets no style, we do it ourselves
setBackground (Color.black); // sets background of the window to black
setMenuBar(Menu.createMenu()); // adds the menubar to the window
// Menu is a object of another file in my project...
[/code]

Author:  wtd [ Mon Jun 05, 2006 10:11 am ]
Post subject: 

Ah.


: