aspose file tools*
The moose likes Threads and Synchronization and the fly likes Listening keyboard and mouse events in the background Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Listening keyboard and mouse events in the background" Watch "Listening keyboard and mouse events in the background" New topic
Author

Listening keyboard and mouse events in the background

Ahmet Aksoy
Ranch Hand

Joined: Apr 04, 2005
Posts: 30
Hi,
I have a dictionary program. It will run continuously in the background.
When the user clicks ctrl+rightMouseButton, it will select the word at the focus, and send it to the dictionary module.
In order to do that, I think I have to use several threads:
1- For the application waiting in the background
2- Listening for the keyboard events in general
3- Listening for the mouse events in general
4- Listening for the focusing events
5- Listening for the clipboard events (already running)

There will be at least 2 hotKeys: one for program activation, and one for the word selection.
When word selection key activated, I will send a double-click to the component at the focus in order to select the word at the focus, and then send a ctrl-c to copy the selected word to the clipboard.
How should I run those threads altogether?
While listening for the clipboard, there is a sleep(100) method. What does happen during that period? Does waiting affect only that thread, or the whole application? Will it affect the general performance of the application?
As you can see easily, I know very little about threads.
What can I do? What should I do?
Especially I need some simple samples.
Thanks in advance.
Ahmet Aksoy
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

wait only affects the thread that makes the call. I doubt if you need threading for your application though. How do you plan to interact with the target applicaiton? Are you planning on a totally seperate running program that will interact through the OS with the target application? If so you will probably be doing lots of JNI.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
I don't think you need your own threads for listeners on the keyboard/mouse events. The AWT engine(part of the JVM) normally handles that. Your code needs to add listeners for each of the type events you want to handle.
When your listener is called with an event, it can then start a thread to do the work necessary, freeing the AWT engine's thread.

?will select the word at the focus
Where are the words that are at the focus? Do you mean at the caret?
Focus is at a component level and a component such as a TextArea can contain many words.
Ahmet Aksoy
Ranch Hand

Joined: Apr 04, 2005
Posts: 30
Hi Mr. C Lamont Gilbert,
Here are my expectations: (now I'm talking about only the windows system)
My program will be waiting on the system tray until the user hits ctrl-right click. By using a focusListener, I hope to know the object which had focus when the mouse clicked, and the key pressed. After then I will send a double-click from my thread. Since there will be no visual part of the thread, there will be no change in the focus, and double clicking will make the word to be selected which was shown by the cursor. After then the thread will send a Ctrl-C in order to copy the selected word to the system clipboard.
When user presses ctrl-right-click, my thread will capture it, and change to a double-click, and then a ctrl-C.
My application can now listen to the clipboard events, and capture the copied text into it automatically. So, I will obtain the word which was under the cursor.
I don't know yet whether I can succeed that scenario or not?
I want to find some clues.
Ahmet Aksoy
Ahmet Aksoy
Ranch Hand

Joined: Apr 04, 2005
Posts: 30
Hi Norm Radder,

I'm not sure whether I must use threads for such a solution, or not.
However, in order to capture the strings copied to the clipboard, I had used threads, and I thought that I could use them for a more complicated problem also.
First, I will try to make a small listener program. It will listen to the keyboard, and the mouse and it will printout the location and the related values of them.
If I can succeed it, then I will try to select the words under the cursor, using ctrl-right click.
At the last step, I will try to copy the selected word to the clipboard.

There is another solution for my problem: "Character Recognition" from the screen. But that will be a far more difficult solution

Ahmet Aksoy
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

you probably dont need to send the double click. There is probably some other method of getting the word under the cursor. I know there are screen reading programs that are capable of doing it, and in USA OS makers are putting facilities into the OS in support of screen reading due to the Americans with Disabilities Act. So I would check the OS API to see what is available and check under accesibility.
Ahmet Aksoy
Ranch Hand

Joined: Apr 04, 2005
Posts: 30
You might be right!
I found some small clues for c# at http://www.codeproject.com/csharp/kbdListenerPackage.asp
I'll try to check WinAPI also.
Thanks.
Ahmet Aksoy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listening keyboard and mouse events in the background