Hi!
I am trying to hook mouseevents in the background but I don't get everything to work :/
public UserActivityHook actHook;
private void mouseclicker_Load(object sender, EventArgs e)
{
actHook = new UserActivityHook(); // create an instance
// 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 MyKeyDown(object sender, MouseEventArgs e)
{
MessageBox.Show("CLICK");
}
the problem is:
public UserActivityHook actHook;
The type or namespace name 'UserActivityHook' could not be found (are you missing a using directive or an assembly reference?)
That error message do I got.
Please help me out here :)
Loading
webb webbhelpPosted Sep 3, 2010, 7:32 PM
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 =)
Felipe RamosPosted Sep 3, 2010, 10:11 AM
webb webbhelpPosted Sep 3, 2010, 9:46 AM
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 RamosPosted Sep 3, 2010, 9:43 AM
webb webbhelpPosted Sep 3, 2010, 9:01 AM
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 RamosPosted Sep 2, 2010, 8:51 PM
webb webbhelpPosted Sep 2, 2010, 2:28 PM
* 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 :)
webb webbhelpPosted Sep 2, 2010, 2:10 PM
I really appreciate your help, Thanks you so much :D
Felipe RamosPosted Sep 2, 2010, 1:19 PM
webb webbhelpPosted Sep 2, 2010, 12:42 PM
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 RamosPosted Sep 2, 2010, 11:08 AM