• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can we get the current key pressed by the user ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi naved, I think this might be useful for you

http://code.google.com/p/jnativehook/
 
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic