Waiting for a Mouse click

  • Thread starter Thread starter Andreas Müller
  • Start date Start date
A

Andreas Müller

Hi all,

I need to wait for the user to click the mouse:

// some code before
WaitForMouseClick();// Wait until the user clicks the mouse
// go on doing something

Under Win32 using C++, you could poll the mouse buttons states. Is this
possible using C# and .NET?

Thanks in advance,
Andy
 
Mike said:
Hi

You need to use the MouseEventArgs Class. see msdn here
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsMo
useEventArgsClassTopic.asp


Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html

Hi Mike,

thanks for the answer, however that was not what I was looking for :-(. I
propably explained it bad.

The normal way in a win form application to capture the mouse is to react to
the mouse events, such as MouseDown using the MouseEventArgs class .

However, I have the (strange? ;-) ) need to do something like this

class Xox
{
public static AquirePointFromUser()
{
// promt the user
MessageBox.Show("Please pick a point");

// now wait *here* for the pick of the user
WaitForMouseClick();

// get the currrent position
return Control.MousePosition;
}
private static WaitForMouseClick()
{
// see below
}
}

I need this for an API of my application. When a client calls
Xox.AquirePointFromUser(), it has to be made sure, that the user really
clicks and that the client can't go on untill it happened.

The idea of the API function is, that when called, the user is prompted to
select something and after that the function returns. So I want to wait for
the mouse to be clicked. What I mean by waiting, is that WaitForMouseClick()
wil not return *before* the mouse was clicked.

In Pseudo code, WaitForMouseClick() would look like this

private static void WaitForMouseClick()
{
while(true)
{
bool bMouseDown = Poll_Mouse_State_Button();//<-this is what
//
im looking for
if(bMouseDown)
return;
}
}

I can do this in Win32, so I guess it can be don using C#, too.

I hope this explains it a bit better.

Cheers,
Andy
 
Hi Mike,
thanks for the answer, however that was not what I was looking for :-(. I
propably explained it bad.

The normal way in a win form application to capture the mouse is to react to
the mouse events, such as MouseDown using the MouseEventArgs class .

However, I have the (strange? ;-) ) need to do something like this

class Xox
{
public static AquirePointFromUser()
{
// promt the user
MessageBox.Show("Please pick a point");

// now wait *here* for the pick of the user
WaitForMouseClick();

// get the currrent position
return Control.MousePosition;
}
private static WaitForMouseClick()
{
// see below
}
}

I need this for an API of my application. When a client calls
Xox.AquirePointFromUser(), it has to be made sure, that the user really
clicks and that the client can't go on untill it happened.

The idea of the API function is, that when called, the user is prompted to
select something and after that the function returns. So I want to wait for
the mouse to be clicked. What I mean by waiting, is that WaitForMouseClick()
wil not return *before* the mouse was clicked.

In Pseudo code, WaitForMouseClick() would look like this

private static void WaitForMouseClick()
{
while(true)
{
bool bMouseDown = Poll_Mouse_State_Button();//<-this is what
//
im looking for
if(bMouseDown)
return;
}
}

I can do this in Win32, so I guess it can be don using C#, too.

I hope this explains it a bit better.

Cheers,
Andy


What you can do is set a state :

class Toto
{
private int state = 0;

//when the user do the condition that blocks, set state to 1

private void onclick(good param)
{
if (state == 1)
do my stufff
else
do my other stuff
}

}

And, if inside the rest of the code you have things that must not be used if
it is not validated, you can do a if (state == 1) then not good.

Might not be clear and perhaps not what you want, nevertheless, I hop it
helps you :o)
 
Mathieu said:
What you can do is set a state :

class Toto
{
private int state = 0;

//when the user do the condition that blocks, set state to 1

private void onclick(good param)
{
if (state == 1)
do my stufff
else
do my other stuff
}

}

And, if inside the rest of the code you have things that must not be
used if it is not validated, you can do a if (state == 1) then not
good.

Might not be clear and perhaps not what you want, nevertheless, I hop
it helps you :o)

It did not :o(, but thank you anyway.

Basically it's the first step. The problem is, that I have to *wait* for the
state to change:
class Toto
{
private int state = 0;
private void OnMouseClick(good param)
{
state = 1;//change the state
}
public Point Pick()
{

// need to wait untill the state changes
while(state == 0)
{
//this will eat all CPU power and block
//my thread, so I never get the MouseClick event.
//Thread.Sleep(10);// Does not help either
}
}
}
 
Back
Top