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

Fix keyboards detection and do full swicth on input method #1

Open
wants to merge 4 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.vs/
RightKeyboard/bin/
RightKeyboard/obj/
bin/
obj/
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ This code is based on the work published here: http://www.codeproject.com/Articl
# Changes
- Removed "current layout cache", and used system call "Get Current Layout" as method prevent syscall overload, as it failed after sleep.
- Added "Clear" context menu, to clear current configuration.
- Make correct distinction between mulitple keyboards layout in a same language.

# Before use
Make sure all your keyboard layouts are set on different languages/locales on windows, as this app can only set layout by setting the system locale. I have here, for example:
- English (United States) locale set to have only US International layout.
- Portuguese (Brazil) set to have only Portuguese (Brazil ABNT) layout.

If you have multiple layouts on same locale it won't work.
You must setup your keyboards layout in windows first.
This app only help you to automate the switch, it doesn't help you to declare your keyboards.

# How to use
1. Press WindowsKey + R
Expand Down
15 changes: 15 additions & 0 deletions RightKeyboard.NUnit/ConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using NUnit.Framework;

namespace RightKeyboard
{
[TestFixture]
class ConfigurationTests
{
[Test]
public void LoadConfiguration_Works()
{
var configuration = Configuration.LoadConfiguration(new KeyboardDevicesCollection());
Assert.That(configuration, Is.Not.Null, "Depends on what is installed on the machine, so manual human check with debugger is required.");
}
}
}
21 changes: 21 additions & 0 deletions RightKeyboard.NUnit/RightKeyboard.NUnit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net4</TargetFramework>

<IsPackable>false</IsPackable>

<RootNamespace>RightKeyboard</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RightKeyboard\RightKeyboard.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions RightKeyboard.NUnit/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using RightKeyboard.Win32;

