c# - Ways of checking if left mouse button is down -


i new programming , first time have done in c#. have source code of program use , modify make better me use personally. program set in way when user holds down left mouse button, mouse clicks in random intervals. want there delay between when mouse held down , again, why implemented code:

public void performleftclick(int xpos, int ypos) {     mouse_event(0x02, xpos, ypos, 0, 0); //leftdown     thread.sleep(49);     mouse_event(0x04, xpos, ypos, 0, 0); //leftup     leftdown = true; } 

...

private void leftmouseup(mousehook.msllhookstruct mousestruct)     {         leftdown = false;     }  private void leftmousedown(mousehook.msllhookstruct mousestruct)     {         if (leftdown == false)         {             timeleftclicked = datetime.now;         }         leftdown = true;     } 

mouse hook (not code obviously):

#region copyright /// <copyright> /// copyright (c) 2011 ramunas geciauskas, http://geciauskas.com /// /// permission hereby granted, free of charge, person obtaining copy /// of software , associated documentation files (the "software"), deal /// in software without restriction, including without limitation rights /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell /// copies of software, , permit persons whom software /// furnished so, subject following conditions: /// /// above copyright notice , permission notice shall included in /// copies or substantial portions of software. /// /// software provided "as is", without warranty of kind, express or /// implied, including not limited warranties of merchantability, /// fitness particular purpose , noninfringement. in no event shall /// authors or copyright holders liable claim, damages or other /// liability, whether in action of contract, tort or otherwise, arising from, /// out of or in connection software or use or other dealings in /// software. /// </copyright> /// <author>ramunas geciauskas</author> /// <summary>contains mousehook class setting low level windows mouse hooks.</summary> #endregion  using system; using system.runtime.interopservices; using system.diagnostics;  namespace ramgectools {        /// <summary>     /// class intercepting low level windows mouse hooks.     /// </summary>     class mousehook     {         /// <summary>         /// internal callback processing function         /// </summary>         private delegate intptr mousehookhandler(int ncode, intptr wparam, intptr lparam);         private mousehookhandler hookhandler;          /// <summary>         /// function called when defined occurs         /// </summary>         /// <param name="mousestruct">msllhookstruct mouse structure</param>         public delegate void mousehookcallback(msllhookstruct mousestruct);          #region events         public event mousehookcallback leftbuttondown;         public event mousehookcallback leftbuttonup;         public event mousehookcallback rightbuttondown;         public event mousehookcallback rightbuttonup;         public event mousehookcallback mousemove;         public event mousehookcallback mousewheel;         public event mousehookcallback doubleclick;         public event mousehookcallback middlebuttondown;         public event mousehookcallback middlebuttonup;         #endregion          /// <summary>         /// low level mouse hook's id         /// </summary>         private intptr hookid = intptr.zero;          /// <summary>         /// install low level mouse hook         /// </summary>         /// <param name="mousehookcallbackfunc">callback function</param>         public void install()         {             hookhandler = hookfunc;             hookid = sethook(hookhandler);         }          /// <summary>         /// remove low level mouse hook         /// </summary>         public void uninstall()         {             if (hookid == intptr.zero)                 return;              unhookwindowshookex(hookid);             hookid = intptr.zero;         }          /// <summary>         /// destructor. unhook current hook         /// </summary>         ~mousehook()         {             uninstall();         }          /// <summary>         /// sets hook , assigns id tracking         /// </summary>         /// <param name="proc">internal callback function</param>         /// <returns>hook id</returns>         private intptr sethook(mousehookhandler proc)         {                using (processmodule module = process.getcurrentprocess().mainmodule)                 return setwindowshookex(wh_mouse_ll, proc, getmodulehandle(module.modulename), 0);         }                  /// <summary>         /// callback function         /// </summary>         private intptr hookfunc(int ncode, intptr wparam, intptr lparam)         {             // parse system messages             if (ncode >= 0)             {                 if (mousemessages.wm_lbuttondown == (mousemessages)wparam)                     if (leftbuttondown != null)                         leftbuttondown((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_lbuttonup == (mousemessages)wparam)                     if (leftbuttonup != null)                         leftbuttonup((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_rbuttondown == (mousemessages)wparam)                     if (rightbuttondown != null)                         rightbuttondown((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_rbuttonup == (mousemessages)wparam)                     if (rightbuttonup != null)                         rightbuttonup((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_mousemove == (mousemessages)wparam)                     if (mousemove != null)                         mousemove((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_mousewheel == (mousemessages)wparam)                     if (mousewheel != null)                         mousewheel((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_lbuttondblclk == (mousemessages)wparam)                     if (doubleclick != null)                         doubleclick((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_mbuttondown == (mousemessages)wparam)                     if (middlebuttondown != null)                         middlebuttondown((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));                 if (mousemessages.wm_mbuttonup == (mousemessages)wparam)                     if (middlebuttonup != null)                         middlebuttonup((msllhookstruct)marshal.ptrtostructure(lparam, typeof(msllhookstruct)));             }             return callnexthookex(hookid, ncode, wparam, lparam);         }          #region winapi         private const int wh_mouse_ll = 14;          private enum mousemessages         {             wm_lbuttondown = 0x0201,             wm_lbuttonup = 0x0202,             wm_mousemove = 0x0200,             wm_mousewheel = 0x020a,             wm_rbuttondown = 0x0204,             wm_rbuttonup = 0x0205,             wm_lbuttondblclk = 0x0203,             wm_mbuttondown = 0x0207,             wm_mbuttonup = 0x0208         }          [structlayout(layoutkind.sequential)]         public struct point         {             public int x;             public int y;         }          [structlayout(layoutkind.sequential)]         public struct msllhookstruct         {             public point pt;             public uint mousedata;             public uint flags;             public uint time;             public intptr dwextrainfo;         }          [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]         private static extern intptr setwindowshookex(int idhook,             mousehookhandler lpfn, intptr hmod, uint dwthreadid);          [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]         [return: marshalas(unmanagedtype.bool)]         public static extern bool unhookwindowshookex(intptr hhk);          [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]         private static extern intptr callnexthookex(intptr hhk, int ncode, intptr wparam, intptr lparam);          [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)]         private static extern intptr getmodulehandle(string lpmodulename);         #endregion     } } 

then after code executed, follows:

if (leftdown && ((int)datetime.now.subtract(timeleftclicked).totalmilliseconds) > numdelaym)     {         random random = new random();         performleftclick(mousex, mousey);         thread.sleep((((int)numclickspeed.value) + random.next(0, (int)numrandomclick.value) - 49));     } 

which holds mouse button (after starting delay), lifts after 49 milliseconds , sleeps user-inputted time (minus 49). however, when lift mouse button code continues loop because set leftdown true (as otherwise code not repeat, annoyingly). want user lifts mouse button code stop looping.

is there way of making don't need set leftdown true , code continue execute while left mouse button being held down, , stopped when lifted? need find method of checking how left mouse button held down this?

thanks in advance.

in windows forms can use mouse events.

you have mousedown event , mouseup event can use similar following code:

 public form1()     {         initializecomponent();         this.mousedown += mousedownfunction;         this.mouseup += mouseupfunction;     }        private void mousedownfunction(object sender, mouseeventargs e)     {         if (e.button == mousebuttons.left)         {             //do on left mouse down         }                 }      private void mouseupfunction(object sender, mouseeventargs e)     {         if (e.button == mousebuttons.left)         {             //do on left mouse         }     } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -