diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 49c6bee..a2167ff 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -34,6 +34,7 @@ private void InitializeComponent() this.iconContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components); this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.hotkeyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.hotkeyTextBox = new Shortcut.Forms.HotkeyTextBox(); this.label1 = new System.Windows.Forms.Label(); @@ -46,47 +47,56 @@ private void InitializeComponent() this.unmuteTextBox = new Shortcut.Forms.HotkeyTextBox(); this.iconContextMenu.SuspendLayout(); this.SuspendLayout(); - // + // // icon - // + // this.icon.ContextMenuStrip = this.iconContextMenu; this.icon.Text = ""; this.icon.Visible = true; this.icon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Icon_MouseClick); - // + // // iconContextMenu - // + // this.iconContextMenu.ImageScalingSize = new System.Drawing.Size(32, 32); this.iconContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem2, this.hotkeyToolStripMenuItem, + this.toolStripMenuItem3, this.toolStripMenuItem1}); this.iconContextMenu.Name = "iconContextMenu"; this.iconContextMenu.Size = new System.Drawing.Size(200, 118); - // + // // toolStripMenuItem2 - // + // this.toolStripMenuItem2.Name = "toolStripMenuItem2"; this.toolStripMenuItem2.Size = new System.Drawing.Size(199, 38); this.toolStripMenuItem2.Text = "Select mic"; this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_Click); - // + // // hotkeyToolStripMenuItem - // + // this.hotkeyToolStripMenuItem.Name = "hotkeyToolStripMenuItem"; this.hotkeyToolStripMenuItem.Size = new System.Drawing.Size(199, 38); this.hotkeyToolStripMenuItem.Text = "Hotkey"; this.hotkeyToolStripMenuItem.Click += new System.EventHandler(this.HotkeyToolStripMenuItem_Click); - // + // + // toolStripMenuItem3 + // + this.toolStripMenuItem3.Name = "toolStripMenuItem3"; + this.toolStripMenuItem3.Size = new System.Drawing.Size(199, 38); + this.toolStripMenuItem3.Text = "Hold Key Mode"; + this.toolStripMenuItem3.Click += new System.EventHandler(this.toolStripMenuItem3_Click); + this.toolStripMenuItem3.CheckOnClick = true; + // // toolStripMenuItem1 - // + // this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(199, 38); this.toolStripMenuItem1.Text = "Exit"; this.toolStripMenuItem1.Click += new System.EventHandler(this.ExitMenuItem_Click); - // + // // hotkeyTextBox - // + // this.hotkeyTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.hotkeyTextBox.Hotkey = null; this.hotkeyTextBox.Location = new System.Drawing.Point(24, 78); @@ -95,9 +105,9 @@ private void InitializeComponent() this.hotkeyTextBox.Size = new System.Drawing.Size(494, 44); this.hotkeyTextBox.TabIndex = 1; this.hotkeyTextBox.Text = "None"; - // + // // label1 - // + // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.label1.Location = new System.Drawing.Point(26, 32); @@ -106,9 +116,9 @@ private void InitializeComponent() this.label1.Size = new System.Drawing.Size(647, 37); this.label1.TabIndex = 2; this.label1.Text = "Register toggle hotkey (auto saved on close)"; - // + // // buttonReset - // + // this.buttonReset.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.buttonReset.Location = new System.Drawing.Point(530, 75); this.buttonReset.Margin = new System.Windows.Forms.Padding(6); @@ -118,9 +128,9 @@ private void InitializeComponent() this.buttonReset.Text = "reset"; this.buttonReset.UseVisualStyleBackColor = true; this.buttonReset.Click += new System.EventHandler(this.ButtonReset_Click); - // + // // label2 - // + // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.label2.Location = new System.Drawing.Point(26, 148); @@ -129,9 +139,9 @@ private void InitializeComponent() this.label2.Size = new System.Drawing.Size(631, 37); this.label2.TabIndex = 4; this.label2.Text = "Register mute hotkey (auto saved on close)"; - // + // // muteTextBox - // + // this.muteTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.muteTextBox.Hotkey = null; this.muteTextBox.Location = new System.Drawing.Point(24, 201); @@ -140,9 +150,9 @@ private void InitializeComponent() this.muteTextBox.Size = new System.Drawing.Size(494, 44); this.muteTextBox.TabIndex = 5; this.muteTextBox.Text = "None"; - // + // // muteReset - // + // this.muteReset.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.muteReset.Location = new System.Drawing.Point(530, 201); this.muteReset.Margin = new System.Windows.Forms.Padding(6); @@ -152,9 +162,9 @@ private void InitializeComponent() this.muteReset.Text = "reset"; this.muteReset.UseVisualStyleBackColor = true; this.muteReset.Click += new System.EventHandler(this.muteReset_Click); - // + // // label3 - // + // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.label3.Location = new System.Drawing.Point(26, 267); @@ -163,9 +173,9 @@ private void InitializeComponent() this.label3.Size = new System.Drawing.Size(667, 37); this.label3.TabIndex = 7; this.label3.Text = "Register unmute hotkey (auto saved on close)"; - // + // // unmuteReset - // + // this.unmuteReset.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.unmuteReset.Location = new System.Drawing.Point(530, 310); this.unmuteReset.Margin = new System.Windows.Forms.Padding(6); @@ -175,9 +185,9 @@ private void InitializeComponent() this.unmuteReset.Text = "reset"; this.unmuteReset.UseVisualStyleBackColor = true; this.unmuteReset.Click += new System.EventHandler(this.unmuteReset_Click); - // + // // unmuteTextBox - // + // this.unmuteTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.unmuteTextBox.Hotkey = null; this.unmuteTextBox.Location = new System.Drawing.Point(24, 310); @@ -186,9 +196,9 @@ private void InitializeComponent() this.unmuteTextBox.Size = new System.Drawing.Size(494, 44); this.unmuteTextBox.TabIndex = 8; this.unmuteTextBox.Text = "None"; - // + // // MainForm - // + // this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(699, 377); @@ -218,6 +228,7 @@ private void InitializeComponent() private System.Windows.Forms.NotifyIcon icon; private System.Windows.Forms.ContextMenuStrip iconContextMenu; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1; + private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem3; private Shortcut.Forms.HotkeyTextBox hotkeyTextBox; private System.Windows.Forms.Label label1; private System.Windows.Forms.ToolStripMenuItem hotkeyToolStripMenuItem; diff --git a/MainForm.cs b/MainForm.cs index 2e9c051..10bf90f 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -8,6 +8,7 @@ using System.Media; using System.Reactive; using System.Windows.Forms; +using System.Timers; namespace MicMute @@ -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; @@ -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(); @@ -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) { @@ -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) { @@ -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) @@ -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); @@ -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); } } @@ -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; + } } }