Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# reliable MouseMove (hop)

Tags:

c#

mousemove

I'm using this function to move the cursor.

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

When I use a hotkey to trigger it, the cursor will move to the correct coords and next time I move the mouse it continues from that position. Working as intended.

However I need to trigger SetCursorPos during a MouseMove event. If user moves mouse into a certain area I want it to hop to a different place and carry on from there. But right now it hops to the destination and then immediately hops back (90% of the time). How can I avoid that behavior?

Edit: I decided to work around it by clipping the cursor in 1 by 1 px square for 1 mousemove event. Cursor.Clip(MousePosition, new Rectangle(1, 1));

like image 250
user1340531 Avatar asked Mar 25 '13 01:03

user1340531


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

My guess is that there is another control on top of your form in the area where you want to trigger the event. If so, the control is capturing the MouseMove event.

For example, here I've added a green 200x200 panel at position 0, 0 in the upper left hand corner. If the mouse moves over the panel, the form's MouseMove event will stop capturing the mouse cursor position. In my form's mouse_move event, I set the form's text to display the mouse coordinates. Notice the coordinates in the Window Text are still 200, 200 when the mouse was actually at 0, 0 (can't see my cursor due to having to click on SnippingTool.exe to get the screenshot).

enter image description here

To remedy this, use the same code you placed in your form's MouseMove event in the panel's MouseMove event (or whichever control you are using). This results in the correct coordinates in the form's text.

enter image description here

And here is the code (This could obviously be refactored into a single method):

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        this.Text = string.Format("X: {0}, Y: {1}", e.X, e.Y);

        if (e.X >= 0 && e.X <= 200)
        {
            if (e.Y >= 0 && e.Y <= 200)
            {
                SetCursorPos(500, 500);
            }
        }
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        this.Text = string.Format("X: {0}, Y: {1}", e.X, e.Y);

        if (e.X >= 0 && e.X <= 200)
        {
            if (e.Y >= 0 && e.Y <= 200)
            {
                SetCursorPos(500, 500);
            }
        }
    }
}
like image 193
Inisheer Avatar answered Sep 28 '22 07:09

Inisheer