Cześć,
tworzę autoclickera(do gier oczywiście) i pewnie jak na to forum jest to dość błachy temat, ale od czegoś trzeba zacząć, no więc:
chciałem, aby w tle aplikacja przechwytywała event kliknięcia klawisza(dla przykładu 'X') i wykonywała wybraną ilość kliknięć w trakcie sekundy.
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (Keyboard.IsKeyDown(Keys.X)) //używałem równierz Keyboard.IsKeyToggled(Keys.X);
{
LeftClickManager.instance.click();
}
}
oczywiście podczas uruchomienia aplikacji wykonuję bgWorker.RunWorkerAsync();
.
LeftClickManager.cs:
public class LeftClickManager
{
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Keys vKey);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int LEFTUP = 0x0004;
private const int LEFTDOWN = 0x0002;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
private static string GetCaptionOfActiveWindow()
{
var strTitle = string.Empty;
var handle = GetForegroundWindow();
// Obtain the length of the text
var intLength = GetWindowTextLength(handle) + 1;
var stringBuilder = new StringBuilder(intLength);
if (GetWindowText(handle, stringBuilder, intLength) > 0)
{
strTitle = stringBuilder.ToString();
}
return strTitle;
}
static int cps;
static string windowName;
static bool windowChecking;
public static LeftClickManager instance { get; set; }
public LeftClickManager()
{
instance = this;
cps = Properties.CPSSettings.Default.LeftClickCPSMin / 1000;
windowName = Properties.Settings.Default.windowName;
windowChecking = Properties.Settings.Default.windowChecking;
}
public void click()
{
mouse_event(dwFlags: LEFTUP, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
mouse_event(dwFlags: LEFTDOWN, dx: 0, dy: 0, cButtons: 0, dwExtraInfo: 0);
Thread.Sleep(1);
}
}
Keyboard.cs:
public abstract class Keyboard
{
[Flags]
private enum KeyStates
{
None = 0,
Down = 1,
Toggled = 2
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern short GetKeyState(int keyCode);
private static KeyStates GetKeyState(Keys key)
{
KeyStates state = KeyStates.None;
short retVal = GetKeyState((int)key);
//If the high-order bit is 1, the key is down
//otherwise, it is up.
if ((retVal & 0x8000) == 0x8000)
state |= KeyStates.Down;
//If the low-order bit is 1, the key is toggled.
if ((retVal & 1) == 1)
state |= KeyStates.Toggled;
return state;
}
public static bool IsKeyDown(Keys key)
{
return KeyStates.Down == (GetKeyState(key) & KeyStates.Down);
}
public static bool IsKeyToggled(Keys key)
{
return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled);
}
}