Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 41 additions & 30 deletions MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 64 additions & 7 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Media;
using System.Reactive;
using System.Windows.Forms;
using System.Timers;


namespace MicMute
Expand All @@ -18,7 +19,7 @@ public partial class MainForm : Form
public CoreAudioController AudioController = new CoreAudioController();
private readonly HotkeyBinder hotkeyBinder = new HotkeyBinder();
private readonly RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\MicMute");

// toggle
private readonly string registryKeyName = "Hotkey";
private Hotkey hotkey;
Expand All @@ -45,13 +46,23 @@ enum MicStatus
}
private MicStatus currentStatus;

private bool myVisible;
private bool myVisible;
public bool MyVisible
{
get { return myVisible; }
set { myVisible = value; Visible = value; }
}

public bool key_hold = false;

// Time in milliseconds.
// Wait and revert mute state after key "release".
public static int key_hold_toggle_back_timer_wait = 1000;

private static System.Timers.Timer key_hold_toggle_back_timer;
public bool key_hold_toggled_back = true;
public bool key_hold_held = false;

public MainForm()
{
InitializeComponent();
Expand Down Expand Up @@ -84,17 +95,17 @@ private void MainForm_Load(object sender, EventArgs e)

UpdateSelectedDevice();
AudioController.AudioDeviceChanged.Subscribe(OnNextDevice);

// toggle
var hotkeyValue = registryKey.GetValue(registryKeyName);
if (hotkeyValue != null)
{
var converter = new Shortcut.Forms.HotkeyConverter();
hotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatus);
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatusCheck);
}

// mute
// mute
hotkeyValue = registryKey.GetValue(registryKeyMute);
if (hotkeyValue != null)
{
Expand All @@ -103,7 +114,7 @@ private void MainForm_Load(object sender, EventArgs e)
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey)) hotkeyBinder.Bind(muteHotkey).To(MuteMicStatus);
}

// unmute
// unmute
hotkeyValue = registryKey.GetValue(registryKeyUnmute);
if (hotkeyValue != null)
{
Expand All @@ -116,6 +127,10 @@ private void MainForm_Load(object sender, EventArgs e)
//{
// Debug.WriteLine("{0} - {1}", x.Device.Name, x.ChangedType.ToString());
//});

// track hold/release option
key_hold_toggle_back_timer = new System.Timers.Timer(key_hold_toggle_back_timer_wait);
key_hold_toggle_back_timer.Elapsed += OnToggleTimerDoneAutoToggle;
}

private void OnMuteChanged(DeviceMuteChangedArgs next)
Expand Down Expand Up @@ -181,11 +196,48 @@ private void UpdateIcon(Icon icon, string tooltipText)
this.icon.Text = tooltipText;
}

public void OnToggleTimerDoneAutoToggle(Object source, ElapsedEventArgs e)
{
// Reset.
key_hold_toggle_back_timer.Stop();
key_hold_toggled_back = true;

// Toggle to previous mute state, if key was held.
if (key_hold_held)
{
key_hold_held = false;
ToggleMicStatus();
}
}

public async void ToggleMicStatus()
{
await getSelectedDevice()?.ToggleMuteAsync();
}

public void ToggleMicStatusCheck()
{
if (key_hold)
{
// If state was toggled back, then restart key hold.
if (key_hold_toggled_back)
{
key_hold_toggled_back = false;
key_hold_toggle_back_timer.Start();
}
else
{
// Restart toggle back timer.
key_hold_held = true;
key_hold_toggle_back_timer.Stop();
key_hold_toggle_back_timer.Start();
return;
}
}

ToggleMicStatus();
}

public async void MuteMicStatus()
{
await getSelectedDevice()?.SetMuteAsync(true);
Expand Down Expand Up @@ -248,7 +300,7 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey))
{
registryKey.SetValue(registryKeyName, hotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatus);
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatusCheck);
}
}

Expand Down Expand Up @@ -364,5 +416,10 @@ private void toolStripMenuItem2_Click(object sender, EventArgs e)

UpdateSelectedDevice();
}

private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
key_hold = !key_hold;
}
}
}