⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idlekeyboard.dpr

📁 一个朋友写的delphi的漂亮时钟
💻 DPR
字号:
{*******************************************************}
{*            Email: fansheng_hx@163.com               *}
{*               QQ: 39262884                          *}
{*******************************************************}

library IdleKeyboard;

uses
  Windows,
  Messages,
  IdleConst in 'IdleConst.pas';

var
  MSG_KEYDOWN: UINT;
  MSG_KEYUP: UINT;
  hMappingFile: THandle;
  pMapMem: PKeyboardMappingMem;
  khook: HHook;

function KeyboardHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall
begin
  if iCode >= HC_ACTION then
  begin
    pMapMem^.KeyCode := wParam;
    case ((lParam shr 30) and $F) of
      0:                                // Key down
        begin
          pMapMem^.MsgID := MSG_KEYDOWN;
          SendMessage(pMapMem^.Handle, pMapMem^.MsgID, 0, 0);
        end;
      1:                                // key up
        begin
          pMapMem^.MsgID := MSG_KEYUP;
          SendMessage(pMapMem^.Handle, pMapMem^.MsgID, 0, 0);
        end;
    end;
  end;      
  Result := CallNextHookEx(khook, iCode, wParam, lParam);
end;

function EnableKeyboardHook(hWindow: HWND): BOOL; stdcall;
begin
  Result := False;
  if khook <> 0 then
    Exit;
  pMapMem^.Handle := hWindow;
  khook := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProc, HInstance, 0);
  Result := khook <> 0;
end;

function DisableKeyboardHook: BOOL; stdcall;
begin
  if khook <> 0 then
  begin
    UnhookWindowshookEx(khook);
    khook := 0;
  end;
  Result := khook = 0;
end;

procedure DllMain(dwReason: DWORD);
begin
  case dwReason of
    DLL_PROCESS_ATTACH:
      begin
        hMappingFile := OpenFileMapping(FILE_MAP_WRITE, False, KeyboardMappingFileName);
        if hMappingFile = 0 then
        begin
          hMappingFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
            0, SizeOf(TKeyboardMappingMem), KeyboardMappingFileName);
        end;
        if hMappingFile = 0 then
          MessageBox(0, 'cannot create share memory!', 'Error', MB_OK or MB_ICONERROR);

        pMapMem := MapViewOfFile(hMappingFile, FILE_MAP_WRITE or FILE_MAP_READ,
          0, 0, 0);
        if pMapMem = nil then
        begin
          CloseHandle(hMappingFile);
          MessageBox(0, 'cannot map share memory!', 'Error', MB_OK or MB_ICONERROR);
        end;
        khook := 0;
        MSG_KEYDOWN := RegisterWindowMessage(MSGKEYDOWN);
        MSG_KEYUP := RegisterWindowMessage(MSGKEYUP);
      end;
    DLL_PROCESS_DETACH:
      begin
        UnMapViewOfFile(pMapMem);
        CloseHandle(hMappingFile);
        if khook <> 0 then
          DisableKeyboardHook;
      end;
    DLL_THREAD_ATTACH:
      begin
      end;
    DLL_THREAD_DETACH:
      begin
      end;
  end;
end;

exports
  EnableKeyboardHook,
  DisableKeyboardHook;

begin
  DisableThreadLibraryCalls(HInstance);
  DLLProc := @DLLMain;
  DLLMain(DLL_PROCESS_ATTACH);
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -