I'm trying to get the key the user pressed from another app but all i can get is the location of the key that has been pressed without the keyboard condition (uppercase/lowercase/shift pressed) and i want to get the actual key
my code:
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;
}