-
-
-
-
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
- {
- int vkCode = Marshal.ReadInt32(lParam);
-
- switch (vkCode)
- {
- case 87:
-
-
-
-
- TB_output.Text = vkCode.ToString();
-
- break;
- }
- }
- }
Thank you guys !
Here is the entire code in case it helps...
So what I am making is a BOT for mortal kombat, it will do auto combos!
I will perform the combo manually, and the software will detect the distance in milli seconds from each keystroke I make.
Then, it will output c++ code for me so I can go back to c++ to write my code generated by realtime game input.
The first thing I tried: Use keydown / keyup event. It works BUT, if the program is not in FOCUS, it will not register the hotkeys.
The other method I tried, would register global hotkeys, but then, what ever key is registered as a global hotkey no longer works anywhere else but this program..
This other method I am trying here seems to work. It will capture my hotkeys, and the keys in question are still usable in any other window.
Problem is I ended up with this static method for the low level hook, and now I cant output my data to my textbox :'(.
Here is the probably irrelevant entire code:
**Note I am no longer using the KeyEventArgs.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Timers;
- using System.Runtime.InteropServices;
- using System.Collections;
- using System.Diagnostics;
- namespace Xetal_mk11_ComboGenerator {
- public partial class Form1: Form {
- public Form1() {
- InitializeComponent();
- System.Timers.Timer aTimer = new System.Timers.Timer();
- aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
- aTimer.Interval = 1;
- aTimer.Enabled = true;
- this.KeyPreview = true;
- _hookID = SetHook(_proc);
-
-
- }
- private
- const int WH_KEYBOARD_LL = 13;
- private
- const int WM_KEYDOWN = 0x0100;
- private
- const int WM_KEYUP = 0x0101;
- private static LowLevelKeyboardProc _proc = HookCallback;
- private static IntPtr _hookID = IntPtr.Zero;
- private static IntPtr SetHook(LowLevelKeyboardProc proc) {
- using(Process curProcess = Process.GetCurrentProcess())
- using(ProcessModule curModule = curProcess.MainModule) {
- return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
- GetModuleHandle(curModule.ModuleName), 0);
- }
- }
-
- private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
-
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- [
- return :MarshalAs(UnmanagedType.Bool)
- ]
- private 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);
- static string _code;
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
- if (nCode >= 0 && wParam == (IntPtr) WM_KEYDOWN) {
- int vkCode = Marshal.ReadInt32(lParam);
-
-
- switch (vkCode) {
- case 87:
-
- break;
- }
- }
- if (nCode >= 0 && wParam == (IntPtr) WM_KEYUP) {
- int vkCode = Marshal.ReadInt32(lParam);
-
- MessageBox.Show(vkCode.ToString());
- switch (vkCode) {
- case 87:
-
-
- break;
- }
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- public static void _keypress() {
-
-
- }
- public static void _keyrelease() {
-
- }
- string w = "0x57";
- string s = "0x53";
- string a = "0x41";
- string d = "0x44";
- string timer;
- string quadrado = "VK_INSERT";
- string triangulo = "VK_HOME";
- string r2 = "VK_PRIOR";
- string x = "VK_DELETE";
- string bolinha = "VK_END";
- string r1 = "VK_NEXT";
- string l1 = "VK_SPACE";
- string l2 = "VK_UP";
- int i = 1;
- private void OnTimedEvent(object source, ElapsedEventArgs e) {
- i++;
- try {
- this.EndInvoke(this.BeginInvoke(new MethodInvoker(delegate() {
- TB_sleep.Text = i.ToString();
- })));
- } catch {
- }
- }
- private void Form1_Load(object sender, EventArgs e) {
- }
- private void BTN_up_Click(object sender, EventArgs e) {
- }
-
-
-
- private void Form1_KeyDown(object sender, KeyEventArgs e) {
-
- timer = i.ToString();
-
- if (e.KeyCode == Keys.W) {
- TB_output.Text += "keybd_event(" + w + ", 0, 0, 0);\r\n" + "Sleep(" + timer + ");\r\n";
- i = 0;
-
- }
- if (e.KeyCode == Keys.S) {
- TB_output.Text += "keybd_event(" + s + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.A) {
- TB_output.Text += "keybd_event(" + a + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.D) {
- TB_output.Text += "keybd_event(" + d + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
-
-
- if (e.KeyCode == Keys.Insert) {
- TB_output.Text += "keybd_event(" + quadrado + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.Home) {
- TB_output.Text += "keybd_event(" + triangulo + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.PageUp) {
- TB_output.Text += "keybd_event(" + r2 + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
-
-
- if (e.KeyCode == Keys.Delete) {
- TB_output.Text += "keybd_event(" + x + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.End) {
- TB_output.Text += "keybd_event(" + bolinha + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.PageDown) {
- TB_output.Text += "keybd_event(" + r1 + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
-
-
- if (e.KeyCode == Keys.Space) {
- TB_output.Text += "keybd_event(" + l1 + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
- if (e.KeyCode == Keys.Up) {
- TB_output.Text += "keybd_event(" + l2 + ", 0, 0, 0);\r\n" + "Sleep(15);\r\n";
- i = 0;
- }
-
-
- if (e.KeyCode == Keys.Enter) {
- TB_output.Text += "Sleep(15);\r\n";
- }
-
- if (e.KeyCode == Keys.OemPeriod) {
- TB_output.Text = "";;
- }
-
- if (e.KeyCode == Keys.F10) {
- Clipboard.Clear();
- Clipboard.SetText(TB_output.Text);
- }
- }
-
-
-
- private void Form1_KeyUp(object sender, KeyEventArgs e) {
-
- if (e.KeyCode == Keys.W) {
- TB_output.Text += "keybd_event(" + w + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.S) {
- TB_output.Text += "keybd_event(" + s + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.A) {
- TB_output.Text += "keybd_event(" + a + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.D) {
- TB_output.Text += "keybd_event(" + d + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
-
-
- if (e.KeyCode == Keys.Insert) {
- TB_output.Text += "keybd_event(" + quadrado + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.Home) {
- TB_output.Text += "keybd_event(" + triangulo + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.PageUp) {
- TB_output.Text += "keybd_event(" + r2 + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
-
-
- if (e.KeyCode == Keys.Delete) {
- TB_output.Text += "keybd_event(" + x + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.End) {
- TB_output.Text += "keybd_event(" + bolinha + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.PageDown) {
- TB_output.Text += "keybd_event(" + r1 + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
-
- if (e.KeyCode == Keys.Space) {
- TB_output.Text += "keybd_event(" + l1 + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- if (e.KeyCode == Keys.Up) {
- TB_output.Text += "keybd_event(" + l2 + ", 0, KEYEVENTF_KEYUP, 0);\r\n";
- }
- }
- private void BTN_clear_Click(object sender, EventArgs e) {
- TB_output.Text = "";
- }
- }
- }