-1

I want to get the keyboard input (single) using windows api's

i have two found option 1. keybd_event() of user32.dll

VOID WINAPI keybd_event(
  _In_  BYTE bVk,
  _In_  BYTE bScan,
  _In_  DWORD dwFlags,
  _In_  ULONG_PTR dwExtraInfo
);

2 SendInput() of user32.dll

UINT WINAPI SendInput(
  _In_  UINT nInputs,
  _In_  LPINPUT pInputs,
  _In_  int cbSize
);

i want to import them in my WPF app which one should i go after ??

AVIK DUTTA
  • 736
  • 6
  • 23
  • Those functions generate input event messages. Is that what you want? It doesn't sound like it. – David Heffernan Dec 05 '14 at 07:29
  • Oo ..ok @David ...And yes i don't want that ... i just want to take input from key board by using the native api's – AVIK DUTTA Dec 05 '14 at 09:26
  • Please can you fix the question to make it 100% clear what you want. Please remove mention of these two unrelated functions. Please also explain why you can't use the standard built in WPF facilities to receive input. – David Heffernan Dec 05 '14 at 09:40

1 Answers1

2

Two alternatives.

RegisterHotKey

// Registers a hot key with Windows.
[DllImport(“user32.dll”)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport(“user32.dll”)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

Since you are targeting WPF you would also need to add a WndProc to your HwndSource.

More information in this question: How to handle WndProc messages in WPF?

SetWindowsHookEx

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool UnhookWindowsHookEx(IntPtr hhk);

More information from PInvoke.net: SetWindowsHookEx (user32)

Community
  • 1
  • 1
Cadde
  • 508
  • 4
  • 13
  • icame across this `delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);` every thing is ok but what are the parameters that it is taking(// i know there types but what are the values that i should pass) code=? wParam=? lParam=? – AVIK DUTTA Dec 05 '14 at 10:35
  • @AVIKDUTTA The arguments are filled in automatically by the Windows message pump / Queue. You would need to read about how WndProc works here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc%28v=vs.110%29.aspx – Cadde Dec 05 '14 at 11:35