- //Hello there guys.
- //This is my static method. It is a low level hotkey hook.
- //I need to be able to send outputs to TB_output. What's the best way to achieve this?
- 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: // this works. breakpoint hits.
- // I want to be able to change the value of my TB_output
- // textbox from here. What's the best way?
- TB_output.Text = vkCode.ToString(); // obviously this doesn't work :'(
- break;
- }
- }
- }
Trying to change value of textbox.txt from a static method.
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);
- // Application.Run();
- //UnhookWindowsHookEx(_hookID);
- }
- 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);
- //Console.WriteLine((Keys)vkCode);
- //MessageBox.Show(vkCode.ToString());
- switch (vkCode) {
- case 87:
- // _keypress();
- break;
- }
- }
- if (nCode >= 0 && wParam == (IntPtr) WM_KEYUP) {
- int vkCode = Marshal.ReadInt32(lParam);
- //Console.WriteLine((Keys)vkCode);
- MessageBox.Show(vkCode.ToString());
- switch (vkCode) {
- case 87:
- // MessageBox.Show("You Release W");
- // _keyrelease();
- break;
- }
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- public static void _keypress() {
- // _code = "";
- }
- public static void _keyrelease() {
- // MessageBox.Show("Fuck You too");
- }
- string w = "0x57"; // 87
- string s = "0x53"; // 83
- string a = "0x41"; // 65
- string d = "0x44"; // 68
- string timer;
- string quadrado = "VK_INSERT"; // front punch //45
- string triangulo = "VK_HOME"; // back punch //36
- string r2 = "VK_PRIOR"; // block //33
- string x = "VK_DELETE"; // front kick //46
- string bolinha = "VK_END"; // back kick //35
- string r1 = "VK_NEXT"; // interact //34
- string l1 = "VK_SPACE"; // throw //32
- string l2 = "VK_UP"; // stance flip //38
- 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) {
- }
- //
- // Press keys
- //
- private void Form1_KeyDown(object sender, KeyEventArgs e) {
- //Form1_KeyDown(sender, null);
- timer = i.ToString();
- //directionals
- 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;
- }
- //3 top
- 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;
- }
- //3 bot
- 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;
- }
- //throw and stance
- 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;
- }
- // sleep
- 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(); //Clear if any old value is there in Clipboard
- Clipboard.SetText(TB_output.Text);
- }
- }
- //
- //Release Keys
- //
- private void Form1_KeyUp(object sender, KeyEventArgs e) {
- //directionals
- 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";
- }
- //3 top
- 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";
- }
- //3 bot
- 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";
- }
- //throw and stance
- 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 = "";
- }
- }
- }
Mario ScheerenPosted Jun 13, 2020, 2:55 AM
thiago costaPosted Jun 13, 2020, 8:57 AM