想要使用windows api从键盘输入

时间:2014-12-05 07:07:57

标签: c# wpf winapi window user32

我想使用windows api&#39>来获得键盘输入(单个)

我有两个找到的选项 1. keybd_event() of user32.dll

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

2 user32.dll的SendInput()

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

我想在我的WPF应用程序中导入它们,我应该去哪个?

1 个答案:

答案 0 :(得分:2)

两种选择。

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);

由于您要定位WPF,因此您还需要向HwndSource添加WndProc。

此问题中的更多信息: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);

来自PInvoke.net的更多信息:SetWindowsHookEx (user32)