diff SetHotkey.cs @ 0:aca8706f4eec default tip

Initial commit
author Brad Greco <brad@bgreco.net>
date Mon, 13 Oct 2014 21:28:19 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SetHotkey.cs	Mon Oct 13 21:28:19 2014 -0500
@@ -0,0 +1,83 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Text;
+using System.Windows.Forms;
+
+/*
+ * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
+ */
+namespace QuickQR
+{
+    public partial class SetHotkey : Form
+    {
+        private bool capturing = false;
+        public KeyBinding keyBinding;
+
+        public SetHotkey(KeyBinding keyBinding)
+        {
+            InitializeComponent();
+            this.keyBinding = new KeyBinding();
+            hotkeyLabel.Text = keyBinding.toString();
+        }
+
+        private void captureButton_Click(object sender, EventArgs e)
+        {
+            capturing = true;
+            hotkeyLabel.Text = "Waiting... (ESC to clear)";
+            keyBinding.Win = false;
+        }
+
+        private void SetHotkey_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (capturing && e.KeyCode == Keys.Escape)
+            {
+                capturing = false;
+                keyBinding.key = 0;
+                keyBinding.keyChar = "";
+                hotkeyLabel.Text = keyBinding.comboString();
+            }
+            else if (capturing && (e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin))
+            {
+                keyBinding.Win = true;
+            }
+            else if (capturing && e.KeyCode != Keys.ShiftKey && e.KeyCode != Keys.Menu && e.KeyCode != Keys.Alt && e.KeyCode != Keys.ControlKey)
+            {
+                keyBinding.key = (int)e.KeyCode;
+                keyBinding.keyChar = e.KeyCode.ToString();
+                keyBinding.Shift = e.Shift;
+                keyBinding.Alt = e.Alt;
+                keyBinding.Ctrl = e.Control;
+                hotkeyLabel.Text = keyBinding.toString();
+            }
+        }
+
+        private void SetHotkey_KeyUp(object sender, KeyEventArgs e)
+        {
+            if (capturing && (e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin))
+            {
+                keyBinding.Win = false;
+            }
+            else if (capturing && e.KeyCode != Keys.ShiftKey && e.KeyCode != Keys.Menu && e.KeyCode != Keys.Alt && e.KeyCode != Keys.ControlKey)
+            {
+                capturing = false;
+            }
+            okButton.Enabled = !capturing;
+        }
+
+        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
+        {
+            if (keyData == Keys.Escape && capturing)
+            {
+                keyBinding.key = 0;
+                hotkeyLabel.Text = keyBinding.toString();
+                capturing = false;
+                okButton.Enabled = true;
+                return true;
+            }
+            return base.ProcessCmdKey(ref msg, keyData);
+        }
+    }
+}