Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- namespace keysGauntlet
- {
- public partial class Form1 : Form
- {
- [DllImport("user32.dll")]
- static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
- int dwExtraInfo);
- // Get a handle to an application window.
- [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
- public static extern IntPtr FindWindow(string lpClassName,
- string lpWindowName);
- [DllImport("user32.dll")]
- private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
- [DllImport("user32.dll")]
- private static extern bool UnhookWindowsHookEx(IntPtr hhk);
- [DllImport("user32.dll")]
- private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
- [DllImport("kernel32.dll")]
- private static extern IntPtr GetModuleHandle(string lpModuleName);
- [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
- internal static extern short GetKeyState(int virtualKeyCode);
- // Activate an application window.
- [DllImport("USER32.DLL")]
- public static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- static extern IntPtr GetMessageExtraInfo();
- [DllImport("user32.dll")]
- static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);
- /* enumerate windows*/
- delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
- [DllImport("user32.dll")]
- static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,
- IntPtr lParam);
- static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
- {
- var handles = new List<IntPtr>();
- foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
- EnumThreadWindows(thread.Id,
- (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
- return handles;
- }
- private const uint WM_GETTEXT = 0x000D;
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam,
- StringBuilder lParam);
- private const int WH_KEYBOARD_LL = 13;
- private const int WH_GETMESSAGE = 3;
- private const int WM_KEYDOWN = 0x0100;
- private const int WM_KEYUP = 0x0101;
- private static LowLevelKeyboardProc hookProc2 = HookCallback2;
- private static IntPtr hookId = IntPtr.Zero;
- public IntPtr moduleHandle;
- public class Win32
- {
- [DllImport("User32.Dll")]
- public static extern long SetCursorPos(int x, int y);
- [DllImport("User32.Dll")]
- public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);
- [StructLayout(LayoutKind.Sequential)]
- public struct POINT
- {
- public int x;
- public int y;
- }
- }
- [Flags]
- private enum KeyStates
- {
- None = 0,
- Down = 1,
- Toggled = 2
- }
- 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);
- }
- private static IntPtr SetHook(LowLevelKeyboardProc hookProc, IntPtr moduleHandle)
- {
- return SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, moduleHandle, 0);
- }
- private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- moduleHandle = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
- hookId = SetHook(hookProc2, moduleHandle);
- }
- private static IntPtr HookCallback2(int nCode, IntPtr wParam, IntPtr lParam)
- {
- int vkCode = Marshal.ReadInt32(lParam);
- int returnMe = 0;
- /* user details */
- //-----------------Movement
- //W
- string key_W = "F1";
- //A
- string key_A = "F4";
- //S
- string key_S = "F3";
- //D
- string key_D = "F2";
- //-----------------Spells
- //Left click
- string key_LClick = "F12";
- //Right click
- string key_RClick = "F8";
- //Left shift
- string key_LShift = "F10";
- //Space
- string key_Space = "F9";
- //esc
- string key_Esc = "F11";
- //-----------------Other
- //Relic 1 Q
- string key_Q = "F6";
- //Relic 2 E
- string key_E = "F7";
- //Interact F
- string key_F = "F5";
- //-----------------Resolution
- int resolution_Height = 1080;
- int resolution_Width = 1920;
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
- {
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_Esc) //esc
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x01),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_LShift) //ice
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x2a),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_Space) //fire
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x39),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- // MessageBox.Show(((System.Windows.Forms.Keys)vkCode).ToString());
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_LClick) //fire
- {
- const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
- const uint MOUSEEVENTF_LEFTUP = 0x0004;
- //mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- mouse_event(MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_RClick) //fire
- {
- const uint RIGHTDOWN = 0x0008;
- const uint RIGHTUP = 0x0010;
- //mouse_event(RIGHTDOWN, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- mouse_event(RIGHTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_W)
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x11),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- else if (((System.Windows.Forms.Keys)vkCode).ToString() == key_S)
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x1f),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- else if (((System.Windows.Forms.Keys)vkCode).ToString() == key_D)
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x20),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- else if (((System.Windows.Forms.Keys)vkCode).ToString() == key_A)
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyUp((ushort)0x1e),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- }
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
- {
- //f5,6,7 is F, Q, E
- //MessageBox.Show(((System.Windows.Forms.Keys)vkCode).ToString());
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_Esc) //esc
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x01),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_F) //F
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x21),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_Q) //Q
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x10),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_E) //E
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x12),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_LShift) //ice
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x2a),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_Space) //fire
- {
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x39),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_LClick) //spell
- {
- const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
- const uint MOUSEEVENTF_LEFTUP = 0x0004;
- mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- //mouse_event(MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_RClick) //ligghtning
- {
- const uint RIGHTDOWN = 0x0008;
- const uint RIGHTUP = 0x0010;
- mouse_event(RIGHTDOWN, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- // mouse_event(RIGHTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
- }
- if (((System.Windows.Forms.Keys)vkCode).ToString() == key_W)
- {
- Keys myKey = new Keys();
- //Keys.LButton;
- myKey = Keys.D;
- Keys myKey2 = new Keys();
- myKey2 = Keys.A;
- Keys myKey3 = new Keys();
- myKey3 = Keys.LButton;
- if (!IsKeyDown(myKey3))
- {
- if (IsKeyDown(myKey))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = resolution_Width;
- int screenHeight = 0;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else if (IsKeyDown(myKey2))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = 0;
- int screenHeight = 0;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = resolution_Width / 2;
- int screenHeight = 0;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- }
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x11),
- };
- returnMe = 1;
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- }
- else if (((System.Windows.Forms.Keys)vkCode).ToString() == key_S) //down
- {
- Keys myKey = new Keys();
- myKey = Keys.A;
- Keys myKey2 = new Keys();
- myKey2 = Keys.D;
- Keys myKey3 = new Keys();
- myKey3 = Keys.LButton;
- if (!IsKeyDown(myKey3))
- {
- if (IsKeyDown(myKey))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = 0;
- int screenHeight = resolution_Height;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else if (IsKeyDown(myKey2))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = resolution_Width;
- int screenHeight = resolution_Height;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else
- {
- int screenWidth = resolution_Width / 2; //resolution_Width
- int screenHeight = resolution_Height;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- Win32.SetCursorPos(p.x, p.y);
- }
- }
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x1f),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- else if (((System.Windows.Forms.Keys)vkCode).ToString() == key_D)//right
- {
- Keys myKey = new Keys();
- myKey = Keys.W;
- Keys myKey2 = new Keys();
- myKey2 = Keys.S;
- Keys myKey3 = new Keys();
- myKey3 = Keys.LButton;
- if (!IsKeyDown(myKey3))
- {
- if (IsKeyDown(myKey))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = resolution_Width;
- int screenHeight = 0;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else if (IsKeyDown(myKey2))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = resolution_Width;
- int screenHeight = resolution_Height;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else
- {
- int screenWidth = resolution_Width; //resolution_Width
- int screenHeight = resolution_Height / 2;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- Win32.SetCursorPos(p.x, p.y);
- }
- }
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x20),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- else if (((System.Windows.Forms.Keys)vkCode).ToString() == key_A)
- {
- Keys myKey = new Keys();
- myKey = Keys.W;
- Keys myKey2 = new Keys();
- myKey2 = Keys.S;
- Keys myKey3 = new Keys();
- myKey3 = Keys.LButton;
- if (!IsKeyDown(myKey3))
- {
- if (IsKeyDown(myKey))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = 0;
- int screenHeight = 0;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else if (IsKeyDown(myKey2))
- {
- //System.Windows.Input.Keyboard.GetKeyStates(Key.Q));
- int screenWidth = 0;
- int screenHeight = resolution_Height;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- //Win32.ClientToScreen(moduleHandle, ref p);
- Win32.SetCursorPos(p.x, p.y);
- }
- else
- {
- int screenWidth = 0; //resolution_Width
- int screenHeight = resolution_Height / 2;// resolution_Height;
- Win32.POINT p = new Win32.POINT();
- p.x = Convert.ToInt16(screenWidth);
- p.y = Convert.ToInt16(screenHeight);
- Win32.SetCursorPos(p.x, p.y);
- }
- }
- INPUT[] inputs = new INPUT[] {
- INPUT.VirtualKeyDown((ushort)0x1e),
- };
- SendInput(inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
- returnMe = 1;
- }
- }
- if (returnMe == (int)1)
- {
- return (System.IntPtr)1;
- }
- else
- {
- return CallNextHookEx(hookId, nCode, wParam, lParam);
- }
- }
- /***********************************************************
- *
- * Keyboard Input
- *
- *
- * *********************************************************/
- private enum INPUTTYPE : uint
- {
- Keyboard = 1
- }
- private enum KEYEVENTF : uint
- {
- KeyUp = 2,
- Scan = 8,
- }
- [DllImport("User32.dll", SetLastError = true)]
- private static extern uint SendInput(int nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] inputs, int cbSize);
- [StructLayout(LayoutKind.Sequential)]
- private struct INPUT
- {
- public INPUTTYPE type;
- public INPUT_U u;
- public static INPUT VirtualKeyDown(ushort keyP)
- {
- var input = new INPUT() { type = INPUTTYPE.Keyboard };
- input.u.ki = new KEYBDINPUT() { scanCode = (ushort)keyP, flags = (KEYEVENTF)0x0008 };
- return input;
- }
- public static INPUT VirtualKeyUp(ushort keyP)
- {
- var input = new INPUT() { type = INPUTTYPE.Keyboard };
- input.u.ki = new KEYBDINPUT() { scanCode = (ushort)keyP, flags = (KEYEVENTF)0x0008 | (KEYEVENTF)0x0002 };
- return input;
- }
- }
- [StructLayout(LayoutKind.Explicit)]
- private struct INPUT_U
- {
- [FieldOffset(0)]
- public KEYBDINPUT ki;
- [FieldOffset(0)]
- public MOUSEINPUT mi;
- [FieldOffset(0)]
- public HARDWAREINPUT hi;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct KEYBDINPUT
- {
- public ushort virtualKey;
- public ushort scanCode;
- public KEYEVENTF flags;
- public uint time;
- public IntPtr extraInfo;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct MOUSEINPUT
- {
- public int dx;
- public int dy;
- public uint mouseData;
- public uint flags;
- public uint time;
- public IntPtr extraInfo;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct HARDWAREINPUT
- {
- public int uMsg;
- public IntPtr wParam;
- public IntPtr lParam;
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox1_MouseDown(object sender, MouseEventArgs e)
- {
- }
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- // Key currentKey = new Key();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment