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

Merge Dev into Master #8

Open
wants to merge 6 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
.idea/

# User-specific files
*.suo
Expand Down
12 changes: 11 additions & 1 deletion Bukva/Bukva/Bukva.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="KeyTranslator\IKeyTranslator.cs" />
<Compile Include="KeyTranslator\KeyStateKeyTranslator.cs" />
<Compile Include="KeyTranslator\LowLevelKeyTranslator.cs" />
<Compile Include="PackageManager.xaml.cs">
<DependentUpon>PackageManager.xaml</DependentUpon>
</Compile>
<Compile Include="Win32.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
Expand All @@ -67,12 +73,15 @@
</Compile>
<Compile Include="FixedLengthQueue.cs" />
<Compile Include="KeyPressListener.cs" />
<Compile Include="KeyTranslator.cs" />
<Compile Include="LetterTable.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="PackageManager.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
Expand Down Expand Up @@ -100,5 +109,6 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
48 changes: 32 additions & 16 deletions Bukva/Bukva/KeyPressListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

Expand All @@ -15,7 +17,12 @@ abstract class KeyPressListener

protected void RaiseKeyPressedEvent(string keyPressed)
{
OnKeyPressed(this, new KeyPressedEventArgs(keyPressed));
OnKeyPressed(this, new KeyPressedEventArgs(keyPressed, false));
}

protected void RaiseKeyPressedEvent(string keyPressed, bool scrollLockPressed)
{
OnKeyPressed(this, new KeyPressedEventArgs(keyPressed, scrollLockPressed));
}

public abstract void DeleteLastKeyPressed();
Expand All @@ -24,14 +31,17 @@ protected void RaiseKeyPressedEvent(string keyPressed)

