-1
public partial class Form3 : Form
{
    private readonly KeyboardHookListener m_KeyboardHookManager;
    private readonly MouseHookListener m_MouseHookManager;

    [DllImport("user32")]
    private static extern Int32 ClipCursor(RECT lpRect);

    [DllImport("user32")]
    private static extern Int32 ShowCursor(Int32 bShow);

    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out POINT lpPoint);

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;

        public static implicit operator Point(POINT point)
        {
            return new Point(point.X, point.Y);
        }
    }

    public static Point GetCursorPosition()
    {
        POINT lpPoint;
        GetCursorPos(out lpPoint);
        //bool success = User32.GetCursorPos(out lpPoint);
        // if (!success)

        return lpPoint;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public Int32 Left;
        public Int32 Top;
        public Int32 Right;
        public Int32 Bottom;
    }

    public Form3()
    {
        InitializeComponent();

        m_KeyboardHookManager = new KeyboardHookListener(new GlobalHooker());
        m_KeyboardHookManager.Enabled = true;
        m_KeyboardHookManager.KeyDown += HookManager_KeyDown;
        m_KeyboardHookManager.KeyUp += HookManager_KeyUp;

        m_MouseHookManager = new MouseHookListener(new GlobalHooker());
        m_MouseHookManager.Enabled = true;
        m_MouseHookManager.MouseDown += HookManager_MouseDown;
        m_MouseHookManager.MouseUp += HookManager_MouseUp;
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        //ShowCursor(0);
        //MoveCursor();
    }
    private void HookManager_KeyDown(object sender, KeyEventArgs e)
    {
        label1.Text = e.KeyData.ToString() + " Pressed";
    }

    private void HookManager_KeyUp(object sender, KeyEventArgs e)
    {
        label1.Text = e.KeyData.ToString() + " Released";
    }

    private void HookManager_MouseUp(object sender, MouseEventArgs e)
    {
        label1.Text = e.Button.ToString() + " Released";
    }

    private void HookManager_MouseDown(object sender, MouseEventArgs e)
    {
        label1.Text = e.Button.ToString() + " Pressed";
        ShowCursor(0);
        MoveCursor();
        System.Threading.Thread.Sleep(5000);//5 sec   
        RestoreCursor(this);
        ShowCursor(1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //RestoreCursor(this);
        //ShowCursor(1);
    }

    private void Form3_MouseMove(object sender, MouseEventArgs e)
    {
        //MoveCursor();
    }

    private RECT MouseTrap;
    public void RestoreCursor(System.Windows.Forms.Form ThisForm)
    {
        var _with1 = MouseTrap;
        _with1.Top = 0;
        _with1.Left = 0;
        _with1.Right = Screen.PrimaryScreen.Bounds.Width;
        _with1.Bottom = Screen.PrimaryScreen.Bounds.Height;

        ClipCursor(_with1);
    }

    private void MoveCursor()
    {
        // set the Current cursor, move the cursor's Position,
        // and set its clipping rectangle to the form. 
        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(this.Left, this.Top);
        Cursor.Clip = new Rectangle(this.Left, this.Top, this.Width, this.Height);            
    }
}

I am developing one sample application My requirement is when user clicks on anywhere on desktop for example click on Windows "Start" button then Hide the cursor for 2 sec then show the cursor after 2 sec at the same position.

For that I have written above code and used Thread.Sleep(2000) for waiting but cursor position restores to Form's left.

https://stackoverflow.com/questions/16781948/how-can-i-hook-globaly-all-the-keys-and-all-the-mouse-buttons/16782294

GlobalMouseHookDLL

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
ND's
  • 2,155
  • 6
  • 38
  • 59
  • 1
    I'm sure you don't want to hear this, but this is an awful, ill-conceived idea. It will break in all sorts of subtle ways, and your code makes several significant assumptions that cannot be generalized and therefore will not work reliably across all system configurations. Also, it's far from bulletproof; there are many ways that the user could re-enable the mouse, so this isn't really doing anything but creating an annoying obstacle. Why is this even a "requirement"? I can't imagine how it would serve any constructive purpose. – Cody Gray - on strike Jun 07 '17 at 09:17

1 Answers1

0

you can achieve that using System.Windows.Forms.Timer.

Declare Timer on your form.

public partial class Form3 : Form
{
    private Timer timer = new Timer();

Set it on Form_Load

timer.Enabled = false;
timer.Tick += Timer_Tick;
timer.Interval = 5000;

When user moves mouse, instead of Thread.Sleep activate timer. When timer ticks (when specified 5000 milliseconds pass), Timer_Tick method will execute. In it, show pointer.

private void HookManager_MouseDown(object sender, MouseEventArgs e)
{
    label1.Text = e.Button.ToString() + " Pressed";
    ShowCursor(0);
    MoveCursor();
    timer.Start();
}

private void Timer_Tick(object sender, EventArgs e)
{

    RestoreCursor(this);
    ShowCursor(1);
    timer.Stop();
}
Nino
  • 6,931
  • 2
  • 27
  • 42
  • I have added timer_tick above code but still not working mouse down event is only fired first time not for second time – ND's Jun 07 '17 at 09:19
  • @John Can you put breakpoint in `Timer_Tick` method to see if that event is fired? Have you added `timer.Tick += Timer_Tick;` in `FormLoad` or in Form constructor? – Nino Jun 07 '17 at 09:21
  • It's hard to say what is wrong, especially with all that external cods – Nino Jun 07 '17 at 09:46