|
|
|
@ -4,7 +4,11 @@
@@ -4,7 +4,11 @@
|
|
|
|
|
using System; |
|
|
|
|
using System.Collections; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Globalization; |
|
|
|
|
using System.Reflection; |
|
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
using System.Text; |
|
|
|
|
using System.Threading; |
|
|
|
|
using System.Windows; |
|
|
|
|
using System.Windows.Controls; |
|
|
|
|
using System.Windows.Controls.Primitives; |
|
|
|
@ -240,5 +244,79 @@ namespace ICSharpCode.Core.Presentation
@@ -240,5 +244,79 @@ namespace ICSharpCode.Core.Presentation
|
|
|
|
|
{ |
|
|
|
|
return (KeyGesture)new KeyGestureConverter().ConvertFromInvariantString(text.Replace('|', '+')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static string GetDisplayStringForShortcut(KeyGesture kg) |
|
|
|
|
{ |
|
|
|
|
string old = kg.GetDisplayStringForCulture(Thread.CurrentThread.CurrentUICulture); |
|
|
|
|
string text = KeyCodeConversion.KeyToUnicode(kg.Key.ToKeys()); |
|
|
|
|
if (text != null) { |
|
|
|
|
if ((kg.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) |
|
|
|
|
text = "Alt+" + text; |
|
|
|
|
if ((kg.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) |
|
|
|
|
text = "Shift+" + text; |
|
|
|
|
if ((kg.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) |
|
|
|
|
text = "Ctrl+" + text; |
|
|
|
|
if ((kg.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows) |
|
|
|
|
text = "Win+" + text; |
|
|
|
|
return text; |
|
|
|
|
} |
|
|
|
|
return old; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static class KeyCodeConversion |
|
|
|
|
{ |
|
|
|
|
[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); |
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")] |
|
|
|
|
static extern bool GetKeyboardState(byte[] pbKeyState); |
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")] |
|
|
|
|
static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl); |
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")] |
|
|
|
|
static extern IntPtr GetKeyboardLayout(uint idThread); |
|
|
|
|
|
|
|
|
|
/// <remarks>Only works with Windows.Forms.Keys. The WPF Key enum seems to be horribly distorted!</remarks>
|
|
|
|
|
public static string KeyToUnicode(WinForms.Keys key) |
|
|
|
|
{ |
|
|
|
|
StringBuilder sb = new StringBuilder(255); |
|
|
|
|
byte[] keyState = new byte[255]; |
|
|
|
|
IntPtr hkl = GetKeyboardLayout(0); |
|
|
|
|
|
|
|
|
|
if (!GetKeyboardState(keyState)) return null; |
|
|
|
|
|
|
|
|
|
uint scanCode = MapVirtualKeyEx((uint)key, 0, hkl); |
|
|
|
|
if (scanCode < 1) return null; |
|
|
|
|
|
|
|
|
|
ClearKeyboardBuffer(hkl); |
|
|
|
|
int len = ToUnicodeEx((uint)key, scanCode, keyState, sb, sb.Capacity, 0, hkl); |
|
|
|
|
if (len > 0) |
|
|
|
|
return sb.ToString(0, len).ToUpper(); |
|
|
|
|
|
|
|
|
|
ClearKeyboardBuffer(hkl); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void ClearKeyboardBuffer(IntPtr hkl) |
|
|
|
|
{ |
|
|
|
|
StringBuilder sb = new StringBuilder(10); |
|
|
|
|
uint key = (uint)WinForms.Keys.Space; |
|
|
|
|
int rc; |
|
|
|
|
do { |
|
|
|
|
rc = ToUnicodeEx(key, MapVirtualKeyEx(key, 0, hkl), new byte[255], sb, sb.Capacity, 0, hkl); |
|
|
|
|
} while(rc < 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static WinForms.Keys ToKeys(this Key key) |
|
|
|
|
{ |
|
|
|
|
WinForms.Keys result; |
|
|
|
|
if (Enum.TryParse(key.ToString(), out result)) |
|
|
|
|
return result; |
|
|
|
|
return WinForms.Keys.None; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|