[WIP] Testing passing hotkeys

Testing the best way to pass hotkeys through
to the Hotkey form in a way that I can reuse it
across the whole application.
This commit is contained in:
Terry MacDonald 2021-04-24 16:06:00 +12:00
parent b360fc9f37
commit 0e7ee3acef
5 changed files with 174 additions and 67 deletions

View File

@ -343,6 +343,7 @@ namespace DisplayMagician.UIForms
this.btn_hotkey.TabIndex = 35; this.btn_hotkey.TabIndex = 35;
this.btn_hotkey.Text = "&Hotkey"; this.btn_hotkey.Text = "&Hotkey";
this.btn_hotkey.UseVisualStyleBackColor = true; this.btn_hotkey.UseVisualStyleBackColor = true;
this.btn_hotkey.Click += new System.EventHandler(this.btn_hotkey_Click);
// //
// DisplayProfileForm // DisplayProfileForm
// //

View File

@ -7,6 +7,7 @@ using DisplayMagicianShared;
using Manina.Windows.Forms; using Manina.Windows.Forms;
using System.Drawing; using System.Drawing;
using System.Collections.Generic; using System.Collections.Generic;
using WK.Libraries.HotkeyListenerNS;
namespace DisplayMagician.UIForms namespace DisplayMagician.UIForms
{ {
@ -459,60 +460,19 @@ namespace DisplayMagician.UIForms
} }
} }
/*private static void MainWindow_Closed(object sender, EventArgs e) private void btn_hotkey_Click(object sender, EventArgs e)
{ {
DeviceNotification.UnRegisterUsbDeviceNotification(); Hotkey testHotkey = new Hotkey();
DeviceNotification.UnRegisterMonitorDeviceNotification(); var displayHotkeyForm = new HotkeyForm(testHotkey);
//ilv_saved_shortcuts.SuspendLayout();
displayHotkeyForm.ShowDialog(this);
if (displayHotkeyForm.DialogResult == DialogResult.OK)
{
MessageBox.Show($"We got the hotkey {displayHotkeyForm.Hotkey.ToString()}", "results", MessageBoxButtons.OK, MessageBoxIcon.Error);
// As this is an edit, we need to manually force saving the shortcut library
//ShortcutRepository.SaveShortcuts();
} }
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
if (!(PresentationSource.FromVisual(this) is HwndSource source))
return;
source.AddHook(this.WndProc);
DeviceNotification.RegisterUsbDeviceNotification(source.Handle);
DeviceNotification.RegisterMonitorDeviceNotification(source.Handle);
}*/
// Notification registeration for display detection from here http://codetips.nl/detectmonitor.html
/*protected override void WndProc(ref Message m)
{
const int WM_SETTINGCHANGE = 0x001A;
const int SPI_SETWORKAREA = 0x02F;
const int WM_DISPLAYCHANGE = 0x007E;
const int WM_DEVICECHANGE = 0x0219; // WM Device change message ID
const int DBT_DEVICEARRIVAL = 0x8000; // WM Device Change Event: System detected a new device
const int DBT_DEVICEREMOVECOMPLETE = 0x8004; // WM Device Change Event: Device is gone
const int x_bitshift = 0;
const int y_bitshift = 16;
const int xy_mask = 0xFFFF;
bool displayChange = false;
switch (m.Msg)
{
case WM_DISPLAYCHANGE:
case DeviceNotification.DbtDeviceRemoveComplete:
case DeviceNotification.DbtDeviceArrival:
{
if (DeviceNotification.IsMonitor(lParam))
Debug.WriteLine($"Monitor {((int)wParam == DeviceNotification.DbtDeviceArrival ? "arrived" : "removed")}");
if (DeviceNotification.IsUsbDevice(lParam))
Debug.WriteLine($"Usb device {((int)wParam == DeviceNotification.DbtDeviceArrival ? "arrived" : "removed")}");
} }
break;
}
base.WndProc(ref m);
}*/
} }
} }

View File

