-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicmutetokey.cpp
93 lines (84 loc) · 3.27 KB
/
micmutetokey.cpp
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
#include <windows.h>
#include <stdio.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <fstream>
#include "INIReader.h"
int GetMute() {
HRESULT hr;
CoInitialize(NULL);
IMMDeviceEnumerator* deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&deviceEnumerator);
IMMDevice* defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
IAudioEndpointVolume* endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID*)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
BOOL mute;
hr = endpointVolume->GetMute(&mute);
endpointVolume->Release();
CoUninitialize();
return mute ? 1 : 0;
}
int main()
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
INIReader reader("config.ini");
if (reader.ParseError() != 0) {
printf("Can't load 'config.ini'\n");
std::ofstream ConfigFile("config.ini");
ConfigFile << "[General]\n";
ConfigFile << "refreshRate=100\n\n";
ConfigFile << "[Keys]\n";
ConfigFile << "onMute=0x82\n";
ConfigFile << "onUnmute=0x82\n";
ConfigFile << "; Here are all key codes: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes\n";
ConfigFile << "; By default, both keys are f19\n";
printf("Succesfully created config file\n");
}
printf("Config loaded from 'config.ini'\n");
int muteKeyCode = reader.GetInteger("Keys", "onMute", 0x82); // virtual key-code to simulate on mute
int unmuteKeyCode = reader.GetInteger("Keys", "onUnmute", 0x82); // virtual key-code to simulate on unmute
int refreshRate = 1000 / reader.GetInteger("General", "refreshRate", 100);
int currentMicMuteStatus = GetMute();
int oldCurrentMicMuteStatus = currentMicMuteStatus;
while (true)
{
Sleep(refreshRate);
oldCurrentMicMuteStatus = currentMicMuteStatus;
currentMicMuteStatus = GetMute();
if (oldCurrentMicMuteStatus != currentMicMuteStatus)
{
if (currentMicMuteStatus == 0)
{
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wScan = 0;
inp.ki.time = 0;
inp.ki.dwExtraInfo = 0;
inp.ki.wVk = muteKeyCode; // virtual-key code
inp.ki.dwFlags = 0; // 0 for key press
SendInput(1, &inp, sizeof(INPUT));
inp.ki.dwFlags = KEYEVENTF_KEYUP; // for key release
SendInput(1, &inp, sizeof(INPUT));
}
else
{
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wScan = 0;
inp.ki.time = 0;
inp.ki.dwExtraInfo = 0;
inp.ki.wVk = unmuteKeyCode; // virtual-key code
inp.ki.dwFlags = 0; // 0 for key press
SendInput(1, &inp, sizeof(INPUT));
inp.ki.dwFlags = KEYEVENTF_KEYUP; // for key release
SendInput(1, &inp, sizeof(INPUT));
}
}
}
return 0;
}