效果
    image.png

    1. #include <ntddk.h>
    2. PDEVICE_OBJECT myKeyBoard;
    3. typedef struct {
    4. PDEVICE_OBJECT LowKbdDevice;
    5. }DEVICE_EXTION,*PDEVICE_EXTENTION;
    6. typedef struct _KEYBOARD_INPUT_DATA {
    7. USHORT UnitId;
    8. USHORT MakeCode;
    9. USHORT Flags;
    10. USHORT Reserved;
    11. ULONG ExtraInformation;
    12. } KEYBOARD_INPUT_DATA, * PKEYBOARD_INPUT_DATA;
    13. int pendingkey = 0;
    14. VOID Unload(PDRIVER_OBJECT DriverObject) {
    15. LARGE_INTEGER interval = { 0 };
    16. PDEVICE_OBJECT DeviceObject = DriverObject->DeviceObject;
    17. interval.QuadPart = -10 * 1000 * 1000;
    18. IoDetachDevice(((PDEVICE_EXTENTION)DeviceObject->DeviceExtension)->LowKbdDevice);
    19. if (pendingkey)
    20. {
    21. KeDelayExecutionThread(KernelMode,FALSE,&interval);
    22. }
    23. IoDeleteDevice(myKeyBoard);
    24. KdPrint(("driver unloaded\r\n"));
    25. }
    26. NTSTATUS MyattachDevice(PDRIVER_OBJECT DriverObject) {
    27. NTSTATUS status;
    28. UNICODE_STRING TargatDevice = RTL_CONSTANT_STRING(L"\\Device\\KeyboardClass0");
    29. status = IoCreateDevice(DriverObject,sizeof(DEVICE_EXTION),NULL,FILE_DEVICE_KEYBOARD,0,FALSE,&myKeyBoard);
    30. if (!NT_SUCCESS(status))
    31. {
    32. return status;
    33. }
    34. myKeyBoard->Flags |= DO_BUFFERED_IO;
    35. myKeyBoard->Flags &= ~DO_DEVICE_INITIALIZING;
    36. RtlZeroMemory(myKeyBoard->DeviceExtension,sizeof(DEVICE_EXTION));
    37. status = IoAttachDevice(myKeyBoard,&TargatDevice,&((PDEVICE_EXTENTION)myKeyBoard->DeviceExtension)->LowKbdDevice);
    38. if (!NT_SUCCESS(status))
    39. {
    40. IoDeleteDevice(myKeyBoard);
    41. return status;
    42. }
    43. return status;
    44. }
    45. NTSTATUS ReadComplete(PDEVICE_OBJECT DeviceObject,PIRP Irp,PVOID Context){
    46. CHAR* KeyFlag[4] = {"keydown","keyup","e0","e1"};
    47. PKEYBOARD_INPUT_DATA Keys = (PKEYBOARD_INPUT_DATA)Irp->AssociatedIrp.SystemBuffer;
    48. int structnum = Irp->IoStatus.Information / sizeof(KEYBOARD_INPUT_DATA);
    49. if (Irp->IoStatus.Status == STATUS_SUCCESS)
    50. {
    51. for (int i = 0; i < structnum; i++)
    52. {
    53. KdPrint(("the scan code is %x (%s)\n", Keys->MakeCode, KeyFlag[Keys->Flags]));
    54. }
    55. }
    56. if (Irp->PendingReturned)
    57. {
    58. IoMarkIrpPending(Irp);
    59. }
    60. pendingkey--;
    61. return Irp->IoStatus.Status;
    62. }
    63. NTSTATUS DispatchRead(IN PDEVICE_OBJECT DeviceObject,PIRP Irp) {
    64. IoCopyCurrentIrpStackLocationToNext(Irp);
    65. IoSetCompletionRoutine(Irp, ReadComplete, NULL, TRUE, TRUE, TRUE);
    66. return IoCallDriver(((PDEVICE_EXTENTION)DeviceObject->DeviceExtension)->LowKbdDevice, Irp);
    67. }
    68. NTSTATUS DispatchPass(IN PDEVICE_OBJECT DeviceObject, PIRP Irp) {
    69. IoCopyCurrentIrpStackLocationToNext(Irp);
    70. pendingkey++;
    71. return IoCallDriver(((PDEVICE_EXTENTION)DeviceObject->DeviceExtension)->LowKbdDevice,Irp);
    72. }
    73. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
    74. NTSTATUS status;
    75. DriverObject->DriverUnload = Unload;
    76. int i;
    77. for (int i = 0; i <=IRP_MJ_MAXIMUM_FUNCTION; i++)
    78. {
    79. DriverObject->MajorFunction[i] = DispatchPass;
    80. }
    81. DriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
    82. KdPrint(("driver loaded\r\n"));
    83. status = MyattachDevice(DriverObject);
    84. if (!NT_SUCCESS(status))
    85. {
    86. KdPrint(("attach device fault\r\n"));
    87. }else {
    88. KdPrint(("attach device success\r\n"));
    89. }
    90. return STATUS_SUCCESS;
    91. }