namespace RightKeyboard.NUnit
{
[TestFixture]
public class Tests
{
[Test]
public void GetKeyboardLayoutName_MustReturnLanguageNameAndKeyboardName()
{
var list = API.GetKeyboardLayoutList();
foreach (var layout in list)
{
var name = API.GetKeyboardLayoutName(layout);
Assert.That(name, Is.Not.Empty, "Depends on what is installed on the machine, so manual human check with debugger is required.");
}
}
}
}
6 changes: 6 additions & 0 deletions RightKeyboard.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RightKeyboard", "RightKeyboard\RightKeyboard.csproj", "{D9A15FB8-E81F-4472-8DF4-948E083AE6BC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RightKeyboard.NUnit", "RightKeyboard.NUnit\RightKeyboard.NUnit.csproj", "{79526D79-46A9-4EEB-8AC3-711344DC4A39}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{D9A15FB8-E81F-4472-8DF4-948E083AE6BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9A15FB8-E81F-4472-8DF4-948E083AE6BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9A15FB8-E81F-4472-8DF4-948E083AE6BC}.Release|Any CPU.Build.0 = Release|Any CPU
{79526D79-46A9-4EEB-8AC3-711344DC4A39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79526D79-46A9-4EEB-8AC3-711344DC4A39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79526D79-46A9-4EEB-8AC3-711344DC4A39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79526D79-46A9-4EEB-8AC3-711344DC4A39}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
74 changes: 74 additions & 0 deletions RightKeyboard/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;

namespace RightKeyboard
{
public class Configuration
{
public Dictionary<IntPtr, Layout> LanguageMappings { get; } = new Dictionary<IntPtr, Layout>();

public static Configuration LoadConfiguration(KeyboardDevicesCollection devices)
{
var configuration = new Configuration();
var languageMappings = configuration.LanguageMappings;

string configFilePath = GetConfigFilePath();
if (File.Exists(configFilePath))
{
using (TextReader input = File.OpenText(configFilePath))
{

var layouts = Layout.EnumerateLayouts().ToDictionary(k => k.Identifier, v => v);

string line;
while ((line = input.ReadLine()) != null)
{
string[] parts = line.Split('=');
Debug.Assert(parts.Length == 2);

string deviceName = parts[0];
var layoutId = new IntPtr(int.Parse(parts[1], NumberStyles.HexNumber));

if (devices.TryGetByName(deviceName, out var deviceHandle)
&& layouts.TryGetValue(layoutId, out var layout))
{
languageMappings.Add(deviceHandle, layout);
}
}
}
}

return configuration;
}

public void Save(KeyboardDevicesCollection devices)
{
string configFilePath = GetConfigFilePath();
using (TextWriter output = File.CreateText(configFilePath))
{
foreach (var device in devices)
{
if (LanguageMappings.TryGetValue(device.Handle, out var layout))
{
output.WriteLine("{0}={1:X8}", device.Name, layout.Identifier.ToInt32());
}
}
}
}

private static string GetConfigFilePath()
{
string configFileDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RightKeyboard");
if (!Directory.Exists(configFileDir))
{
Directory.CreateDirectory(configFileDir);
}

return Path.Combine(configFileDir, "config.txt");
}
}
}
17 changes: 17 additions & 0 deletions RightKeyboard/KeyboardDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace RightKeyboard
{
public struct KeyboardDevice
{
public string Name { get; }

public IntPtr Handle { get; }

public KeyboardDevice(string name, IntPtr handle)
{
Name = name;
Handle = handle;
}
}
}
44 changes: 44 additions & 0 deletions RightKeyboard/KeyboardDevicesCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using RightKeyboard.Win32;

namespace RightKeyboard
{
public class KeyboardDevicesCollection : IEnumerable<KeyboardDevice>
{
private Dictionary<string, IntPtr> devicesByName = new Dictionary<string, IntPtr>();

public bool TryGetByName(string deviceName, out IntPtr deviceHandle)
{
return devicesByName.TryGetValue(deviceName, out deviceHandle);
}

public KeyboardDevicesCollection()
{
foreach (API.RAWINPUTDEVICELIST rawInputDevice in API.GetRawInputDeviceList())
{
if (rawInputDevice.dwType == API.RIM_TYPEKEYBOARD)
{
IntPtr deviceHandle = rawInputDevice.hDevice;
string deviceName = API.GetRawInputDeviceName(deviceHandle);
devicesByName.Add(deviceName, deviceHandle);
}
}
}

public IEnumerator<KeyboardDevice> GetEnumerator()
{
return devicesByName
.Select(kvp => new KeyboardDevice(kvp.Key, kvp.Value))
.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
58 changes: 12 additions & 46 deletions RightKeyboard/Layout.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,44 @@
using System;
using System.IO;
using System.Reflection;
using RightKeyboard.Win32;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

namespace RightKeyboard {
/// <summary>
/// Represents a keyboard layout
/// </summary>
public class Layout {
private readonly ushort identifier;

/// <summary>
/// Gets the layout's identifier
/// </summary>
public ushort Identifier {
get {
return identifier;
}
}

private readonly string name;
public IntPtr Identifier { get; }

/// <summary>
/// Gets the layout's name
/// </summary>
public string Name {
get {
return name;
}
}
public string Name { get; }

/// <summary>
/// Initializes a new instance of Layout
/// </summary>
/// <param name="identifier"></param>
/// <param name="name"></param>
public Layout(ushort identifier, string name) {
this.identifier = identifier;
this.name = name;
public Layout(IntPtr identifier, string name) {
this.Identifier = identifier;
this.Name = name;
}

public override string ToString() {
return name;
return Name;
}

private static Layout[] cachedLayouts = null;

/// <summary>
/// Gets the keyboard layouts from a ressource file
/// Gets the installed keyboard layouts
/// </summary>
/// <returns></returns>
public static Layout[] GetLayouts() {
if(cachedLayouts == null) {
List<Layout> layouts = new List<Layout>();
using(Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("RightKeyboard.Layouts.txt")) {
using(TextReader reader = new StreamReader(input)) {
string line;
while((line = reader.ReadLine()) != null) {
layouts.Add(GetLayout(line));
}
}
}
cachedLayouts = layouts.ToArray();
}
return cachedLayouts;
}

private static Layout GetLayout(string line) {
string[] parts = line.Trim().Split('=');

ushort identifier = ushort.Parse(parts[0], NumberStyles.HexNumber);
string name = parts[1];
return new Layout(identifier, name);
public static IEnumerable<Layout> EnumerateLayouts() {
return API.GetKeyboardLayoutList()
.Select(p => new Layout(p, API.GetKeyboardLayoutName(p)));
}
}
}
16 changes: 2 additions & 14 deletions RightKeyboard/LayoutSelectionDialog.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RightKeyboard.Win32;

namespace RightKeyboard {
public partial class LayoutSelectionDialog : Form {
Expand All @@ -19,15 +14,8 @@ private void LoadLanguageList() {
lbLayouts.Items.Clear();
recentLayoutsCount = 0;

IntPtr[] installedLayouts = API.GetKeyboardLayoutList();

foreach(Layout layout in RightKeyboard.Layout.GetLayouts()) {
foreach(IntPtr installedLayout in installedLayouts) {
ushort languageId = unchecked((ushort)installedLayout.ToInt32());
if(layout.Identifier == languageId) {
lbLayouts.Items.Add(layout);
}
}
foreach(Layout layout in Layout.EnumerateLayouts()) {
lbLayouts.Items.Add(layout);
}

lbLayouts.SelectedIndex = 0;
Expand Down
Loading