如何编写一个本地化的屏幕上的键盘 [英] How to write an localized on-screen-keyboard
问题描述
我要屏幕键盘为我们公司的计划,该计划主要是对行业的PC用触摸功能使用上写的。
I have to write an on screen keyboard for our company's program, which is mostly used on industry's PCs with touch capability.
我们不能使用Windows默认键盘,因为我们并不需要在键盘上的键。于是,我被要求写在C#中自定义的。
We can't use the windows default keyboard because we don't need all keys on the keyboard. So I was asked to write a custom one in C#.
我已经发现的这个博客作为参考,但我不知道如何下手。
I already found this blog as reference, but I'm not sure how to start.
我创建了一个小型原型GUI并指定为每个键扫描码,而这些扫描码转换为相关的字符。并把它们发送到主动控制。但我不知道扫描码我应该使用。
I created a small prototype GUI and assign for each key a scancode, and translate these scancodes to the related character. And send them to the active control. But I'm not sure what scancodes I should use.
所以我的问题是,这是正确的方式来写一个OSK这样,如果它是扫描码我应该使用?任何链接?
So my question is, is that the correct way to write a OSK like this and if yes which scancodes should I use? Any links?
我也不能确定如何处理<大骨节病>移动骨节病>美国...
I'm also not sure how to handle the shift states...
编辑:
好吧,我做了些研究,并用OSK它读取当前的键盘布局,甚至想出了处理容易<大骨节病>移骨节病>状态(<大骨节病>移骨节病>和<大骨节病> Alt键的Gr 骨节病>)。我写了 KeyButton
类从按钮
继承,这 KeyButton
有一个类型字节的扫描码
属性,如果你指定一个有效的扫描码吧,在 KeyButton
将调用相关函数来得到正确的文本。我用从迈克尔·卡普兰博客的功能与一些小的修改。 。最终事实证明,我不得不这样做,因为他做了同样的
Okay I did a bit more research and came up with a osk which reads the current keyboard layout and even handles the easy shift states (Shift and Alt Gr). I wrote a KeyButton
class which inherits from Button
, this KeyButton
has a ScanCode
property of type byte and if you assign a valid scancode to it, the KeyButton
will call the related functions to get the correct text. I used the functions from Michael Kaplan blogs with some small changes. In the end it turned out that I just had to do the same as he did.
所以,回答我的问题是:是的,你必须在使用扫描码的按钮,然后拿到virtualkey和键盘布局中的Unicode。 使用这些扫描码。
So the answer to my question is: Yes, you have to use scancodes on your buttons and then get the virtualkey and the unicode from the keyboard layout. Use these scancodes.
现在我得到的人物留下的唯一一件事就是四处发送这些。
Now I get the characters the only thing left is to send these around.
推荐答案
我写映射键码字符WPF应用程序映射类。
可以这样能有所帮助。
I wrote mapping classes that map key code to character for WPF application. May be this can help.
public class KeyMapper
{
/// <summary>
/// Map key code to character.
/// If key code cannot be mapped returns empty char.
/// </summary>
public static char MapKey(Key key, bool shiftPressed, string culture)
{
CheckCulture(culture);
int englishVirtuaCode = KeyInterop.VirtualKeyFromKey(key);
return EnglishVirtualCodeToChar(englishVirtuaCode, shiftPressed, culture);
}
private static void CheckCulture(string culture)
{
InputLanguage language = InputLanguage.FromCulture(new CultureInfo(culture));
if (language == null)
throw new ArgumentException(string.Format("culture {0} does not exist.", culture));
}
private static char EnglishVirtualCodeToChar(int enlishVirtualCode, bool shiftPressed, string culture)
{
var scanCode = KeyMappingWinApi.MapVirtualKeyEx((uint)enlishVirtualCode, 0, EnglishCultureHandle);
var vitualKeyCode = KeyMappingWinApi.MapVirtualKeyEx(scanCode, 1, GetCultureHandle(culture));
byte[] keyStates = GetKeyStates(vitualKeyCode, shiftPressed);
const int keyInformationSize = 5;
var stringBuilder = new StringBuilder(keyInformationSize);
KeyMappingWinApi.ToUnicodeEx(vitualKeyCode, scanCode, keyStates, stringBuilder, stringBuilder.Capacity, 0, GetCultureHandle(culture));
if (stringBuilder.Length == 0)
return ' ';
return stringBuilder[0];
}
private static IntPtr EnglishCultureHandle
{
get { return GetCultureHandle("en-US"); }
}
private static IntPtr GetCultureHandle(string culture)
{
return InputLanguage.FromCulture(new CultureInfo(culture)).Handle;
}
/// <summary>
/// Gets key states for ToUnicodeEx function
/// </summary>
private static byte[] GetKeyStates(uint keyCode, bool shiftPressed)
{
const byte keyPressFlag = 0x80;
const byte shifPosition = 16; // position of Shift key in keys array
var keyStatses = new byte[256];
keyStatses[keyCode] = keyPressFlag;
keyStatses[shifPosition] = shiftPressed ? keyPressFlag : (byte)0;
return keyStatses;
}
}
public class KeyMappingWinApi
{
[DllImport("user32.dll")]
public static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl);
[DllImport("user32.dll")]
public 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")]
public static extern short VkKeyScanEx(char ch, IntPtr dwhkl);
}
这篇关于如何编写一个本地化的屏幕上的键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!