-2

I wrote a keyboard recorder program in java. The problem I am facing is my program works when it is in front of all windows. I want it record keyboard activities when it is hidden or behind of other windows. How could I manage that? Thank you very much.

An Overflowed Stack
  • 334
  • 1
  • 5
  • 20
  • 3
    No, you cannot use core Java to create a key logger program. You will need to use JNI, JNA or another programming language to write this potentially nefarious program. – Hovercraft Full Of Eels Jan 23 '14 at 22:05
  • 1
    http://stackoverflow.com/questions/901224/listening-for-input-without-focus-in-java – Mark W Jan 23 '14 at 22:08
  • I'd recommend autoIT or AutoHotkey if you aim to do that on windows, there are great examples all over the internet. – Selim Jan 23 '14 at 22:10
  • @Selim: AutoIt and AutoHotKey are great for creating hot-keys, special key presses that when pressed call a function, but I don't think that they can be used by themselves for an OS keyboard hook which is what the OP needs. See Mark W's link for more on what is needed to solve this. Voting to close question as a duplicate. – Hovercraft Full Of Eels Jan 23 '14 at 22:12
  • @HovercraftFullOfEels I can't read that out of the question. I used AutoIT for a lot of games in the past and I can tell that it is capable of doing stuff on keypress-events even if it is not the focused window. – Selim Jan 23 '14 at 22:15
  • @Selim: the OP states that he was looking to extend hs "keyboard recorder program" so that it worked in the background. I've used AutoIt quite a bit as well to help automate a medical office work-flow, but I'm pretty sure that out of the box it does not have the capabilities that the OP is looking for. – Hovercraft Full Of Eels Jan 23 '14 at 22:18

2 Answers2

1

If you are doing this for windows you have 2 choices. A kernel filter driver will capture all keyboard input regardless of which window it is targeting. See this for sample code http://code.msdn.microsoft.com/windowshardware/Kbfiltr-WDF-Version-685ff5c4 Note that this driver may very well catch the keyboard generated key down key up codes which you would need to assemble into a full keystroke.

The other way to do this is through an OS hook into the message pump. See this for starters http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx

You will need to write native code for both of these styles of keyboard capture on Windows OS's. The java sandbox will prevent this on windows.

Himilou
  • 127
  • 10
0

In general, you can't. The operating system decides where keyboard and mouse events get sent, and yours doesn't send the ones you want to background applications.

You need to hook the operating system. Depending on your OS, there could be Java bindings for this. More likely, you would need to write a service or kernel extension using native code.

Nathaniel Waisbrot
  • 23,261
  • 7
  • 71
  • 99