Skip to content

Commit

Permalink
Merge pull request #5 from mgkha/hotkey-unbinding
Browse files Browse the repository at this point in the history
unbind individual hotkey
  • Loading branch information
mgkha authored Jan 10, 2021
2 parents ebfcb53 + a0798f2 commit aa493f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
16 changes: 15 additions & 1 deletion SmartHomeWin/Home.Designer.cs

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

38 changes: 31 additions & 7 deletions SmartHomeWin/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -348,7 +373,6 @@ private void MenuAutoStart_Click(object sender, EventArgs e)
menuAutoStart.Checked = false;
}
}

}

public class HotKeyBinding
Expand Down

0 comments on commit aa493f1

Please sign in to comment.