@ -29,39 +29,118 @@ namespace DisplayMagician.UIForms
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.lbl_hotkey = new System.Windows.Forms.Label(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HotkeyForm));
this.txt_hotkey = new System.Windows.Forms.TextBox();
this.lbl_hotkey_selector = new System.Windows.Forms.Label();
this.btn_save = new System.Windows.Forms.Button();
this.btn_clear = new System.Windows.Forms.Button();
this.lbl_hotkey_heading = new System.Windows.Forms.Label();
this.lbl_hotkey_description = new System.Windows.Forms.Label();
this.SuspendLayout(); this.SuspendLayout();
// //
// lbl_hotkey // txt_hotkey
// //
this.lbl_hotkey.AutoSize = true; this.txt_hotkey.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lbl_hotkey.BackColor = System.Drawing.Color.Black; this.txt_hotkey.Location = new System.Drawing.Point(61, 152);
this.lbl_hotkey.ForeColor = System.Drawing.Color.White; this.txt_hotkey.Name = "txt_hotkey";
this.lbl_hotkey.Location = new System.Drawing.Point(42, 32); this.txt_hotkey.Size = new System.Drawing.Size(270, 26);
this.lbl_hotkey.Name = "lbl_hotkey"; this.txt_hotkey.TabIndex = 1;
this.lbl_hotkey.Size = new System.Drawing.Size(525, 13); //
this.lbl_hotkey.TabIndex = 0; // lbl_hotkey_selector
this.lbl_hotkey.Text = "You can set a Hotkey (a keyboard shortcut) so that you can apply this Display Pro" + //
"file just using your keyboard."; this.lbl_hotkey_selector.AutoSize = true;
this.lbl_hotkey_selector.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
this.lbl_hotkey_selector.ForeColor = System.Drawing.Color.White;
this.lbl_hotkey_selector.Location = new System.Drawing.Point(63, 182);
this.lbl_hotkey_selector.Name = "lbl_hotkey_selector";
this.lbl_hotkey_selector.Size = new System.Drawing.Size(270, 13);
this.lbl_hotkey_selector.TabIndex = 2;
this.lbl_hotkey_selector.Text = "Hold down the keys you\'d like to use, then release them";
//
// btn_save
//
this.btn_save.Anchor = System.Windows.Forms.AnchorStyles.None;
this.btn_save.BackColor = System.Drawing.Color.Black;
this.btn_save.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed;
this.btn_save.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Brown;
this.btn_save.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_save.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btn_save.ForeColor = System.Drawing.Color.White;
this.btn_save.Location = new System.Drawing.Point(162, 222);
this.btn_save.Name = "btn_save";
this.btn_save.Size = new System.Drawing.Size(118, 40);
this.btn_save.TabIndex = 3;
this.btn_save.Text = "&Save";
this.btn_save.UseVisualStyleBackColor = false;
this.btn_save.Click += new System.EventHandler(this.btn_save_Click);
//
// btn_clear
//
this.btn_clear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_clear.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btn_clear.FlatAppearance.MouseDownBackColor = System.Drawing.Color.IndianRed;
this.btn_clear.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Brown;
this.btn_clear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_clear.ForeColor = System.Drawing.Color.White;
this.btn_clear.Location = new System.Drawing.Point(337, 152);
this.btn_clear.Name = "btn_clear";
this.btn_clear.Size = new System.Drawing.Size(75, 26);
this.btn_clear.TabIndex = 6;
this.btn_clear.Text = "&Clear";
this.btn_clear.UseVisualStyleBackColor = true;
this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click);
//
// lbl_hotkey_heading
//
this.lbl_hotkey_heading.AutoSize = true;
this.lbl_hotkey_heading.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lbl_hotkey_heading.ForeColor = System.Drawing.Color.White;
this.lbl_hotkey_heading.Location = new System.Drawing.Point(58, 23);
this.lbl_hotkey_heading.Name = "lbl_hotkey_heading";
this.lbl_hotkey_heading.Size = new System.Drawing.Size(326, 20);
this.lbl_hotkey_heading.TabIndex = 7;
this.lbl_hotkey_heading.Text = "Choose a Hotkey for this Display Profile";
//
// lbl_hotkey_description
//
this.lbl_hotkey_description.AutoSize = true;
this.lbl_hotkey_description.BackColor = System.Drawing.Color.Black;
this.lbl_hotkey_description.ForeColor = System.Drawing.Color.White;
this.lbl_hotkey_description.Location = new System.Drawing.Point(66, 67);
this.lbl_hotkey_description.Name = "lbl_hotkey_description";
this.lbl_hotkey_description.Size = new System.Drawing.Size(318, 52);
this.lbl_hotkey_description.TabIndex = 0;
this.lbl_hotkey_description.Text = resources.GetString("lbl_hotkey_description.Text");
this.lbl_hotkey_description.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// //
// HotkeyForm // HotkeyForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black; this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(442, 221); this.ClientSize = new System.Drawing.Size(442, 274);
this.Controls.Add(this.lbl_hotkey); this.Controls.Add(this.lbl_hotkey_heading);
this.Controls.Add(this.btn_clear);
this.Controls.Add(this.btn_save);
this.Controls.Add(this.lbl_hotkey_selector);
this.Controls.Add(this.txt_hotkey);
this.Controls.Add(this.lbl_hotkey_description);
this.Name = "HotkeyForm"; this.Name = "HotkeyForm";
this.ShowIcon = false; this.ShowIcon = false;
this.ShowInTaskbar = false; this.ShowInTaskbar = false;
this.Text = "Select a Hotkey"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Choose a Hotkey";
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
} }
#endregion #endregion
private System.Windows.Forms.TextBox txt_hotkey;
private System.Windows.Forms.Label lbl_hotkey; private System.Windows.Forms.Label lbl_hotkey_selector;
private System.Windows.Forms.Button btn_save;
private System.Windows.Forms.Button btn_clear;
private System.Windows.Forms.Label lbl_hotkey_heading;
private System.Windows.Forms.Label lbl_hotkey_description;
} }
} }

