是否可以根据布局确定按下的字母键?
这是代码(类):
class GlobalKeyboardHookEventArgs : HandledEventArgs
{
public GlobalKeyboardHook.KeyboardState KeyboardState { get; private set; }
public GlobalKeyboardHook.LowLevelKeyboardInputEvent KeyboardData { get; private set; }
public GlobalKeyboardHookEventArgs(
GlobalKeyboardHook.LowLevelKeyboardInputEvent keyboardData,
GlobalKeyboardHook.KeyboardState keyboardState)
{
KeyboardData = keyboardData;
KeyboardState = keyboardState;
}
}
//Based on https://gist.github.com/Stasonix
class GlobalKeyboardHook : IDisposable
{
public event EventHandler<GlobalKeyboardHookEventArgs> KeyboardPressed;
public GlobalKeyboardHook()
{
_windowsHookHandle = IntPtr.Zero;
_user32LibraryHandle = IntPtr.Zero;
_hookProc = LowLevelKeyboardProc; // we must keep alive _hookProc, because GC is not aware about SetWindowsHookEx behaviour.
_user32LibraryHandle = LoadLibrary("User32");
if (_user32LibraryHandle == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to load library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}
.............
并致电:
private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
{
KeysConverter kc = new KeysConverter();
if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown)
{
GrabbedKeys += kc.ConvertToString(e.KeyboardData.VirtualCode);
return;
}
if (e.KeyboardData.VirtualCode != GlobalKeyboardHook.VkSnapshot)
return;
}
它的工作正常,如果我按下键盘的'我的程序存储's',但我怎么能为俄语键盘布局做同样的事情?如果键盘布局是ru-RU,我按's',我的应用程序存储'ы'?