From a8e9615e379351f5d218f2ecd01e87920ae95b6d Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Fri, 20 Nov 2020 07:25:41 -0800 Subject: [PATCH] Added unicode key support. --- KVMControl.cs | 48 +++++++++++++++++++++++++++++++--- KVMSettingsForm.Designer.cs | 19 ++++++++++---- KVMSettingsForm.cs | 6 +++++ KVMSettingsForm.resx | 52 +++++++++++++++++++++++++++++-------- KVMViewer.cs | 2 ++ 5 files changed, 107 insertions(+), 20 deletions(-) diff --git a/KVMControl.cs b/KVMControl.cs index 370cdf4..833a660 100644 --- a/KVMControl.cs +++ b/KVMControl.cs @@ -38,6 +38,7 @@ namespace MeshCentralRouter private int scalinglevel = 1024; // 100% scale private int frameRate = 100; // Medium frame rate private bool swamMouseButtons = false; + private bool remoteKeybaordMap = false; private double scalefactor = 1; public List displays = new List(); public ushort currentDisp = 0; @@ -49,6 +50,7 @@ namespace MeshCentralRouter public KVMViewer parent = null; private readonly KVMControlHook.KVMCallback KeyboardCallback; private bool keyboardIsAttached; + private long killNextKeyPress = 0; private enum KvmCommands @@ -73,6 +75,7 @@ namespace MeshCentralRouter Jumbo = 27, Disconnect = 59, Alert = 65, + KeyUnicode = 85, MouseCursor = 88 } @@ -105,6 +108,7 @@ namespace MeshCentralRouter public int ScalingLevel { get { return scalinglevel; } set { scalinglevel = value; SendCompressionLevel(); } } public int FrameRate { get { return frameRate; } set { frameRate = value; SendCompressionLevel(); } } public bool SwamMouseButtons { get { return swamMouseButtons; } set { swamMouseButtons = value; } } + public bool RemoteKeybaordMap { get { return remoteKeybaordMap; } set { remoteKeybaordMap = value; } } public double ScaleFactor { get { return scalefactor; } set { scalefactor = value; } } @@ -374,18 +378,53 @@ namespace MeshCentralRouter Send(bw); } - private void SendKey(KeyEventArgs e, byte action) + public void SendUnicodeKey(ushort key, byte action) { //if (state != ConnectState.Connected) return; BinaryWriter bw = GetBinaryWriter(); - bw.Write(IPAddress.HostToNetworkOrder((short)KvmCommands.Key)); - bw.Write(IPAddress.HostToNetworkOrder((short)6)); + bw.Write(IPAddress.HostToNetworkOrder((short)KvmCommands.KeyUnicode)); + bw.Write(IPAddress.HostToNetworkOrder((short)7)); bw.Write((byte)action); - bw.Write((byte)e.KeyCode); + bw.Write((byte)(key >> 8)); + bw.Write((byte)(key & 0xFF)); Send(bw); } + private void SendPress(KeyPressEventArgs e, byte action) + { + //if (state != ConnectState.Connected) return; + + if (remoteKeybaordMap == true) return; + + if (killNextKeyPress > 0) { + long t = DateTime.Now.Ticks; + if ((t - killNextKeyPress) < 10) { killNextKeyPress = 0; return; } + } + + ushort c = (ushort)e.KeyChar; + if (c < 32) + { + SendKey((byte)c, 0); + } + else + { + SendUnicodeKey(c, 0); + } + } + + private void SendKey(KeyEventArgs e, byte action) + { + //if (state != ConnectState.Connected) return; + + if (remoteKeybaordMap == true) { SendKey((byte)e.KeyCode, action); return; } // Use old key system that uses the remote keyboard mapping. + string keycode = e.KeyCode.ToString(); + if ((action == 0) && (e.Control == false) && (e.Alt == false) && (((e.KeyValue >= 48) && (e.KeyValue <= 57)) || (keycode.Length == 1) || (keycode.StartsWith("Oem") == true))) return; + if ((e.Control == true) || (e.Alt == true)) { killNextKeyPress = DateTime.Now.Ticks; } + SendKey((byte)e.KeyCode, action); + e.Handled = true; + } + private short LastX = 0; private short LastY = 0; private void SendMouse(MouseEventArgs e, byte action) @@ -655,6 +694,7 @@ namespace MeshCentralRouter private void KVMControl_KeyPress(object sender, KeyPressEventArgs e) { + SendPress(e, 0); e.Handled = true; } diff --git a/KVMSettingsForm.Designer.cs b/KVMSettingsForm.Designer.cs index ff14b04..2674f17 100644 --- a/KVMSettingsForm.Designer.cs +++ b/KVMSettingsForm.Designer.cs @@ -32,6 +32,7 @@ this.cancelButton = new System.Windows.Forms.Button(); this.okButton = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.swapMouseButtonsCheckBox = new System.Windows.Forms.CheckBox(); this.frameRateComboBox = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.scalingComboBox = new System.Windows.Forms.ComboBox(); @@ -40,7 +41,7 @@ this.label3 = new System.Windows.Forms.Label(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.label4 = new System.Windows.Forms.Label(); - this.swapMouseButtonsCheckBox = new System.Windows.Forms.CheckBox(); + this.remoteKeyboardMapCheckBox = new System.Windows.Forms.CheckBox(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); @@ -63,6 +64,7 @@ // groupBox1 // resources.ApplyResources(this.groupBox1, "groupBox1"); + this.groupBox1.Controls.Add(this.remoteKeyboardMapCheckBox); this.groupBox1.Controls.Add(this.swapMouseButtonsCheckBox); this.groupBox1.Controls.Add(this.frameRateComboBox); this.groupBox1.Controls.Add(this.label2); @@ -73,6 +75,12 @@ this.groupBox1.Name = "groupBox1"; this.groupBox1.TabStop = false; // + // swapMouseButtonsCheckBox + // + resources.ApplyResources(this.swapMouseButtonsCheckBox, "swapMouseButtonsCheckBox"); + this.swapMouseButtonsCheckBox.Name = "swapMouseButtonsCheckBox"; + this.swapMouseButtonsCheckBox.UseVisualStyleBackColor = true; + // // frameRateComboBox // resources.ApplyResources(this.frameRateComboBox, "frameRateComboBox"); @@ -121,11 +129,11 @@ resources.ApplyResources(this.label4, "label4"); this.label4.Name = "label4"; // - // swapMouseButtonsCheckBox + // remoteKeyboardMapCheckBox // - resources.ApplyResources(this.swapMouseButtonsCheckBox, "swapMouseButtonsCheckBox"); - this.swapMouseButtonsCheckBox.Name = "swapMouseButtonsCheckBox"; - this.swapMouseButtonsCheckBox.UseVisualStyleBackColor = true; + resources.ApplyResources(this.remoteKeyboardMapCheckBox, "remoteKeyboardMapCheckBox"); + this.remoteKeyboardMapCheckBox.Name = "remoteKeyboardMapCheckBox"; + this.remoteKeyboardMapCheckBox.UseVisualStyleBackColor = true; // // KVMSettingsForm // @@ -164,5 +172,6 @@ private System.Windows.Forms.ComboBox qualityComboBox; private System.Windows.Forms.Label label3; private System.Windows.Forms.CheckBox swapMouseButtonsCheckBox; + private System.Windows.Forms.CheckBox remoteKeyboardMapCheckBox; } } \ No newline at end of file diff --git a/KVMSettingsForm.cs b/KVMSettingsForm.cs index ae432a6..3f183af 100644 --- a/KVMSettingsForm.cs +++ b/KVMSettingsForm.cs @@ -124,6 +124,12 @@ namespace MeshCentralRouter set { swapMouseButtonsCheckBox.Checked = value; } } + public bool RemoteKeybaordMap + { + get { return remoteKeyboardMapCheckBox.Checked; } + set { remoteKeyboardMapCheckBox.Checked = value; } + } + private void okButton_Click(object sender, EventArgs e) { DialogResult = System.Windows.Forms.DialogResult.OK; diff --git a/KVMSettingsForm.resx b/KVMSettingsForm.resx index cc49c12..5b2f1dd 100644 --- a/KVMSettingsForm.resx +++ b/KVMSettingsForm.resx @@ -123,7 +123,7 @@ - 312, 207 + 312, 236 75, 23 @@ -151,7 +151,7 @@ Bottom, Right - 231, 207 + 231, 236 75, 23 @@ -177,6 +177,36 @@ Top, Bottom, Left, Right + + True + + + NoControl + + + 128, 128 + + + 157, 17 + + + 11 + + + Use Remote Keyboard Map + + + remoteKeyboardMapCheckBox + + + System.Windows.Forms.CheckBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + groupBox1 + + + 0 + True @@ -202,7 +232,7 @@ groupBox1 - 0 + 1 Top, Left, Right @@ -226,7 +256,7 @@ groupBox1 - 1 + 2 True @@ -253,7 +283,7 @@ groupBox1 - 2 + 3 Top, Left, Right @@ -277,7 +307,7 @@ groupBox1 - 3 + 4 True @@ -304,7 +334,7 @@ groupBox1 - 4 + 5 Top, Left, Right @@ -328,7 +358,7 @@ groupBox1 - 5 + 6 True @@ -355,13 +385,13 @@ groupBox1 - 6 + 7 12, 68 - 375, 133 + 375, 162 2 @@ -439,7 +469,7 @@ 6, 13 - 399, 242 + 399, 271 diff --git a/KVMViewer.cs b/KVMViewer.cs index d82c3ce..658a750 100644 --- a/KVMViewer.cs +++ b/KVMViewer.cs @@ -344,10 +344,12 @@ namespace MeshCentralRouter form.Scaling = kvmControl.ScalingLevel; form.FrameRate = kvmControl.FrameRate; form.SwamMouseButtons = kvmControl.SwamMouseButtons; + form.RemoteKeybaordMap = kvmControl.RemoteKeybaordMap; if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { kvmControl.SetCompressionParams(form.Compression, form.Scaling, form.FrameRate); kvmControl.SwamMouseButtons = form.SwamMouseButtons; + kvmControl.RemoteKeybaordMap = form.RemoteKeybaordMap; } } }