File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we get the current key pressed by the user ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we get the current key pressed by the user ?" Watch "Can we get the current key pressed by the user ?" New topic
Author

Can we get the current key pressed by the user ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Is there any similar class in java like MouseInfo which can give us current keycode pressed by the user even when our application is out of focus?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

No, and for a good reason. It would allow you to catch any key press anywhere on the screen, in any application. So I could write a little application, trick you into running it, and then let it run in the background collecting all the passwords you type anywhere, sending them to me. You wouldn't want that, would you?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote:No, and for a good reason. It would allow you to catch any key press anywhere on the screen, in any application. So I could write a little application, trick you into running it, and then let it run in the background collecting all the passwords you type anywhere, sending them to me. You wouldn't want that, would you?
lol
First rob i dont play with this kind of toy , i m asking over here because its my requirement in application i m building , so jokes apart I have to write this in C or C++? or which Other OO language provide this benefits , by OO i mean it similar to java which handles GC and all and which is Object Oriented?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

I didn't say you are planning on doing something nasty like this, I just meant to illustrate why it's not easy.

As far as I know, there is only one way to do this - write your own keyboard driver. This should then intercept the pressed keys and delegate the actual work to the original keyboard driver. You will definitely have to use C or C++ (or some other language that allows you to do low-level programming) for that.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

In Windows an application can register a system-wide keyboard hook. It is probably a lot easier than writing a keyboard driver, but still the hook callback function must reside in an DLL. It's been ages since I've been doing this kind of things to in Windows, so the best I can offer is a MSDN link: SetWindowsHookEx.

Edit: of course, this kind of things is platform specific, so if you're not on Windows, this is not going to help. If you're on Windows, you'll need to write the DLL (in C or C++ perhaps), but you should then be able to use that code via JNI or JNA. It might be possible to call a Java method from the DLL, I believe, but I'm not sure on which thread the DLL will be called. You might want to store the keypresses in the DLL and use Java thread to call the DLL and obtain the keypressess once in a while.

If you just want to register a global shortcut key, there is a different (and much better) Windows function for that, that function even prevents application to register the same global shortcut.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
might be easier if you tell us why your app needs to listen for keystrokes.
e.g. if you just want to start the app with some hotkeys (regardless of which
app/window has focus), that's quite simple in windows.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

Martin Vajsar wrote:In Windows an application can register a system-wide keyboard hook. It is probably a lot easier than writing a keyboard driver, but still the hook callback function must reside in an DLL. It's been ages since I've been doing this kind of things to in Windows, so the best I can offer is a MSDN link: SetWindowsHookEx.

I knew of the function, but didn't know it could also be used to receive keyboard events. Thanks for the info
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi naved, I think this might be useful for you

http://code.google.com/p/jnativehook/


Love all, trust a few, do wrong to none.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we get the current key pressed by the user ?