-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
183 lines (154 loc) · 6.65 KB
/
Copy pathmain.c
File metadata and controls
183 lines (154 loc) · 6.65 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* winenter - A lightweight Windows utility to launch Command Prompt with Win+Enter
*
* A lightweight Windows utility that registers a system-wide low-level keyboard hook
* to launch the Command Prompt (cmd.exe) in the user's profile directory using Win+Enter.
* It runs quietly in the system tray and prevents the OS from triggering default actions.
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include "resource.h"
// Auto-link required libraries for MSVC users
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "shell32.lib")
// Custom message identifiers
#define WM_TRAYICON (WM_USER + 1)
#define ID_TRAY_EXIT 1001
#define ID_TRAY_CMD 1002
#define ID_TRAY_POWERSHELL 1003
#define SHELL_CMD 0
#define SHELL_POWERSHELL 1
#define WINENTER_REG_PATH "Software\\WinEnter"
#define WINENTER_REG_VALUE "SelectedShell"
int selectedShell = SHELL_CMD;
NOTIFYICONDATA nid;
HHOOK hKeyboardHook;
static int LoadSelectedShell(void) {
HKEY hKey;
DWORD shell = SHELL_CMD;
DWORD dataSize = sizeof(shell);
if (RegOpenKeyEx(HKEY_CURRENT_USER, WINENTER_REG_PATH, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
if (RegGetValue(hKey, NULL, WINENTER_REG_VALUE, RRF_RT_REG_DWORD, NULL, &shell, &dataSize) != ERROR_SUCCESS) {
shell = SHELL_CMD;
}
RegCloseKey(hKey);
}
return (shell == SHELL_POWERSHELL) ? SHELL_POWERSHELL : SHELL_CMD;
}
static void SaveSelectedShell(int shell) {
HKEY hKey;
DWORD value = (DWORD)shell;
DWORD disposition;
if (RegCreateKeyEx(HKEY_CURRENT_USER, WINENTER_REG_PATH, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &disposition) == ERROR_SUCCESS) {
RegSetValueEx(hKey, WINENTER_REG_VALUE, 0, REG_DWORD, (const BYTE *)&value, sizeof(value));
RegCloseKey(hKey);
}
}
// Low-Level Keyboard Hook Procedure
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) {
KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
if (pKeyBoard->vkCode == VK_RETURN) {
// Check if either the Left or Right Windows key is held down
if ((GetAsyncKeyState(VK_LWIN) & 0x8000) || (GetAsyncKeyState(VK_RWIN) & 0x8000)) {
char userProfile[MAX_PATH];
const char* shellExe = (selectedShell == SHELL_POWERSHELL) ? "powershell.exe" : "cmd.exe";
// Fallback to default directory if USERPROFILE is not found
if (GetEnvironmentVariable("USERPROFILE", userProfile, MAX_PATH)) {
ShellExecute(NULL, "open", shellExe, NULL, userProfile, SW_SHOWNORMAL);
} else {
ShellExecute(NULL, "open", shellExe, NULL, NULL, SW_SHOWNORMAL);
}
return 1; // Block the keypress from reaching other applications (e.g., Narrator)
}
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
// The main window procedure that handles messages
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE:
// 1. Install Low-Level Keyboard Hook to bypass OS restrictions
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
if (hKeyboardHook == NULL) {
MessageBox(hwnd, "Failed to install keyboard hook.", "Hook Error", MB_ICONERROR);
}
// 2. Configure and add the system tray icon
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_TRAYICON;
nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APP_ICON)); // Custom icon
lstrcpy(nid.szTip, "Press Win+Enter to launch CMD");
Shell_NotifyIcon(NIM_ADD, &nid);
break;
case WM_TRAYICON:
// 4. Handle right-clicks on the system tray icon
if (lParam == WM_RBUTTONUP) {
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | (selectedShell == SHELL_CMD ? MF_CHECKED : MF_UNCHECKED), ID_TRAY_CMD, "Command Prompt");
AppendMenu(hMenu, MF_STRING | (selectedShell == SHELL_POWERSHELL ? MF_CHECKED : MF_UNCHECKED), ID_TRAY_POWERSHELL, "PowerShell");
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, ID_TRAY_EXIT, "Exit");
SetForegroundWindow(hwnd); // Required to make the menu close properly
int cmd = TrackPopupMenu(hMenu, TPM_RETURNCMD | TPM_NONOTIFY, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
if (cmd == ID_TRAY_EXIT) {
PostMessage(hwnd, WM_CLOSE, 0, 0);
} else if (cmd == ID_TRAY_CMD) {
selectedShell = SHELL_CMD;
SaveSelectedShell(selectedShell);
} else if (cmd == ID_TRAY_POWERSHELL) {
selectedShell = SHELL_POWERSHELL;
SaveSelectedShell(selectedShell);
}
}
break;
case WM_DESTROY:
// 5. Clean up before exiting
if (hKeyboardHook) {
UnhookWindowsHookEx(hKeyboardHook);
}
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
// WinMain prevents a standard console window from appearing
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
selectedShell = LoadSelectedShell();
const char CLASS_NAME[] = "CmdLauncherClass";
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClassEx(&wc);
// Create a hidden message-handling window
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"winenter",
0, // Passing 0 instead of WS_OVERLAPPEDWINDOW keeps it hidden
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL
);
if (hwnd == NULL) {
return 0;
}
// Standard message loop
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}