Created
July 4, 2017 22:42
-
-
Save Cranc/9e26aedc73643c13f2678ccee8eb33bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MouseClick | |
{ | |
/// <summary> | |
/// static class with Misc functions | |
/// </summary> | |
public static class Misc | |
{ | |
/// <summary> | |
/// some key codes | |
/// </summary> | |
public const uint WM_LBUTTONDOWN = 0x201; | |
public const uint WM_LBUTTONUP = 0x202; | |
public const uint MK_LBUTTON = 0x0001; | |
public const uint K_KEYDOWN = 0x100; | |
public const uint K_KEYUP = 0x101; | |
public const uint K_KEY5 = 0x35; | |
public const uint K_KEY4 = 0x34; | |
public const uint K_KEY3 = 0x33; | |
public const uint K_KEY2 = 0x32; | |
public const uint K_KEY1 = 0x31; | |
/// <summary> | |
/// Checks a given string for digits only (should be faster then most checks like REGEX) | |
/// </summary> | |
/// <param name="s">string to check</param> | |
/// <returns></returns> | |
public static bool DigitsOnly(string s) | |
{ | |
int len = s.Length; | |
if (s.Equals("")) | |
return false; | |
for (int i = 0; i < len; ++i) | |
{ | |
char c = s[i]; | |
if (c < '0' || c > '9') | |
return false; | |
} | |
return true; | |
} | |
/// <summary> | |
/// Creates LParam. | |
/// </summary> | |
/// <param name="LoWord"> Low word</param> | |
/// <param name="HiWord"> High word</param> | |
/// <returns>created LParam</returns> | |
public static IntPtr CreateLParam(int LoWord, int HiWord) | |
{ | |
return (IntPtr)((HiWord << 16) | (LoWord & 0xffff)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
namespace MouseClick | |
{ | |
/// <summary> | |
/// Class represents a Mouseclick. | |
/// </summary> | |
class MouseClick | |
{ | |
[DllImport("user32.dll", SetLastError = true)] | |
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | |
[DllImport("user32.dll", CharSet = CharSet.Auto)] | |
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); | |
public bool isrunning; | |
private IntPtr hWnd; | |
private int interval; | |
private Timer timer; | |
/// <summary> | |
/// volatiles | |
/// </summary> | |
private volatile int posx, posy; | |
/// <summary> | |
/// Constructor. | |
/// </summary> | |
/// <param name="windowName">name of the window where the mouseclick is executed in.</param> | |
public MouseClick(string windowName) | |
{ | |
hWnd = (IntPtr)FindWindow(null, windowName);//find window with window name | |
interval = 500; | |
this.posx = 200; | |
this.posy = 200; | |
} | |
/// <summary> | |
/// Change the Window for the mouse click. | |
/// </summary> | |
/// <param name="windowName">name of the window where the mouseclick is executed in.</param> | |
/// <returns></returns> | |
public bool SetWindowName(string windowName) | |
{ | |
hWnd = (IntPtr)FindWindow(null, windowName);//find window with window name | |
if (this.hWnd == null) | |
return false; | |
return true; | |
} | |
/// <summary> | |
/// Change the amount of Clicks per Second executed. | |
/// [cps is a Range between 1-1000] | |
/// </summary> | |
/// <param name="cps">Clicks per second.</param> | |
public void setClicks(int cps) | |
{ | |
if (cps > 1000) | |
interval = 1; | |
else if (cps < 1) | |
interval = 1000; | |
else | |
interval = 1000 / cps; | |
} | |
/// <summary> | |
/// Change Mouse Position of the "artifical" mouse. | |
/// </summary> | |
/// <param name="x">x position.</param> | |
/// <param name="y">y position.</param> | |
public void SetMousePosition(int x, int y) | |
{ | |
this.posx = x; | |
this.posy = y; | |
} | |
/// <summary> | |
/// Returns the current position as a Tuple(int,int). | |
/// </summary> | |
/// <returns>position as Tuple.</returns> | |
public Tuple<int, int> getPosition() | |
{ | |
return new Tuple<int, int>(this.posx, this.posy); | |
} | |
/// <summary> | |
/// Starts the mouse clicking. | |
/// </summary> | |
public void StartClick() | |
{ | |
isrunning = true; | |
TimerCallback tmCallback = CheckEffectExpiry; | |
timer = new Timer(tmCallback, "run", 500, interval); | |
} | |
/// <summary> | |
/// Stops the mouse clicking. | |
/// </summary> | |
public void StopClick() | |
{ | |
isrunning = false; | |
if (timer != null) | |
timer.Dispose(); | |
} | |
/// <summary> | |
/// Function is executed by timer and sends mouse click to window. | |
/// </summary> | |
/// <param name="objectinfo"></param> | |
private void CheckEffectExpiry(object objectinfo) | |
{ | |
if (hWnd != null) | |
{ | |
SendMessage( | |
hWnd, | |
Misc.WM_LBUTTONDOWN, | |
new IntPtr(Misc.MK_LBUTTON), | |
Misc.CreateLParam(this.posx, this.posy) | |
); | |
SendMessage( | |
hWnd, | |
Misc.WM_LBUTTONUP, | |
new IntPtr(Misc.MK_LBUTTON), | |
Misc.CreateLParam(this.posx, this.posy) | |
); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MouseClick | |
{ | |
/// <summary> | |
/// Class represents a Key on the keyboard. | |
/// </summary> | |
public class MyKey | |
{ | |
public int Id { get; set; } | |
public uint keycode; | |
public uint interval; | |
//identifies one specific instance of this class | |
public Guid InstanceID { get; private set; } | |
private uint count; | |
/// <summary> | |
/// Constructor. | |
/// </summary> | |
/// <param name="keycode">key code.</param> | |
/// <param name="interval">interval of button press.</param> | |
public MyKey(uint keycode, uint interval) | |
{ | |
this.keycode = keycode; | |
this.interval = interval; | |
this.Id = (int) keycode; | |
this.count = interval; | |
this.InstanceID = Guid.NewGuid(); | |
} | |
/// <summary> | |
/// Can be used to determin if Key should be executed [use with IncreaseCount()]. | |
/// </summary> | |
/// <returns>true, if the Key should be executed.</returns> | |
public bool Execute() | |
{ | |
if (count >= interval) | |
{ | |
count = 0; | |
return true; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Can be used to increase the Count used to check the interval [use with Execute()]. | |
/// </summary> | |
public void IncreaseCount() | |
{ | |
count++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment