Added source code.

This commit is contained in:
2018-02-06 01:24:46 +02:00
parent 1d9f2990c8
commit 8b28da5b80
367 changed files with 22964 additions and 0 deletions

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace TibisMathematicsSuite.KeyboardIO
{
public static class Hotkey
{
private static HotkeyWindow window = new HotkeyWindow();
private static int currentId = 0;
public static event EventHandler<MyKeyEventArgs> HotkeyPressed
{
add { window.HotkeyPressed += value; }
remove { window.HotkeyPressed -= value; }
}
#region Winapi routines
// Registers a hot key with Windows.
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion
public static void Register(MyKey key)
{
currentId++;
if (!RegisterHotKey(window.Handle, currentId, (uint)key.Mods, (uint)key.Key))
throw new InvalidOperationException("Could not register the hotkey!");
}
public static void UnregisterAll()
{
for (; currentId > 0; currentId--)
UnregisterHotKey(window.Handle, currentId);
}
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TibisMathematicsSuite.KeyboardIO
{
class HotkeyWindow : NativeWindow, IDisposable
{
#region Constants
private const int WM_HOTKEY = 0x312;
#endregion
#region Events
public event EventHandler<MyKeyEventArgs> HotkeyPressed;
#endregion
#region Constructor
public HotkeyWindow()
{
this.CreateHandle(new System.Windows.Forms.CreateParams());
}
#endregion
#region Processor
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY && HotkeyPressed != null)
{
int vk = ((int)m.LParam >> 16) & 0xffff;
int mods = (int)m.LParam & 0xffff;
HotkeyPressed(this, new MyKeyEventArgs((Keys) vk, (MyKey.Modifiers) mods));
}
}
#endregion
#region Dispose
public void Dispose()
{
this.DestroyHandle();
}
#endregion
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TibisMathematicsSuite.KeyboardIO
{
class KeyMessageFilter : IMessageFilter
{
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private Dictionary<Keys, bool> keyPressed = new Dictionary<Keys, bool>();
public Dictionary<Keys, bool> KeyPressedTable { get { return keyPressed; } }
public event EventHandler<KeyEventArgs> KeyDown;
public event EventHandler<KeyEventArgs> KeyUp;
public bool IsKeyPressed(Keys key)
{
return keyPressed[key];
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_KEYDOWN)
{
keyPressed[(Keys)m.WParam] = true;
if (KeyDown != null) KeyDown(this, new KeyEventArgs((Keys)m.WParam));
}
else if (m.Msg == WM_KEYUP)
{
keyPressed[(Keys)m.WParam] = false;
if (KeyUp != null) KeyUp(this, new KeyEventArgs((Keys)m.WParam));
}
return false;
}
}
}

View File

@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace TibisMathematicsSuite.KeyboardIO
{
public class KeyShortcutBox : TextBox
{
#region Variables
private KeyMessageFilter keyFilter = new KeyMessageFilter();
private MyKey currentKey = new MyKey();
private bool inputFinished = false;
#endregion
#region Properties
public MyKey Key
{
get {
return MyKey.Parse(this.Text);
}
}
#endregion
#region Constructor
public KeyShortcutBox() : base()
{
this.ReadOnly = true;
this.BackColor = SystemColors.Window;
Application.AddMessageFilter(keyFilter);
keyFilter.KeyDown += new EventHandler<KeyEventArgs>(keyFilter_KeyDown);
keyFilter.KeyUp += new EventHandler<KeyEventArgs>(keyFilter_KeyUp);
}
#endregion
#region Key updating
private void keyFilter_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.LMenu:
case Keys.RMenu:
case Keys.Menu: currentKey.Mods &= ~MyKey.Modifiers.Alt; break;
case Keys.LShiftKey:
case Keys.RShiftKey:
case Keys.ShiftKey: currentKey.Mods &= ~MyKey.Modifiers.Shift; break;
case Keys.LControlKey:
case Keys.RControlKey:
case Keys.ControlKey: currentKey.Mods &= ~MyKey.Modifiers.Ctrl; break;
case Keys.LWin:
case Keys.RWin: currentKey.Mods &= ~MyKey.Modifiers.Win; break;
default:
inputFinished = true;
currentKey.Key = Keys.None;
break;
}
updateKey();
}
private void keyFilter_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.LMenu:
case Keys.RMenu:
case Keys.Menu: currentKey.Mods |= MyKey.Modifiers.Alt; break;
case Keys.LShiftKey:
case Keys.RShiftKey:
case Keys.ShiftKey: currentKey.Mods |= MyKey.Modifiers.Shift; break;
case Keys.LControlKey:
case Keys.RControlKey:
case Keys.ControlKey: currentKey.Mods |= MyKey.Modifiers.Ctrl; break;
case Keys.LWin:
case Keys.RWin: currentKey.Mods |= MyKey.Modifiers.Win; break;
default: currentKey.Key = e.KeyCode; break;
}
inputFinished = false;
updateKey();
}
private void updateKey()
{
if (!inputFinished && this.Focused && this.Enabled)
this.Text = currentKey.ToString();
}
#endregion
#region Reset
public void Reset()
{
this.Text = "";
this.inputFinished = false;
}
#endregion
}
}

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TibisMathematicsSuite.KeyboardIO
{
public class MyKey
{
#region Data types
[Flags]
public enum Modifiers
{
None = 0x0, Alt = 0x1, Ctrl = 0x2, Shift = 0x4, Win = 0x8
}
#endregion
#region Properties
public Modifiers Mods { get; set; }
public System.Windows.Forms.Keys Key { get; set; }
public bool TestModifier(Modifiers m)
{
return ((this.Mods & m) > 0);
}
#endregion
#region Constructors
public MyKey()
{
Mods = Modifiers.None;
Key = System.Windows.Forms.Keys.None;
}
public MyKey(System.Windows.Forms.Keys key)
{
Key = key;
Mods = Modifiers.None;
}
public MyKey(System.Windows.Forms.Keys key, Modifiers mods)
{
Key = key;
Mods = mods;
}
#endregion
#region To and from string
public static MyKey Parse(string str)
{
MyKey key = new MyKey();
System.Windows.Forms.Keys temp;
var keys = str.Split('+');
foreach (var i in keys)
switch (i)
{
case "Alt": key.Mods |= Modifiers.Alt; break;
case "Ctrl": key.Mods |= Modifiers.Ctrl; break;
case "Shift": key.Mods |= Modifiers.Shift; break;
case "Win": key.Mods |= Modifiers.Win; break;
default:
Enum.TryParse<System.Windows.Forms.Keys>(i, out temp);
key.Key = temp;
break;
}
return key;
}
public static string ToString(MyKey key)
{
StringBuilder str = new StringBuilder();
if ((key.Mods & Modifiers.Win) != 0) str.Append("Win+");
if ((key.Mods & Modifiers.Ctrl) != 0) str.Append("Ctrl+");
if ((key.Mods & Modifiers.Alt) != 0) str.Append("Alt+");
if ((key.Mods & Modifiers.Shift) != 0) str.Append("Shift+");
str.Append(Enum.GetName(typeof(System.Windows.Forms.Keys), key.Key));
return str.ToString();
}
public override string ToString()
{
return MyKey.ToString(this);
}
#endregion
#region Overloads
public override bool Equals(object obj)
{
MyKey b = obj as MyKey;
if (b == null) return false;
return (b.Key == this.Key && b.Mods == this.Mods);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static bool operator== (MyKey A, MyKey B)
{
if ((object)A == null || (object)B == null) return ((object)A == (object)B);
return (A.Key == B.Key) && (A.Mods == B.Mods);
}
public static bool operator!=(MyKey A, MyKey B)
{
return !(A == B);
}
#endregion
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TibisMathematicsSuite.KeyboardIO
{
public class MyKeyEventArgs : EventArgs
{
public MyKey Key { get; set; }
public MyKeyEventArgs() : base() { }
public MyKeyEventArgs(MyKey key)
{
Key = key;
}
public MyKeyEventArgs(System.Windows.Forms.Keys k, MyKey.Modifiers mods = MyKey.Modifiers.None)
{
Key = new MyKey(k, mods);
}
}
}