Skip to content

Commit

Permalink
Added parameters /tray and /minimized to start this application in th…
Browse files Browse the repository at this point in the history
…e background
  • Loading branch information
mkanzler committed Apr 20, 2024
1 parent 5869473 commit d9ca884
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 37 deletions.
Binary file added AutoTyper/AutoTyper-Small.ico
Binary file not shown.
Binary file added AutoTyper/AutoTyper.aps
Binary file not shown.
104 changes: 93 additions & 11 deletions AutoTyper/AutoTyper.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#include <Windows.h>
#include <string>
#include <iostream>
#include "resource.h"


HWND currentWindow;
NOTIFYICONDATA g_nid;
HINSTANCE gHInstance;
#define UNIQUE_MK_AUTOTYPER L"MK-AutoTyper"

void PressKey(BYTE keyCode, bool keyCtrl = false, bool keyAlt = false, bool keyShift = false) {
if (keyShift) keybd_event(VK_LSHIFT, 0, 0, 0);
if (keyCtrl) keybd_event(VK_LCONTROL, 0, 0, 0);
Expand Down Expand Up @@ -106,12 +112,43 @@ int getIKeyStrokeDelay() {
return iKeyStrokeDelay;
}

// Function to create systray icon
void CreateSystemTrayIcon(HWND hWnd) {
g_nid.cbSize = sizeof(NOTIFYICONDATA);
g_nid.hWnd = hWnd;
g_nid.uID = 1;
g_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
g_nid.uCallbackMessage = WM_USER + 1;
g_nid.hIcon = LoadIcon(gHInstance, MAKEINTRESOURCE(IDI_SMALL));
lstrcpy(g_nid.szTip, TEXT("MK AutoTyper"));

Shell_NotifyIcon(NIM_ADD, &g_nid);
}

// Function to handle right-click context menu
void ShowContextMenu(HWND hWnd) {
POINT pt;
GetCursorPos(&pt);

HMENU hMenu = CreatePopupMenu();
InsertMenu(hMenu, -1, MF_BYPOSITION | MF_STRING, 1, TEXT("Exit"));

SetForegroundWindow(hWnd); // Set the window as foreground
TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
DestroyMenu(hMenu);
}

// Function to remove systray icon
void RemoveSystemTrayIcon() {
Shell_NotifyIcon(NIM_DELETE, &g_nid);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
textboxTypeContent = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 10, 10, 760, 500, hwnd, NULL, GetModuleHandle(NULL), NULL);
CreateWindow(L"BUTTON", L"TYPE", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10, 520, 100, 30, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
HWND labelKeyStrokeDelay = CreateWindowEx(0, L"STATIC", L"Keystroke delay (ms):", WS_CHILD | WS_VISIBLE,130, 520, 120, 30,hwnd,NULL,GetModuleHandle(NULL),NULL);
HWND labelKeyStrokeDelay = CreateWindowEx(0, L"STATIC", L"Keystroke delay (ms):", WS_CHILD | WS_VISIBLE, 130, 520, 120, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
txtKeyStrokeDelay = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"10", WS_CHILD | ES_NUMBER | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 255, 520, 100, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
HWND labelInitialDelay = CreateWindowEx(0, L"STATIC", L"Initial delay (ms):", WS_CHILD | WS_VISIBLE, 385, 520, 120, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
txtInitialDelay = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"1000", WS_CHILD | ES_NUMBER | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 510, 520, 100, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
Expand All @@ -120,9 +157,29 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
}
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case 1: { // ID of the button
simulateKeystrokes(getTxtBoxString(textboxTypeContent), getIKeyStrokeDelay(), getiInitialDelay());
break;
case 1: { // ID of the button
if (HIWORD(wParam) == 0) {
RemoveSystemTrayIcon();
PostQuitMessage(0);
}
simulateKeystrokes(getTxtBoxString(textboxTypeContent), getIKeyStrokeDelay(), getiInitialDelay());
break;
}
}
break;
}
case WM_CLOSE: {
ShowWindow(currentWindow, SW_HIDE);
}
case WM_USER+1: { // Systray icon message
switch (lParam) {
case WM_LBUTTONUP: { // Left click on systray icon
ShowWindow(currentWindow, SW_SHOW);
ShowWindow(currentWindow, SW_NORMAL);
break;
}
case WM_RBUTTONDOWN: {
ShowContextMenu(currentWindow);
}
}
break;
Expand Down Expand Up @@ -178,24 +235,49 @@ void setHook() {
// Entry point of the application
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
setHook(); // Set the keyboard hook
// Register the window class
const wchar_t CLASS_NAME[] = L"MK-AutoTyper";

// Attempt to create a named mutex
HANDLE hMutex = CreateMutex(NULL, TRUE, UNIQUE_MK_AUTOTYPER);
// Check if the mutex already exists
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
HWND hOtherWnd = FindWindow(UNIQUE_MK_AUTOTYPER, NULL);

ShowWindow(hOtherWnd, SW_SHOW);
return 0;
}

WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
gHInstance = hInstance;

wc.lpszClassName = UNIQUE_MK_AUTOTYPER;

RegisterClass(&wc);

// Create the window
HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"MK AutoTyper", WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_HIDE);
if (hwnd == NULL) {
currentWindow = CreateWindowEx(0, UNIQUE_MK_AUTOTYPER, L"MK AutoTyper", WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
if (currentWindow == NULL) {
return 0;
}
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_AUTOTYPER));
HICON hIconSmall = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));
SendMessage(currentWindow, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
SendMessage(currentWindow, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall);


std::string cmdLine(lpCmdLine);

ShowWindow(hwnd, nCmdShow);

if (!(cmdLine.find("/tray") != std::string::npos)) {
ShowWindow(currentWindow, SW_SHOW);
}
if ((cmdLine.find("/minimized") != std::string::npos)) {
ShowWindow(currentWindow, SW_SHOWMINIMIZED);
}
CreateSystemTrayIcon(currentWindow);

// Run the message loop
MSG msg = {};
Expand Down
Binary file modified AutoTyper/AutoTyper.ico
Binary file not shown.
Binary file modified AutoTyper/AutoTyper.rc
Binary file not shown.
8 changes: 4 additions & 4 deletions AutoTyper/AutoTyper.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
Expand All @@ -22,9 +22,9 @@
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{8e7e5f83-b94f-4ec0-9023-7c4684bb5e9f}</ProjectGuid>
<RootNamespace>AutoTyper</RootNamespace>
<RootNamespace>MK-AutoTyper</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>MK AutoTyper</ProjectName>
<ProjectName>MK-AutoTyper</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -153,8 +153,8 @@
<ResourceCompile Include="AutoTyper.rc" />
</ItemGroup>
<ItemGroup>
<Image Include="AutoTyper-Small.ico" />
<Image Include="AutoTyper.ico" />
<Image Include="small.ico" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
2 changes: 1 addition & 1 deletion AutoTyper/AutoTyper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<Image Include="small.ico">
<Image Include="AutoTyper-Small.ico">
<Filter>Resource Files</Filter>
</Image>
<Image Include="AutoTyper.ico">
Expand Down
40 changes: 19 additions & 21 deletions AutoTyper/Resource.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by AutoTyper.rc
//
#define IDC_MYICON 2
#define IDD_AUTOTYPER_DIALOG 102
#define IDS_APP_TITLE 103
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDI_AUTOTYPER 107
#define IDI_SMALL 108
#define IDC_AUTOTYPER 109
#define IDR_MAINFRAME 128
#define IDI_ICON3 132
#define IDC_STATIC -1

#define IDS_APP_TITLE 103

#define IDR_MAINFRAME 128
#define IDD_AUTOTYPER_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDI_AUTOTYPER 107
#define IDI_SMALL 108
#define IDC_AUTOTYPER 109
#define IDC_MYICON 2
#ifndef IDC_STATIC
#define IDC_STATIC -1
#endif
// Next default values for new objects
//
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NO_MFC 130
#define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 136
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
Binary file removed AutoTyper/small.ico
Binary file not shown.
Binary file modified Screenshots/UI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d9ca884

Please sign in to comment.