View File

@ -7,14 +7,75 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using WK.Libraries.HotkeyListenerNS;
namespace DisplayMagician.UIForms namespace DisplayMagician.UIForms
{ {
public partial class HotkeyForm : Form public partial class HotkeyForm : Form
{ {
HotkeySelector hks;
Hotkey myHotkey = null;
public Hotkey Hotkey {
get
{
return myHotkey;
}
set
{
if (value is Hotkey)
myHotkey = value;
}
}
public HotkeyForm() public HotkeyForm()
{ {
InitializeComponent(); InitializeComponent();
myHotkey = new Hotkey();
hks = new HotkeySelector();
hks.EmptyHotkeyText = "";
hks.Enable(txt_hotkey);
this.ActiveControl = txt_hotkey;
}
public HotkeyForm(Hotkey hotkeyToEdit = null, string hotkeyName = "")
{
InitializeComponent();
if (hotkeyToEdit == null)
myHotkey = new Hotkey();
else
myHotkey = hotkeyToEdit;
hks = new HotkeySelector();
hks.EmptyHotkeyText = "";
hks.Enable(txt_hotkey, hotkeyToEdit);
this.ActiveControl = txt_hotkey;
}
private void btn_clear_Click(object sender, EventArgs e)
{
hks.Reset(txt_hotkey);
}
private void btn_save_Click(object sender, EventArgs e)
{
Program.HotkeyListener.Update
(
// Reference the current clipping hotkey for directly updating
// the hotkey without a need for restarting your application.
ref myHotkey,
// Convert the selected hotkey's text representation
// to a Hotkey object and update it.
HotkeyListener.Convert(txt_hotkey.Text)
);
this.Hotkey = myHotkey;
this.DialogResult = DialogResult.OK;
this.Close();
} }
} }
} }

View File

@ -117,4 +117,10 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<data name="lbl_hotkey_description.Text" xml:space="preserve">
<value>Choose a Hotkey (a keyboard shortcut) so that you can apply this
Display Profile using your keyboard. This must be a Hotkey that
is unique across all your applications otherwise DisplayMagician
might not see it.</value>
</data>
</root> </root>