From a0798f285fd545868630ea11021360a643aaf049 Mon Sep 17 00:00:00 2001 From: KHA Date: Mon, 11 Jan 2021 01:43:09 +0630 Subject: [PATCH] unbind individual hotkey --- SmartHomeWin/Home.Designer.cs | 16 ++++++++++++++- SmartHomeWin/Home.cs | 38 ++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/SmartHomeWin/Home.Designer.cs b/SmartHomeWin/Home.Designer.cs index 27d2936..0d2da70 100644 --- a/SmartHomeWin/Home.Designer.cs +++ b/SmartHomeWin/Home.Designer.cs @@ -50,6 +50,7 @@ private void InitializeComponent() this.tsmHelp = new System.Windows.Forms.ToolStripMenuItem(); this.tsAbout = new System.Windows.Forms.ToolStripMenuItem(); this.btnUnbindKeys = new System.Windows.Forms.Button(); + this.btnUnbindHotkey = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // @@ -207,7 +208,7 @@ private void InitializeComponent() // btnUnbindKeys // this.btnUnbindKeys.Enabled = false; - this.btnUnbindKeys.Location = new System.Drawing.Point(830, 196); + this.btnUnbindKeys.Location = new System.Drawing.Point(830, 247); this.btnUnbindKeys.Name = "btnUnbindKeys"; this.btnUnbindKeys.Size = new System.Drawing.Size(100, 33); this.btnUnbindKeys.TabIndex = 6; @@ -215,11 +216,23 @@ private void InitializeComponent() this.btnUnbindKeys.UseVisualStyleBackColor = true; this.btnUnbindKeys.Click += new System.EventHandler(this.BtnUnbindKeys_Click); // + // btnUnbindHotkey + // + this.btnUnbindHotkey.Enabled = false; + this.btnUnbindHotkey.Location = new System.Drawing.Point(830, 197); + this.btnUnbindHotkey.Name = "btnUnbindHotkey"; + this.btnUnbindHotkey.Size = new System.Drawing.Size(100, 33); + this.btnUnbindHotkey.TabIndex = 8; + this.btnUnbindHotkey.Text = "Unbind Hotkey"; + this.btnUnbindHotkey.UseVisualStyleBackColor = true; + this.btnUnbindHotkey.Click += new System.EventHandler(this.BtnUnbindHotkey_Click); + // // Home // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(952, 413); + this.Controls.Add(this.btnUnbindHotkey); this.Controls.Add(this.btnUnbindKeys); this.Controls.Add(this.btnBindHotKey); this.Controls.Add(this.btnOnOff); @@ -262,6 +275,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem menuAutoStart; private System.Windows.Forms.Button btnUnbindKeys; private System.Windows.Forms.ToolStripMenuItem tsAbout; + private System.Windows.Forms.Button btnUnbindHotkey; } } diff --git a/SmartHomeWin/Home.cs b/SmartHomeWin/Home.cs index 76bb65c..c4c0f41 100644 --- a/SmartHomeWin/Home.cs +++ b/SmartHomeWin/Home.cs @@ -79,12 +79,13 @@ protected override void WndProc(ref Message m) KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); int id = m.WParam.ToInt32(); - Debug.WriteLine($"id : {id}"); - Debug.WriteLine($"modifier : {modifier}"); - Debug.WriteLine($"key : {key}"); + Debug.WriteLine($"id: {id}"); + // Debug.WriteLine($"modifier : {modifier}"); + // Debug.WriteLine($"key : {key}"); var dev = Program.tuya.DevicesList.Where(dev => dev.Id == hotkeyBinding[id].DevId).FirstOrDefault(); + int state = dev.Dev_type == "scene" ? 0 : Convert.ToInt32(dev.Data.State); Program.tuya.ControlDevicesAsync(hotkeyBinding[id].DevId, state ^ 1); @@ -96,14 +97,16 @@ protected override void WndProc(ref Message m) private void BtnRefresh_Click(object sender, EventArgs e) { - btnOnOff.Text = "ON/OFF"; - btnOnOff.Enabled = false; - btnBindHotKey.Enabled = false; RefreshDevicesList(true); } private async void RefreshDevicesList(bool hardRefresh = false) { + btnOnOff.Text = "ON/OFF"; + btnOnOff.Enabled = false; + btnBindHotKey.Enabled = false; + btnUnbindHotkey.Enabled = false; + if (hardRefresh) await Program.tuya.DiscoverDevices(); @@ -147,12 +150,15 @@ private void DeviceListView_SelectedIndexChanged(object sender, EventArgs e) btnOnOff.Text = deviceListView.SelectedItems[0].SubItems[2].Text == "scene" ? "TRIGGER" : "ON"; btnOnOff.Enabled = true; btnBindHotKey.Enabled = true; + var selectDeviceHK = hotkeyBinding.Where(hk => hk.DevId == deviceListView.SelectedItems[0].SubItems[1].Text).FirstOrDefault(); + btnUnbindHotkey.Enabled = selectDeviceHK != null; } else { btnOnOff.Text = "ON/OFF"; btnOnOff.Enabled = false; btnBindHotKey.Enabled = false; + btnUnbindHotkey.Enabled = false; } if (listening) DoneBinding(); @@ -298,6 +304,25 @@ private void UnregisterAllHotkeys() btnUnbindKeys.Enabled = false; } + private void BtnUnbindHotkey_Click(object sender, EventArgs e) + { + var selectDeviceHK = hotkeyBinding.Where(hk => hk.DevId == deviceListView.SelectedItems[0].SubItems[1].Text).FirstOrDefault(); + if(selectDeviceHK != null) + { + int index = hotkeyBinding.IndexOf(selectDeviceHK); + + Debug.WriteLine("index: " + index); + + for (int i = 0; i < hotkeyBinding.Count; i++) + { + UnregisterHotKey(this.Handle, i); + } + hotkeyBinding.Remove(selectDeviceHK); + RegisterHotKey(); + RefreshDevicesList(); + } + } + private void BtnUnbindKeys_Click(object sender, EventArgs e) { var result = MessageBox.Show("Are you sure you want to remove all hotkeys?", "Unbinding Hotkeys", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); @@ -348,7 +373,6 @@ private void MenuAutoStart_Click(object sender, EventArgs e) menuAutoStart.Checked = false; } } - } public class HotKeyBinding