Click to See Complete Forum and Search --> : Keystroke Logging
Andy_Hollywood
Jun 5th, 2001, 03:51 AM
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
VirtuallyVB
Jun 5th, 2001, 07:07 PM
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.
Dillinger4
Jun 8th, 2001, 12:10 PM
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 :)
Dillinger4
Jun 8th, 2001, 12:15 PM
Someone else ask the same quesiton and i think this would
put you on the right track. It envolves a hook as VirtuallyVB pointed out.
http://161.58.186.97/showthread.php?s=&threadid=81490
billrogers
Jun 11th, 2001, 08:56 AM
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.
Dillinger4
Aug 23rd, 2001, 01:57 AM
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. :o
What you want to use is the KeyEvent class which is a subclass of InputEvent: Take a look......
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.
Dillinger4
Aug 24th, 2001, 01:45 AM
Hey Vert what do you think?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.