Felipe Ramos
posted
243 posts
since
Feb 28, 2008
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
Maybe we are talking about different things, but I was under the impression that UserActivityHook was a class writtem to wrap Win32 calls using C# with P/Invoke. You can find a complete listing at koders.
|
|
|
|
|
|
webb webbhelp
posted
7 posts
since
Aug 30, 2010
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
Hi!
First of all I want to thank you for replying, I really appreciate that :)
I got the class that I am supposed to use but now I got an error message :/
public void Start(bool InstallMouseHook, bool InstallKeyboardHook) { // install Mouse hook only if it is not installed and must be installed if (hMouseHook == 0 && InstallMouseHook) { // Create an instance of HookProc. MouseHookProcedure = new HookProc(MouseHookProc); //install hook hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]), 0); //If SetWindowsHookEx fails. if (hMouseHook == 0) { //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. int errorCode = Marshal.GetLastWin32Error(); //do cleanup Stop(true, false, false); //Initializes and throws a new instance of the Win32Exception class with the specified error. throw new Win32Exception(errorCode); //---------------------------------------------------------------------HERE IS THE PROBLEM } }
// install Keyboard hook only if it is not installed and must be installed if (hKeyboardHook == 0 && InstallKeyboardHook) { // Create an instance of HookProc. KeyboardHookProcedure = new HookProc(KeyboardHookProc); //install hook hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]), 0); //If SetWindowsHookEx fails. if (hKeyboardHook == 0) { //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. int errorCode = Marshal.GetLastWin32Error(); //do cleanup Stop(false, true, false); //Initializes and throws a new instance of the Win32Exception class with the specified error. throw new Win32Exception(errorCode); } } }
I got a error message win32 exception i unhandled when I use this code:
UserActivityHook actHook; private void playersDream_Load(object sender, EventArgs e) { actHook = new UserActivityHook(); // crate an instance with global hooks // hang on events actHook.OnMouseActivity += new MouseEventHandler(MouseMoved); actHook.KeyDown += new KeyEventHandler(MyKeyDown); actHook.KeyPress += new KeyPressEventHandler(MyKeyPress); actHook.KeyUp += new KeyEventHandler(MyKeyUp); }
public void MouseMoved(object sender, MouseEventArgs e) { MessageBox.Show("1"); }
public void MyKeyDown(object sender, KeyEventArgs e) { MessageBox.Show("2"); }
public void MyKeyPress(object sender, KeyPressEventArgs e) { MessageBox.Show("3"); }
public void MyKeyUp(object sender, KeyEventArgs e) { MessageBox.Show("4"); }
I include this: using System; using System.Windows.Forms; using gma.System.Windows;
and the file "useractivityhook.cs" - class.
I have no idea what is wrong and I really hope you could help me.
I really appreciate your help and hope i got more help with this, thanks =)
|
|
|
|
|
|
Felipe Ramos
posted
243 posts
since
Feb 28, 2008
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
I found the microsoft tutorial to set the hook for the mouse. It may be better for you to write this code and understand what is going on instead of using the UserActivityHook (couldn't debug it). I follow the tutorial and it worked.
|
|
|
|
|
|
webb webbhelp
posted
7 posts
since
Aug 30, 2010
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
That one worked absolutely perfect! I really appreciate your help, Thanks you so much :D
|
|
|
|
|
|
webb webbhelp
posted
7 posts
since
Aug 30, 2010
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
I have a a few more question I would be happy if you could help me with :/
* This reacts when the mouse is moving, how can I decide a function to run when I CLICK? Does this functions, works with keyboard events to?
I got a warning:
'System.AppDomain.GetCurrentThreadId()' is obsolete: 'AppDomain.GetCurrentThreadId has been deprecated because it does not provide a stable Id when managed threads are running on fibers (aka lightweight threads). To get a stable identifier for a managed thread, use the ManagedThreadId property on Thread. http://go.microsoft.com/fwlink/?linkid=14202'
I am not really good at C# yet, and specially not with threads, can I fix this warning, do you got a idéa?
I would be very happy if you want to spend some more minutes to help me with this :)
|
|
|
|
|
|
Felipe Ramos
posted
243 posts
since
Feb 28, 2008
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
webb webbhelp
posted
7 posts
since
Aug 30, 2010
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
Really thank you for all help, the most works now! I use the code: http://support.microsoft.com/default.aspx?scid=kb;en-us;318804 Because I thought it worked but it didn't work like I wanted it! Now I can only click in the program window, it doesn't work if I click outside the window, nothing is happening, I did a messagebox if you click, but the messagebox just shows if you click in the program :/
I would be really happy if you could see what is wrong, I uploaded my source files.
Thank you, I really hope you will help me more, I really appreciate your help, it doesn't seems that somebody knows this except you =)
|
|
|
|
|
|
Felipe Ramos
posted
243 posts
since
Feb 28, 2008
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
webb the attached project is from a developer name George Mamaladze found in CodeProject.com. The solution contains the code for all the hooks you want to setup. There are a couple of difference on how to set global or applicaiton hooks, but the biggest change is the WH_MOUSE = 7 to WH_MOUSE_LL = 14. Hope the code speeds your project and good luck.
|
|
|
|
|
|
webb webbhelp
posted
7 posts
since
Aug 30, 2010
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
Thank you very much, I will test It right now, but now when your here I take the minute to ask: I read somewhere that globalhooks didn't worked in C#, is that wrong? or have I missunderstood it? If you got time and want you can explane the different with global hooks and low level hooks, if you don't want, just ignore :) I am happy your helping :)
|
|
|
|
|
|
Felipe Ramos
posted
243 posts
since
Feb 28, 2008
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
webb my understanding and I may be wrong is that local hooks refers to the hooks that work only for your application and global hooks work outside of it. For instance the issue you were having with the clicks not working outside of your form. What I did hear was that the global hooks didn't work in Vista or 7.
|
|
|
|
|
|
webb webbhelp
posted
7 posts
since
Aug 30, 2010
from
|
|
Re: Mouse background hook
|
|
|
|
|
|
|
|
|
|
|
Thanks thanks thanks :)
The program works perfect, exact how I wanted it ;P But now I got another problem, I actually think it is the last :P
When the user click somewhere in the computer, I got this code to make 1 more click: //Call the imported function with the cursor's current position int X = Cursor.Position.X; int Y = Cursor.Position.Y; mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
It works, it clicks BUT when I click on the left mousebutton, a function with this code in, runs and make a click, and when this click the hook thinks that I am clicking, so I got into an infinity loop here ;P
I am not sure what I shall do to fix it, I tried to add a boolean variable that changed to true when I clicked on the mouse and then it went back to false when the program had clicked but it didn't worked. I uploaded the files if it would be needed!
in: HookManager.Callbacks.cs , there is where the check if a mouse button has been press is, in there I call a function in the class: MouseEvents.cs, and that function is the function which simulate a click!
I would be very happy if you could spend a little bit more time on this, because I think and hope this is the last question. I didn't wanted to ask but iI didn't succed to solve it by my self, I have tried for hours now :/
Thanks =)
|
|
|
|
|
|