Skip to content

Instantly share code, notes, and snippets.

@Cranc
Created July 4, 2017 22:42
Show Gist options
  • Save Cranc/9e26aedc73643c13f2678ccee8eb33bb to your computer and use it in GitHub Desktop.
Save Cranc/9e26aedc73643c13f2678ccee8eb33bb to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace MouseClick
{
/// <summary>
/// Class represents a button click.
/// </summary>
class ButtonClick
{
[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 MyKey key;
private Timer timer;
private volatile int posx, posy;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="windowName">Name of the window button hit is executed.</param>
/// <param name="key">Key that is to be executed.</param>
/// <param name="posx"></param>
/// <param name="posy"></param>
public ButtonClick(string windowName, MyKey key, int posx = 200, int posy = 200)
{
this.hWnd = (IntPtr)FindWindow(null, windowName);//find window with window name
this.key = key;
this.posx = posx;
this.posy = posy;
}
/// <summary>
/// Changes position coordinates.
/// </summary>
/// <param name="posx">x position.</param>
/// <param name="posy">y position.</param>
public void SetButtonPosition(int posx, int posy)
{
this.posx = posx;
this.posy = posy;
}
/// <summary>
/// Change window button is executed in.
/// </summary>
/// <param name="windowName"></param>
/// <returns></returns>
public bool SetWindowName(string windowName)
{
this.hWnd = (IntPtr)FindWindow(null, windowName);
if (this.hWnd == null)
return true;
return false;
}
/// <summary>
/// Starts the Button Clicking.
/// </summary>
public void StartClick()
{
TimerCallback tmCallback = CheckButtonExpiry;
this.timer = new Timer(tmCallback, "run", 500, key.interval * 1000);
this.isrunning = true;
}
/// <summary>
/// Stops the Button Clicking.
/// </summary>
public void StopClick()
{
this.isrunning = false;
if(this.timer != null)
this.timer.Dispose();
}
/// <summary>
/// Function is executed by timer and sends button click to window.
/// </summary>
/// <param name="objectinfo"></param>
private void CheckButtonExpiry(object objectinfo)
{
if (hWnd != null)
{
SendMessage(
hWnd,
Misc.K_KEYDOWN,
new IntPtr(key.keycode),
Misc.CreateLParam(this.posx, this.posy)
);
SendMessage(
hWnd,
Misc.K_KEYUP,
new IntPtr(key.keycode),
Misc.CreateLParam(this.posx, this.posy)
);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
namespace MouseClick
{
/// <summary>
/// Manages a List of ButtonClicks.
/// </summary>
class ButtonClickList
{
private List<KeyValuePair<MyKey, ButtonClick>> list;
private string windowName;
private bool isrunning = false;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="windowName">Name of the Window the ButtonClicks are executed in.</param>
public ButtonClickList(string windowName)
{
this.list = new List<KeyValuePair<MyKey, ButtonClick>>();
this.windowName = windowName;
}
/// <summary>
/// Adds a new button to the list and starts clicking if the list is already running.
/// </summary>
/// <param name="key">Key to add.</param>
public void AddButton(MyKey key)
{
ButtonClick value = new ButtonClick(this.windowName, key);
if (this.isrunning)
value.StartClick();
this.list.Add(new KeyValuePair <MyKey, ButtonClick>(key,value));
}
/// <summary>
/// Removes a button from the list and stops the the clicking if the button is running.
/// </summary>
/// <param name="key">Key to be removed.</param>
/// <returns>true, when button removed.
/// false, when button dosnt exist.</returns>
public bool RemoveButton(MyKey key)
{
var item = this.list.SingleOrDefault(x => x.Key.InstanceID.Equals(key.InstanceID));
if (item.Key == null)
return false;
if (item.Value.isrunning)
item.Value.StopClick();
this.list.Remove(item);
return true;
}
/// <summary>
/// Changes the windowname of all ButtonClick elements in the list.
/// </summary>
/// <param name="windowName">Name of the new Window.</param>
public void SetWindowName(string windowName)
{
this.windowName = windowName;
foreach (var l in this.list)
{
l.Value.SetWindowName(windowName);
}
}
/// <summary>
/// Starts all buttons in the list.
/// </summary>
public void StartClick()
{
this.isrunning = true;
foreach (var l in this.list) {
if (!l.Value.isrunning)
l.Value.StartClick();
}
}
/// <summary>
/// stops all buttons in the list.
/// </summary>
public void StopClick()
{
this.isrunning = false;
foreach (var l in this.list)
{
if (l.Value.isrunning)
l.Value.StopClick();
}
}
}
}
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));
}
}
}
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)
);
}
}
}
}
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