ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm writting a small program for my university research, and need to capture user keystrokes (already taking screen shots and tracking mouse location). I'm working in Java 1.5 Beta but any pointers would be appreachiated
OK, i need to capture user keystrokes, I was figuring something like System.in could be used but i don't know how to make it capture keystrokes outside of my program ... ie, which input stream to use. Also, i need to do this without interference to the user, so the keystrokes need to be passed forward (or not intercepted) to the program the user is passing them to.
While we are at it, i also need to capture mouse clicks (left/right/double) when they occur.
Any ideas or pointers would be appreachiated
Additional (edit)
i had been thinking that it's a simple matter of changing System.in to something more global, but that doesn't seem to be it.
I turned up something interesting though, Sockets.
Is it possible to connect a socket to the Xserver to monitor keystrokes.
Socket echoSocket = new Socket(InetAddress address, int port);
where the address is my local system/the local Xserver, and the port is the appropriate Xserver port ...
I don't think it is possible using the docuemented Java classes, you could look at the undocuemented classes in the sun.* packages. As for connecting with a socket, if possible, you would have to worry about implementing the X protocol and all, something Xlib already does (and Java probably uses it under the hood (sun.*)). Plus both of those options are not portable.
if im not mistaken, i believe you can create different listeners that will capture keyboard events. Given that say an "h" is pressed, you could store it in an ArrayList, where you can later display the entire arraylist to the screen or write it to a file. I also know there is a similar way of capturing which specific mouse buttons are clicked, or which direction the wheel is scrolled.
The only problem is I am not sure if you can do this without the java program being ran in the forground with the main focus to the user....
I know I am being broad, but it's the best I can do without spending sometime with the API
Distribution: Slackware Current, RedHat (RHEL, FC, CentOS), openSuSE, Mac OS X
Posts: 617
Thanked: 0
Original Poster
Incidentally,
I fixed it like so:
I grabbed uberkey 1.2 and get it to pipe it's output to a file, i get my java program to observe the file, and as the file changes it extracts the data.
(i modified ueberkey a little so that my program can extract a little more meaningful data).
I checked the Global Hot Key Listerener, noted by aaa ... alas that only works if the program has focus. Never the less, its a very interesting and useful bit of code, so thanks for that.
Still can't capture the mouse clicks ... but there should be a way
Not sure how "Cross platform" you need this app to be but if you are running some *NIX you could parse the data comming from /dev/mouse, /dev/psaux or /dev/gpmdata .
Distribution: Slackware Current, RedHat (RHEL, FC, CentOS), openSuSE, Mac OS X
Posts: 617
Thanked: 0
Original Poster
Ahhh ... Interesting I had been considering it, but since no one mentioned that (anywhere), i figured that wasn't a possibility (should have just run cat /dev/psaux i guess ).
Well, it would certainly kill platform independence, but it would give valuable info, in particular mouse clicks ... (since i already jave mouse location).
hmm ... thanks for that, i might have to include that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.