Источник (source): http://www.news2news.com/vfp/?example=572&ver=vcs&PHPSESSID=3D014ff%3D
using System; using System.Windows.Forms; using RawInputHook.DeviceManager; namespace RawInputHook.WinForm { public partial class RawInputMonitorForm : Form { public RawInputMonitorForm() { InitializeComponent(); RawInputDeviceManager mgr = new RawInputDeviceManager(); mgr.RawInputEvent += new RawInputEventHandler(OnRawInputEvent); mgr.AddKeyboardDevice(this.Handle); mgr.RegisterDevices(); Application.AddMessageFilter(mgr); } void OnRawInputEvent(object sender, RawInputEventArgs e) { if (e.asciiCode != 0) { switch (e.asciiCode) { case 13: textBox1.Text += "\r\n"; break; default: textBox1.Text += (char)e.asciiCode; break; } } } } }
// ======================================
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace RawInputHook.DeviceManager
{
public delegate void RawInputEventHandler(
object sender, RawInputEventArgs e);
public class RawInputDeviceManager : IMessageFilter
{
const int WM_INPUT = 0x00ff;
public event RawInputEventHandler RawInputEvent;
List < RAWINPUTDEVICE > devices = new List < RAWINPUTDEVICE > ();
public void AddKeyboardDevice(IntPtr windowHandle)
{
AddDevice(1, 6, 0x100, windowHandle);
}
void AddDevice(ushort usagePage, ushort usage,
Int32 flags, IntPtr windowHandle)
{
devices.Add(new RAWINPUTDEVICE(usagePage, usage,
flags, windowHandle));
}
public bool RegisterDevices()
{
if (devices.Count == 0) return false;
RAWINPUTDEVICE[] d = devices.ToArray();
bool result = RegisterRawInputDevices(d, devices.Count,
Marshal.SizeOf(typeof(RAWINPUTDEVICE)));
return result;
}
void ProcessRawInput(IntPtr hRawInput)
{
RAWINPUT pData = new RAWINPUT();
int pcbSize = Marshal.SizeOf(typeof(RAWINPUT));
int result = GetRawInputData(hRawInput,
RawInputCommand.Input, out pData,
ref pcbSize, Marshal.SizeOf(typeof(RAWINPUTHEADER)));
if (result != -1)
{
if (pData.Header.Type == RawInputType.Keyboard)
{
if ((pData.Keyboard.Flags == 0) ||
(pData.Keyboard.Flags == 2))
{
int asciiCode = VKeyToChar(
pData.Keyboard.VirtualKey,
pData.Keyboard.MakeCode);
RawInputEvent(this,
new RawInputEventArgs(pData, asciiCode));
}
}
}
}
int VKeyToChar(int virtKey, int scanCode)
{
int asciiCode = 0;
byte[] kbdState = new byte[256];
GetKeyboardState(kbdState);
ToAscii(virtKey, scanCode, kbdState, ref asciiCode, 0);
return asciiCode;
}
#region IMessageFilter Members
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_INPUT)
{
if (m.WParam != (IntPtr)0) //the app is on background
{
if (RawInputEvent != null) //event implemented
{
ProcessRawInput(m.LParam);
return true; //stop the message
}
}
}
return false; //allow the message to continue
}
#endregion
[DllImport("user32.dll")]
static extern bool RegisterRawInputDevices(
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
RAWINPUTDEVICE[] pRawInputDevices,
int uiNumDevices, int cbSize);
[DllImport("user32.dll")]
static extern int GetRawInputData(IntPtr hRawInput,
RawInputCommand uiCommand, out RAWINPUT pData,
ref int pcbSize, int cbSizeHeader);
[DllImport("user32.dll")]
static extern int GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
static extern int MapVirtualKey(int uCode, int uMapType);
[DllImport("user32.dll")]
static extern int ToAscii(int uVirtKey, int uScanCode,
byte[] lpKeyState, ref int lpChar, int uFlags);
}
public class RawInputEventArgs : EventArgs
{
public RAWINPUT rawInput;
public int asciiCode = 0;
public RawInputEventArgs(RAWINPUT rawInput, int asciiCode)
{
this.rawInput = rawInput;
this.asciiCode = asciiCode;
}
}
[StructLayout(LayoutKind.Sequential)]
struct RAWINPUTDEVICE
{
public ushort UsagePage;
public ushort Usage;
public Int32 Flags;
public IntPtr WindowHandle;
public RAWINPUTDEVICE(ushort usagePage, ushort usage,
Int32 flags, IntPtr windowHandle)
{
UsagePage = usagePage;
Usage = usage;
Flags = flags;
WindowHandle = windowHandle;
}
}
[StructLayout(LayoutKind.Explicit)]
public struct RAWINPUT
{
[FieldOffset(0)]
public RAWINPUTHEADER Header;
[FieldOffset(16)]
public RAWKEYBOARD Keyboard;
//mouse and HID parts omitted
}
[StructLayout(LayoutKind.Sequential)]
public struct RAWINPUTHEADER
{
public RawInputType Type;
public int Size;
public IntPtr Device;
public IntPtr wParam;
}
[StructLayout(LayoutKind.Sequential)]
public struct RAWKEYBOARD
{
public ushort MakeCode;
public ushort Flags;
public ushort Reserved;
public ushort VirtualKey;
public int Message;
public int ExtraInformation;
}
public enum RawInputType
{
Mouse = 0,
Keyboard = 1,
HID = 2
}
enum RawInputCommand
{
Input = 0x10000003,
Header = 0x10000005
}
}
Комментариев нет:
Отправить комментарий