Results 1 to 12 of 12
  1. #1
    alexsb92 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Question Reading characters without showing up on the screen?

    Hey guys,

    I am trying to read a character in Java without having it show up on the screen. It is possible with the hsa class, but i absolutely hate ready to program, and I do not want to use it. Is there any way to do that with Standard Java libraries?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, this could be done in a Swing GUI, but if you don't know Swing, you have a bit to learn first before you can do this.

    Welcome to the forum by the way.

  3. #3
    alexsb92 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks Fubarable,

    Would you recommend any examples or tutorials that have what I am looking for in them? I'm not a beginner programmer, i just think its a beginner question and thus i posted it here.

    So there is no way to do this in the console? If not, what's the method called, and I'll look it up in the API.

    *If people didn't understand what I'm talking about, I am looking for the Java equivalent of readkey from Pascal.*

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think what you're trying to do is to read in what the user is typing without displaying this anywhere, correct? If so, you'd need to give your Swing application a KeyListener on a component that has focus or an application-global listener (I believe that this would involve using an AWTEventListener but must admit that I've never used one). The best tutorials for Swing are the Sun tutorials. This and the API usually will get you going in the right direction.

  5. #5
    alexsb92 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alexsb92 View Post
    Would this be what you are talking about?
    KeyListener (Java 2 Platform SE v1.4.2)
    Yes, that is one possible solution, and the tutorial you've linked to is good, but the API is a bit dated -- you'll want to use the 1.6 API instead.

  7. #7
    alexsb92 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    So I would be looking for when KeyPressed would happen from here? KeyListener (Java Platform SE 6)

    What exactly would be the difference between KeyPressed and KeyTyped?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alexsb92 View Post
    So I would be looking for when KeyPressed would happen from here? KeyListener (Java Platform SE 6)
    Yep.

    What exactly would be the difference between KeyPressed and KeyTyped?
    The API tells me to look into the KeyEvent API to learn the difference. There you'll find these constants:

    KEY_TYPED: The "key typed" event. This event is generated when a character is entered. In the simplest case, it is produced by a single key press. Often, however, characters are produced by series of key presses, and the mapping from key pressed events to key typed events may be many-to-one or many-to-many.

    KEY_PRESSED: The "key pressed" event. This event is generated when a key is pushed down.

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You'll want to read the link below, but basically keyTyped() is about characters and keyPressed() is about other keys (Shift, Ctrl, Alt, Function keys, arrow keys, etc.). That's a bit oversimplified, but it's a useful way to think about it.

    KeyEvent (Java Platform SE 6)

    -Gary-

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alexsb92 View Post
    I am trying to read a character in Java without having it show up on the screen. It is possible with the hsa class, but i absolutely hate ready to program, and I do not want to use it. Is there any way to do that with Standard Java libraries?
    And if you don't want to use Swing maybe the Console class can help you: it can read a line of characters without echoeing them.

    kind regards,

    Jos

  11. #11
    alexsb92 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    And if you don't want to use Swing maybe the Console class can help you: it can read a line of characters without echoeing them.

    kind regards,

    Jos
    So you are talking about the java.io.Console if I am not mistaken? Do you have a specific method in mind?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alexsb92 View Post
    So you are talking about the java.io.Console if I am not mistaken? Do you have a specific method in mind?
    I was thinking of the readPassword() method (read the API documentation) but you seem to want instantaneous response after pressing a single key. A Console can't do that; you either have to use Swing as other repliers have pointed out or you have to use a third party library or use JNI for that purpose.

    kind regards,

    Jos

Similar Threads

  1. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  2. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM
  3. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM
  4. Showing
    By bostonstate in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 07:49 PM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-07-2007, 11:54 PM

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
  •