From dc03e06324429a425a151f2a3d73539d58a23dd5 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Tue, 22 Sep 2020 11:39:34 -0700 Subject: [PATCH] Changes to desktop keyboard input --- KVMControl.Designer.cs | 3 ++ KVMControl.cs | 39 ++++++++++++++++++++++++ KVMControlHook.cs | 67 +++++++++++++++++++++--------------------- MeshCentralServer.cs | 2 +- 4 files changed, 77 insertions(+), 34 deletions(-) diff --git a/KVMControl.Designer.cs b/KVMControl.Designer.cs index 6473a54..07e2537 100644 --- a/KVMControl.Designer.cs +++ b/KVMControl.Designer.cs @@ -38,6 +38,9 @@ this.Name = "KVMControl"; this.Size = new System.Drawing.Size(585, 364); this.Paint += new System.Windows.Forms.PaintEventHandler(this.KVMControl_Paint); + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KVMControl_KeyDown); + this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KVMControl_KeyPress); + this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.KVMControl_KeyUp); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.KVMControl_MouseDown); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.KVMControl_MouseMove); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.KVMControl_MouseUp); diff --git a/KVMControl.cs b/KVMControl.cs index c42d0fc..e4868bf 100644 --- a/KVMControl.cs +++ b/KVMControl.cs @@ -629,5 +629,44 @@ namespace MeshCentralRouter public static byte[] GetByteArray() { lock (ByteArrayRecycleList) { if (ByteArrayRecycleList.Count == 0) ByteArrayRecycleListCount++; return (ByteArrayRecycleList.Count == 0) ? new byte[65535] : ByteArrayRecycleList.Pop(); } } public static void RecycleByteArray(byte[] obj) { lock (ByteArrayRecycleList) { if (obj != null) ByteArrayRecycleList.Push(obj); } } + private void KVMControl_KeyDown(object sender, KeyEventArgs e) + { + if ((e.KeyCode == Keys.LWin) || (e.KeyCode == Keys.RWin)) return; // Don't process the Windows key + SendKey(e, 0); + e.Handled = true; + } + + private void KVMControl_KeyPress(object sender, KeyPressEventArgs e) + { + e.Handled = true; + } + + private void KVMControl_KeyUp(object sender, KeyEventArgs e) + { + SendKey(e, 1); + e.Handled = true; + } + + protected override bool ProcessCmdKey(ref Message msg, Keys keyData) + { + const int WM_KEYDOWN = 0x100; + const int WM_SYSKEYDOWN = 0x104; + + // Tab keys + if (msg.Msg == WM_KEYDOWN && msg.WParam.ToInt32() == 9) + { + SendKey((byte)msg.WParam.ToInt32(), 0); + return true; + } + + // Handle arrow keys + if (((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN)) && msg.WParam.ToInt32() >= 37 && msg.WParam.ToInt32() <= 40) + { + SendKey((byte)msg.WParam.ToInt32(), 0); + return true; + } + + return false; + } } } diff --git a/KVMControlHook.cs b/KVMControlHook.cs index 809fa0b..1565239 100644 --- a/KVMControlHook.cs +++ b/KVMControlHook.cs @@ -51,45 +51,46 @@ namespace MeshCentralRouter { if (nCode >= 0) { - bool alt = (Control.ModifierKeys & Keys.Alt) != 0; - bool control = (Control.ModifierKeys & Keys.Control) != 0; - Keys key = (Keys)Marshal.ReadInt32(lParam); + if ((key == Keys.LWin) || (key == Keys.RWin)) { + bool alt = (Control.ModifierKeys & Keys.Alt) != 0; + bool control = (Control.ModifierKeys & Keys.Control) != 0; - const int WM_KEYDOWN = 0x100; - const int WM_KEYUP = 0x101; - const int WM_SYSKEYDOWN = 0x104; - const int WM_SYSKEYUP = 0x105; + const int WM_KEYDOWN = 0x100; + const int WM_KEYUP = 0x101; + const int WM_SYSKEYDOWN = 0x104; + const int WM_SYSKEYUP = 0x105; - byte bkey = (byte)key; - byte keyStatus; - switch ((int)wParam) - { - case WM_KEYDOWN: - keyStatus = 0; - break; - case WM_KEYUP: - keyStatus = 1; - break; - case WM_SYSKEYDOWN: - keyStatus = 4; - break; - case WM_SYSKEYUP: - keyStatus = 5; - break; - default: - return KVMKeyboardHook.CallNextHookEx(_hook, nCode, wParam, lParam); - } - - try - { - if (_callback != null) + byte bkey = (byte)key; + byte keyStatus = 255; + switch ((int)wParam) { - _callback(bkey,keyStatus); - return (IntPtr)1; + case WM_KEYDOWN: + keyStatus = 0; + break; + case WM_KEYUP: + keyStatus = 1; + break; + case WM_SYSKEYDOWN: + //keyStatus = 0; // 4 + break; + case WM_SYSKEYUP: + //keyStatus = 1; // 5 + break; + default: + return KVMKeyboardHook.CallNextHookEx(_hook, nCode, wParam, lParam); } + + try + { + if ((_callback != null) && (keyStatus != 255)) + { + _callback(bkey, keyStatus); + return (IntPtr)1; + } + } + catch { } } - catch { } } return KVMKeyboardHook.CallNextHookEx(_hook, nCode, wParam, lParam); } diff --git a/MeshCentralServer.cs b/MeshCentralServer.cs index 52a6e4e..271aa83 100644 --- a/MeshCentralServer.cs +++ b/MeshCentralServer.cs @@ -743,7 +743,7 @@ namespace MeshCentralRouter readBufferLen = 0; Debug("Websocket TCP connected, doing TLS..."); wsstream = new SslStream(wsrawstream, false, VerifyServerCertificate, null); - wsstream.BeginAuthenticateAsClient(url.Host, null, System.Security.Authentication.SslProtocols.Tls12, false, new AsyncCallback(OnTlsSetupSink), this); + wsstream.BeginAuthenticateAsClient(url.Host, null, System.Security.Authentication.SslProtocols.Default, false, new AsyncCallback(OnTlsSetupSink), this); } else {