Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to show keyboard keys based on locales #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/Carnac.Logic/KeyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,11 @@ static IEnumerable<string> ToInputs(bool isLetter, bool isWinKeyPressed, Interce
if (shiftPressed)
yield return "Shift";

yield return interceptKeyEventArgs.Key.Sanitise();
yield return interceptKeyEventArgs.Key.SanitiseLower();
}
else
{
string input;
var shiftModifiesInput = interceptKeyEventArgs.Key.SanitiseShift(out input);

if (!isLetter && !shiftModifiesInput && shiftPressed)
yield return "Shift";

if (interceptKeyEventArgs.ShiftPressed && shiftModifiesInput)
yield return input;
else if (isLetter && !interceptKeyEventArgs.ShiftPressed)
yield return interceptKeyEventArgs.Key.ToString().ToLower();
else
yield return interceptKeyEventArgs.Key.Sanitise();
yield return interceptKeyEventArgs.Key.Sanitise();
}
}

Expand Down
88 changes: 80 additions & 8 deletions src/Carnac.Logic/ReplaceKey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Windows.Input;

namespace Carnac.Logic
{
Expand Down Expand Up @@ -80,6 +83,27 @@ public static class ReplaceKey
{Keys.RWin, "Win"},
};

static readonly Dictionary<Keys, string> SpecialCases = new Dictionary<Keys, string>
{
{Keys.Divide, " / "},
{Keys.Multiply, " * "},
{Keys.Subtract, " - "},
{Keys.Add, " + "},
{Keys.LShiftKey, "Shift"},
{Keys.RShiftKey, "Shift"},
{Keys.LWin, "Win"},
{Keys.RWin, "Win"},
{Keys.LControlKey, "Ctrl"},
{Keys.RControlKey, "Ctrl"},
{Keys.Alt, "Alt"},
{Keys.LMenu, "Alt"},
{Keys.Tab, "Tab"},
{Keys.Back, "Back"},
{Keys.Return, "Return"},
{Keys.Escape, "Escape"},
};

// kept to continue to support keymaps parsing
public static Keys? ToKey(string keyText)
{
foreach (var shiftReplacement in ShiftReplacements)
Expand All @@ -99,21 +123,69 @@ public static class ReplaceKey
return null;
}

public static string Sanitise(this Keys key)
// new implementation of sanitize to support locals
// https://stackoverflow.com/questions/318777/c-sharp-how-to-translate-virtual-keycode-to-char
static public string KeyCodeToUnicode(Keys key, bool lowerOnly = false)
{
return Replacements.ContainsKey(key) ? Replacements[key] : string.Format(key.ToString());
byte[] keyboardState = new byte[255];
if (!lowerOnly)
{
bool keyboardStateStatus = GetKeyboardState(keyboardState);
if (!keyboardStateStatus)
{
return "";
}
}

uint virtualKeyCode = (uint)key;
uint scanCode = MapVirtualKey(virtualKeyCode, 0);
IntPtr inputLocaleIdentifier = GetKeyboardLayout(0);

StringBuilder result = new StringBuilder();
ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier);

return result.ToString();
}

public static bool SanitiseShift(this Keys key, out string sanitisedKeyInput)
[DllImport("user32.dll")]
static extern bool GetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, uint uMapType);

[DllImport("user32.dll")]
static extern IntPtr GetKeyboardLayout(uint idThread);

[DllImport("user32.dll")]
static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);

public static string Sanitise(this Keys key)
{
if (ShiftReplacements.ContainsKey(key))
if (SpecialCases.ContainsKey(key))
{
return SpecialCases[key];
}
string result = KeyCodeToUnicode(key);
if (result.Length > 0)
{
sanitisedKeyInput = ShiftReplacements[key];
return true;
return result;
}
return key.ToString();
}

sanitisedKeyInput = key.Sanitise();
return false;
public static string SanitiseLower(this Keys key)
{
if (SpecialCases.ContainsKey(key))
{
return SpecialCases[key];
}
string result = KeyCodeToUnicode(key, true);
if (result.Length > 0)
{
return result;
}
return key.ToString();
}

}
}