Results 1 to 7 of 7

Thread: Keystroke Logging

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Exclamation Keystroke Logging

    I am trying to write a program that will sit inthe system tray and log peoples keystrokes to a text file. It has to run in the background. Is there an easy way to determine which keys a user has pressed, and if so how would i go about writing such a piece of code?

    Cheers in Advance

    Andy

  2. #2
    VirtuallyVB
    Guest

    Lightbulb Unusual for Microsoft

    Visual Studio had a working sample in C++ relating to "HOOKS" which can log keystrokes and mouse input. Shockingly, the actual sample compiled and functioned as documented (highly unusual for my experience with microsoft advanced concepts).

    I am unfamiliar if Java can do this without making a native call to such a method as that produced in C++. Perhaps the reflective classes may help, but I am guessing.

    Bring the correct tool to the task; I believe it is C/C++ in this case.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yeah that's a hard one. I looked and looked but i see nothing in Java that would enable you to trap non predifined keystrokes.

    There is a KeyStroke class and a KeyMap interface. But all they enable you to do is assocate actions with particular events through a KeyMap class.


    If you figure it out clue us in

  4. #4

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    would a hook really be the correct term? I think really want you need is to make an the api calls in c to get windows message pump to have it pump the keystrokes to your app as well as whatever app has focus. I have done this in c,not that tough, now in java that can be a whole new monster, I think you will have to look at Jini to make calls to a native thing like this.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I finally found the answer to this one after wanting to do the samething myself. But if i left somthing out please forgive me it's
    2:56 in the morning.

    What you want to use is the KeyEvent class which is a subclass of InputEvent: Take a look......

    Code:
                            java.lang.Object 
                                      |
                                      |
                         java.util.EventObject
                                      |
                                      |
                         java.awt.AWTEvent
                                      |
                                      |
                       java.awt.event (Abstract)
                                      |
                                      |
                         InputEvent (Abstract)
                                      |
                                      |
                      __________________
                            |                     |
                            |                     |
                       KeyEvent     MouseEvent
    yes yes i know i left out a host of other classes but....

    Here is a short description of the KeyEvent class.

    "This event is generated when the user presses or releases a key, or does bolth (ie types a character) These situations are characterized by the following constants in the KeyEvent class"

    public static int KEY_PRESSED
    public static int KEY_RELEASED
    public static int KEY_TYPED

    The last one is what you want.

    "This event is delivered when a character has been typed on the keyboard ie.... a key has been pressed (KEY_PRESSED) and then released (KEY_RELEASED) to signify typing a character.

    There are two methods that are defined in the KeyEvent class.....

    int getkeyCode()

    For KEY_PRESSED or KEY_RELEASED events, this method can be used to get the integer key-code associated with the key. The key codes are defined as constants in the KeyEvent.

    char getKeyChar()

    For KEY_TYPED events, the method can be used to get the Unicode character theat results from hitting a key.

    If you want you can also (which i think is cool) log the time the user pressed the key. By using the getWhen() method inherited from the parent class InputEvent.

  7. #7

Posting Permissions

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



Click Here to Expand Forum to Full Width