Reputation: 241
I'm currently writing a program that holds down a mouse button when initially clicked, and continues to hold it until the user presses the mouse button for a second time.
The program works by detecting input globally using a MouseHookListener and then uses an input simulator to hold down the mouse button that has been pressed.
The program is able to hold down the mouse as intended, but there is an issue with the original mouse click that signals the program to simulate the button being held; it still gets carried out. I know that the MouseHookListener uses low level hooks to operate, but it's HookCallBack() method can't be overridden due to it being protected.
Is there any way to block out the original mouse input? Or is there a way to make the original input held in until the mouse is clicked once more?
This is the code I've produced, thus far (note - the mListener is being activated in a forum else where):
public MouseHold()
{
mListener = new MouseHookListener(new GlobalHooker());
mListener.MouseClick += mListener_MouseClick;
}
private bool isDown;
private int count = 0;
private InputSimulator sim = new InputSimulator();
public MouseHookListener mListener;
private void mListener_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (isDown)
{
isDown = false;
Console.Out.WriteLine(count);
}
else
{
Console.Out.WriteLine(count);
isDown = true;
sim.Mouse.LeftButtonDown();
}
}
}
Upvotes: 2
Views: 3295
Reputation: 7931
I am the author of the library you mentioned https://github.com/gmamaladze/globalmousekeyhook.
To enable your usecase I have extended the library by one more event. Please note that the construction of listener has also slightly changed in the new version.
I beleave that with the example below you are able to accomplish your scenario without involving InputSimulator
library.
m_SupressNextUp
- down is fired.m_SupressNextDown
m_SupressNextUp
m_SupressNextDown
internal class Sample
{
private IKeyboardMouseEvents m_GlobalHook;
private bool m_SupressNextUp;
private bool m_SupressNextDown;
public void Subscribe()
{
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
m_GlobalHook.MouseUpExt += GlobalHook_MouseUpExt;
}
void GlobalHook_MouseUpExt(object sender, MouseEventExtArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (m_SupressNextUp)
{
Console.WriteLine(@"First mouse up supress.");
e.Handled = true;
m_SupressNextDown = true;
}
else
{
Console.WriteLine(@"Second mouse up - make it heppen.");
m_SupressNextDown = false;
}
}
}
private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (m_SupressNextDown)
{
Console.WriteLine(@"Second mouse down supress.");
e.Handled = true;
m_SupressNextUp = false;
}
else
{
Console.WriteLine(@"First mouse down - make it heppen.");
m_SupressNextUp = true;
}
}
}
public void Unsubscribe()
{
m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
m_GlobalHook.MouseUpExt -= GlobalHook_MouseUpExt;
m_GlobalHook.Dispose();
}
}
Upvotes: 2
Reputation: 953
after reviewing the documentation for the input simulator I am guessing you need to reverse the mouse down input.
the source code for the input simulator has a mouse up method
private void mListener_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (isDown)
{
isDown = false;
Console.Out.WriteLine(count);
sim.Mouse.LeftButtonUp(); //maybe try this
}
else
{
Console.Out.WriteLine(count);
isDown = true;
sim.Mouse.LeftButtonDown();
}
}
}
Upvotes: 0