我正在尝试获取用户从另一个应用程序按下的键,但我能得到的是没有键盘条件(按下大写/小写/移位)按下的键的位置,我想得到实际的关键
我的代码:
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static int enteredChar;
public static int EnteredChar
{
get { return enteredChar; }
}
public static void start()
{
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(
int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
// char letter=(char) Marshal.ReadByte(lParam);
enteredChar = Marshal.ReadInt32(lParam);
Console.WriteLine((Keys)enteredChar);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
public static int returnKey()
{
return enteredChar;
}