class LowLevelKeyboardHook : KeyPressListener , IDisposable
{

private Win32.CallBackHandler callBackHandler;
private IntPtr hookID = IntPtr.Zero;

private bool trap;

private byte[] keyboardState;
private StringBuilder converterBuffer;

public LowLevelKeyboardHook()
{

callBackHandler = HookCallback;
trap = false;
HookKeyboard();
Expand All @@ -51,6 +61,19 @@ public void Dispose()
Win32.UnhookWindowsHookEx(hookID);
}

private string VirtualKeyToString(int virtualKeyCode)
{
keyboardState = new byte[256];
if (Control.ModifierKeys.HasFlag(Keys.Shift))
keyboardState[(int) Keys.ShiftKey] = 0xff;
//else
//keyboardState[(int) Keys.ShiftKey] = 0x0;

converterBuffer = new StringBuilder(256);
Win32.ToUnicode((uint)virtualKeyCode, 0, keyboardState, converterBuffer, 256, 0);
return converterBuffer.ToString();
}

private bool userKey = true;
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
Expand All @@ -62,26 +85,16 @@ private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
if (nCode >= 0 && (wParam == (IntPtr)Win32.WM_KEYDOWN || wParam == (IntPtr)Win32.WM_SYSKEYDOWN) && Listen)
{
int virtualKeyCode = Marshal.ReadInt32(lParam);
Console.WriteLine(virtualKeyCode.ToString());
Key pressedKey = KeyInterop.KeyFromVirtualKey(virtualKeyCode);

string key;

if (Control.ModifierKeys.HasFlag(Keys.Shift))
{
key = pressedKey.ToString().ToUpper();
}
else
{
key = pressedKey.ToString().ToLower();
}
string key = VirtualKeyToString(virtualKeyCode);
Console.WriteLine(key);

if (key.ToUpper() == "OEMPERIOD")
{
key = ".";
}

RaiseKeyPressedEvent(key);
RaiseKeyPressedEvent(key, virtualKeyCode == 145 ? true : false);

if(trap) {
trap = false;
Expand All @@ -98,6 +111,7 @@ private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
public override void DeleteLastKeyPressed()
{
trap = true;
//Backspace();
}

public override void EmitBackspace()
Expand Down Expand Up @@ -180,10 +194,12 @@ public override void EmitBackspace()
public class KeyPressedEventArgs : EventArgs
{
public string KeyPressed { get; private set; }
public bool ScrollLockPressed { get; private set; }

public KeyPressedEventArgs(string key)
public KeyPressedEventArgs(string key, bool scrollLockPressed)
{
KeyPressed = key;
ScrollLockPressed = scrollLockPressed;
}
}
}
15 changes: 15 additions & 0 deletions Bukva/Bukva/KeyTranslator/IKeyTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bukva
{
interface IKeyTranslator
{
void Start();
void Stop();
void SetLetterTable(LetterTable newLetterTable);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Bukva
{
class KeyTranslator

class KeyStateKeyTranslator
{
FixedLengthQueue<string> buffer;
LetterTable letterTable;

KeyPressListener keyPressListener;

public KeyTranslator(LetterTable letterTable)
public KeyStateKeyTranslator(LetterTable letterTable)
{
buffer = new FixedLengthQueue<string>(3);
buffer.Clear("");
this.letterTable = letterTable;

//keyPressListener = new KeyStateListener();
keyPressListener = new LowLevelKeyboardHook();
keyPressListener = new KeyStateListener();
keyPressListener.Listen = false;
keyPressListener.OnKeyPressed += OnKeyPressed;
}

public void SetLetterTable(LetterTable newLetterTable)
{
letterTable = newLetterTable;
}

public void Start()
{
keyPressListener.Listen = true;
Expand All @@ -42,7 +46,9 @@ private void OnKeyPressed(object sender, KeyPressedEventArgs e)
if (Control.ModifierKeys.HasFlag(Keys.Control) || key == "none")
return;

if (key == "back")
Console.WriteLine(key);

if (key == "\b" || key == "back")
{
HandleUndo();
}
Expand Down
140 changes: 140 additions & 0 deletions Bukva/Bukva/KeyTranslator/LowLevelKeyTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Windows.Forms;

namespace Bukva
{
class LowLevelKeyTranslator : IKeyTranslator
{
FixedLengthQueue<string> buffer;
LetterTable letterTable;

KeyPressListener keyPressListener;

public LowLevelKeyTranslator(LetterTable letterTable)
{
buffer = new FixedLengthQueue<string>(3);
buffer.Clear("");
this.letterTable = letterTable;

//keyPressListener = new KeyStateListener();
keyPressListener = new LowLevelKeyboardHook();
keyPressListener.Listen = false;
keyPressListener.OnKeyPressed += OnKeyPressed;
}

public void SetLetterTable(LetterTable newLetterTable)
{
letterTable = newLetterTable;
}

public void Start()
{
keyPressListener.Listen = true;
}

public void Stop()
{
keyPressListener.Listen = false;
buffer.Clear(""); //What if OnKeyPressed is being executed at this time?
}

private void OnKeyPressed(object sender, KeyPressedEventArgs e)
{
string key = e.KeyPressed;


if (Control.ModifierKeys.HasFlag(Keys.Control) || key == "none" || key == "")
return;

Console.WriteLine(key);

if (key == "\b" || key == "back")
{
HandleUndo();
}
else
{
HandleKeyPress(key);
}

buffer.Insert(key);
}

private void HandleKeyPress(string key)
{
if (letterTable.ContainsKey(buffer.At(1) + buffer.At(2) + key))
{
if (letterTable.ContainsKey(buffer.At(2)) || letterTable.ContainsKey(buffer.At(1) + buffer.At(2)))
{
keyPressListener.EmitBackspace(); //Replace previous translation with new one, if it existed
}

if (!letterTable.ContainsKey(buffer.At(1) + buffer.At(2)))
keyPressListener.EmitBackspace();

keyPressListener.DeleteLastKeyPressed();

SendKeys.SendWait(letterTable[buffer.At(1) + buffer.At(2) + key]);
}
else if (letterTable.ContainsKey(buffer.At(2) + key))
{
keyPressListener.DeleteLastKeyPressed();

if (letterTable.ContainsKey(buffer.At(2)))
keyPressListener.EmitBackspace(); //Replace previous translation with new one, if it existed


SendKeys.SendWait(letterTable[buffer.At(2) + key]);
}
else if (letterTable.ContainsKey(key))
{
keyPressListener.DeleteLastKeyPressed();

SendKeys.SendWait(letterTable[key]);
}
}

private void HandleUndo()
{
keyPressListener.EmitBackspace();
keyPressListener.DeleteLastKeyPressed();

if (letterTable.ContainsKey(buffer.At(0) + buffer.At(1) + buffer.At(2)))
{
if (letterTable.ContainsKey(buffer.At(0) + buffer.At(1)))
{
SendKeys.SendWait(letterTable[buffer.At(0) + buffer.At(1)]);

if (letterTable.ContainsKey(buffer.At(2)))
SendKeys.SendWait(letterTable[buffer.At(2)]);
else
SendKeys.SendWait(buffer.At(2));
}
else
{
if (letterTable.ContainsKey(buffer.At(0)))
SendKeys.SendWait(letterTable[buffer.At(0)]);
else
SendKeys.SendWait(buffer.At(0));


if (letterTable.ContainsKey(buffer.At(1)))
SendKeys.SendWait(letterTable[buffer.At(1)]);
else
SendKeys.SendWait(buffer.At(1));


if (letterTable.ContainsKey(buffer.At(2)))
SendKeys.SendWait(letterTable[buffer.At(2)]);
else
SendKeys.SendWait(buffer.At(2));
}
}
else if (letterTable.ContainsKey(buffer.At(1) + buffer.At(2)))
{
SendKeys.SendWait(letterTable[buffer.At(1)]);
SendKeys.SendWait(letterTable[buffer.At(2)]);
}
}
}
}
14 changes: 10 additions & 4 deletions Bukva/Bukva/LetterTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ class LetterTable : Dictionary<string, string>
{
public string Filename { get; private set; }


//Empty Letter table
public LetterTable() : base()
{
List<string> list = GetCandidateFiles();
Filename = list[0].Split('\\').Last();
ReadInFromFile(list[0]);

}

public LetterTable(string filename) : base()
{
Filename = filename.Split('\\').Last();
ReadInFromFile(filename);
}

public void ReadInFromFile(string filename)
Expand All @@ -34,7 +40,7 @@ public void ReadInFromFile(string filename)
}
}

List<string> GetCandidateFiles()
public static List<string> GetCandidateFiles()
{
List<string> languageFiles = new List<string>();
string[] files = Directory.GetFiles(".");
Expand Down
Loading