• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching specific keystroke  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I am developing a very small personal project to help me tally up some work related data (db problems solved, time spent debugging a particular piece of code, etc). Anyway, i need to catch certain combination of keystrokes (mainly ctrl+ a numpad key) and increase the counter for the corresponding task, or start/stop the timer.
I know how to grab keystrokes inside my own applications and recognize them, but have no idea how to do that from outside the application in focus.
So my question is 2-fold:
1) How to grab keystrokes when the application is not the one on focus
2) Is there a way to have the event listener only being fired when the key combinations i'm interested are pressed?

A google search gave me the answer in a restrict number of sites saying that it is not possible in java, but i would like to know it from a more trustworthy site like this. If i can't do it in java guess i'll have to do in c#...
Thank you in advance for the help
 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Not possibe with pure Java. Look at JNI.
2. Yes.


I made this at work, so it might be a little off or whatever. This is the general idea, so hopefully you get it. Research keyAdapter, keyListener, etc...

Cheers,
cc11rocks

EDIT : Also, research KeyEvent : http://download.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html
 
Daniel Marti
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the fast answer.
I know about key events, what i meant was, instead of being me checking each time a key is pressed, i would only receive the event if the keys i'm interested in are pressed (i am afraid of slightly lagging my typing).
Anyway i'll check JNI and see what i can get from that.
Thanks again.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. And just FYI, Java is very fast. I have had no issues, performance-wise. The only issue I see is start-up time.
cc11rocks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!