-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
157 lines (133 loc) · 5.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using PalWorldPressF;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
class Program
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const byte VK_F = 0x46; // Virtual key code for 'F'
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static bool toggle = false;
private static Thread sendFThread;
private static IntPtr gameHandle = IntPtr.Zero;
public static StatusForm statusForm;
public static bool isApplicationRunning = true;
[STAThread]
public static void Main()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
try
{
ProcessStartInfo procInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = Application.ExecutablePath,
Verb = "runas"
};
Process.Start(procInfo);
Application.Exit();
}
catch (Exception ex)
{
MessageBox.Show("This application requires administrator privileges. " + ex.Message);
return;
}
}
else
{
statusForm = new StatusForm();
_hookID = SetHook(_proc);
sendFThread = new Thread(SendFLoop);
sendFThread.IsBackground = true;
sendFThread.Start();
Application.Run(statusForm);
UnhookWindowsHookEx(_hookID);
}
}
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);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Debug.WriteLine("Key pressed: " + (Keys)vkCode); // Debugging
if ((Keys)vkCode == Keys.P && (Control.ModifierKeys & Keys.Control) == Keys.Control)
{
toggle = !toggle;
Debug.WriteLine("CTRL+P pressed - Toggle is now " + toggle); // Debugging
// Update status in the window
statusForm?.Invoke(new Action(() =>
{
statusForm.UpdateStatus(toggle);
}));
if (toggle)
{
Process[] processes = Process.GetProcessesByName("Palworld-Win64-Shipping");
if (processes.Length > 0)
{
gameHandle = processes[0].MainWindowHandle;
statusForm.Invoke(new Action(() => statusForm.AppendDebugMessage("Palworld handle found: " + gameHandle)));
}
else
{
statusForm.Invoke(new Action(() => statusForm.AppendDebugMessage("PALWORLD not found.")));
}
}
return (IntPtr)1; // Prevent further processing of the key
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private static void SendFLoop()
{
while (isApplicationRunning)
{
if (toggle && gameHandle != IntPtr.Zero)
{
// Simulate a continuous press of the 'F' key
PostMessage(gameHandle, WM_KEYDOWN, VK_F, 0);
}
// No action needed in the "else" case, as no key press will be sent
Thread.Sleep(100); // Delay to prevent too rapid execution
}
}
// DLLImports and function signatures follow, enabling the low-level keyboard hook, message posting, and other Windows API interactions
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out IntPtr ProcessId);
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int 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);
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
}