Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: How to create a system wide mouse or keyboard hook?

  1. #1
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default How to create a system wide mouse or keyboard hook?

    Does anyone know of an easy way of creating a system wide mouse or keyboard hook other than me writing it in C/C++ then implementing JNI?

    Cause that seems like a lot of work just for a global hook :/

    Thanks,
    Chris


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: System Wide Mouse/Keyboard Hook

    Morning Chris,

    What do you mean by a system wide mouse or keyboard hook?
    Could you please give me an example of what you are tying to archive?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    A mouse hook for example tracks all the mouse events. System wide because I don't want to limit it to say my application, such as a MouseListener would do...if that makes sense?

    Chris

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Cool Re: System Wide Mouse/Keyboard Hook

    Quote Originally Posted by Freaky Chris View Post
    A mouse hook for example tracks all the mouse events. System wide because I don't want to limit it to say my application, such as a MouseListener would do...if that makes sense?

    Chris
    OK yes I know what you mean. I was looking to do something similar myself actually. I'll look into it asap and post back if I find a solution!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    Well I have looked to no avail. When I get home I might have a look into writing a C Win32 Hook and linking with JNI. Once I've done that I'll make it all readily avaliable to everyone who wants it. Since it is quite common.

    Chris

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Thumbs up Re: System Wide Mouse/Keyboard Hook

    Quote Originally Posted by Freaky Chris View Post
    Well I have looked to no avail. When I get home I might have a look into writing a C Win32 Hook and linking with JNI. Once I've done that I'll make it all readily avaliable to everyone who wants it. Since it is quite common.

    Chris
    Yes when I looked into it before I couldn't find anything either. Your C Win32 hook idea sounds good.. I think it would be very useful! When its complete, let me know and i'll upload it to our server for our members to download.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    Hey JavaPF. I've tried to get started, I have developed the basic hook in C using Win32, however I need to get a quick understanding of JNI. When I use
    javah -jni -classpath <class location> <class>

    It seems to run flawlessly but does not produce the required .h file. This is my main problem at the moment, once I have got that then I need to try and get C++ to compile down to a god damn DLL, which is proving more work than expected

    Anyhelp with the JNI header generation would be great.

    Thanks,
    Chris

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: System Wide Mouse/Keyboard Hook

    Hey Chris,

    I have very little experience with JNI. Do you use the Eclipse IDE?

    This looks useful:

    http://www.cs.umanitoba.ca/~eclipse/8-JNI.pdf
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    I haven't found a method of executing custom commands from with the Eclipse IDE or NetBeans, So i was using the commandline. That link you gave appears to point to thin air hehe, so it's not overly useful. Maybe it's just college Filters/Blocks/Proxies messing with it, so i'll try when I get home.

    Thanks very much though,
    Chris

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: System Wide Mouse/Keyboard Hook

    That link is working fine for me.

    Take a look at this:

    http://www.javaprogrammingforums.com...s-eclipse.html

    I know its not the same thing but I think you can execute custom commands somewhere here.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  11. #11
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    I've now migrated to notepad and command prompt, and at last progress! I've had to ditch all the high level IDEs lol, ah well it's all good .

    Anyways, I just wanted to tell you that I have now begun the full coding of the system, so I hope to keep you updated with progress and code.

    Can I ask, is there any functionality that you would like me to add? As I don't know how much you know about C++ and JNI so I figure I could add them in at the start.

    Thanks,
    Chris

  12. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    Just wanted to give you an update, I haven't had much chance to get to work on this due to college exams. But I am now in the middle of my exams and will be finished at the end of next week. So hopefully I will be able to dig out old code and go back over this and finish it off and get the code to you

    Also i'd like to produce a sample macro'ing program using it and the Robot API, which should be quite nice.

    Regards,
    Chris

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: System Wide Mouse/Keyboard Hook

    Sounds good Chris I know it will come in very useful. Keep me updated..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  14. #14
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    I doubt this is possible in java since it runs off of a Vm. I could be wrong though.
    Last edited by Fendaril; June 14th, 2009 at 11:27 PM.

  15. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: System Wide Mouse/Keyboard Hook

    Quote Originally Posted by Fendaril View Post
    I doubt this is possible in java since it runs off of a Vm. I could be wrong though.
    Thats why Chris is writing a C hook
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  16. #16
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    Just for the record it's not complex, its just a god damn stupid with UnsatisfiedLinker error, so its hard to even test things, I don't even know why it is doing that to me -_-

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  17. #17
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    I finally found out what the problem was

    I was forgetting to compile it as if it were C code rather than C++, cause C doesn't name mangle like C++ does lol!

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  18. #18
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: System Wide Mouse/Keyboard Hook

    ok ok. Now this one is not so much a programming problem, I just want some help with a bit of logic. For the keyboardhook, I've decided what information to proccess, what key is pressed, is the alt key pressed and is it an extended key, plus was it a keyup or keydown.

    However for the mouse, there are a fair few different states that need to be proccessed, 9 in actually fact. Now If I do this in C/C++ its not to hard because they have nice names like WM_LMBUTTONDOWN but, then there are a whole list of different things they could be true such as ctrldown and shift downand any other mouse button down. So there would be alot of possibles combinations, which would get messy sending the data back to Java. But if I was to do it in Java i'd have to find out a long list of numbers. So basically, I need to know what key data I should procces in C++ and send to Java, my suggestion is as Follows.

    Which button
    Up or Down
    is ctrl pressed
    is shift pressed
    x pos
    y pos.

    Thanks,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. Replies: 8
    Last Post: April 21st, 2013, 08:20 AM
  2. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 09:57 AM
  3. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM
  4. [SOLVED] Mobile operating system using Java technology
    By blackJava in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 16th, 2009, 02:44 PM
  5. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 06:10 AM