title: What is a "hook" and how can I write one in Java? And how to communicate with kernel to know the keys pressed by the user/registering with OS
body: <p>Although I searched a lot, it is still unclear to me as to what exactly a "hook" is. For instance, I read this post on <a href="http://wiki.answers.com/Q/What_does_hook_mean_in_programming_language" rel="noreferrer">wiki answers</a> :</p>
  
  <blockquote>
    <p>A hook is a method of interposing a piece of code in front of another
    piece of code, so that the first piece of code executes before the
    second piece of code, giving the first piece of code an opportunity to
    monitor and/or filter the behavior of the second piece of code. An
    example might be a mouse hook, allowing the hook code to monitor the
    mouse while at the same time preserving the functionality of the
    original mouse event processing routine.</p>
  </blockquote>
  
  <p>I also read this <a href="https://stackoverflow.com/questions/467557/what-is-meant-by-the-term-hook-in-programming">post</a>, but I still don't understand what a "hook" exactly is. Can someone please explain, in layman terms, what is a "hook"? Why exactly do some write a "hook"? Also, is it possible to write a "hook" in Java?</p>
  
  <p><strong>Note:</strong> </p>
  
  <p>I wanted to write a keylogger in java and one of my friend said that you will have to write a "hook" in C. Can't I write the whole keylogger in Java (to be operated on windows only)?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong><em>please give an answer w.r.t keylogger.</em></strong> How can i ask <code>kernel</code> to give the information about the key pressed to my application using hooking ? Or how can i register my application with OS using JNI? I want my application to record keys pressed by the user.</p>