Posted By

xyzeugene on 12/01/17


Tagged


Versions (?)

Keyboard Hooks in Lazarus


 / Published in: Pascal
 

URL: http://lazarus-ccr.sourceforge.net/docs/rtl/keyboard/kbddriver.html

Intercept keyboard events in Lazarus

  1. unit logkeys;
  2.  
  3. interface
  4.  
  5. Procedure StartKeyLogging;
  6. Procedure StopKeyLogging;
  7. Function IsKeyLogging : Boolean;
  8. Procedure SetKeyLogFileName(FileName : String);
  9.  
  10.  
  11. implementation
  12.  
  13. uses sysutils,keyboard;
  14.  
  15. var
  16. NewKeyBoardDriver,
  17. OldKeyBoardDriver : TKeyboardDriver;
  18. Active,Logging : Boolean;
  19. LogFileName : String;
  20. KeyLog : Text;
  21.  
  22. Function TimeStamp : String;
  23.  
  24. begin
  25. TimeStamp:=FormatDateTime('hh:nn:ss',Time());
  26. end;
  27.  
  28. Procedure StartKeyLogging;
  29.  
  30. begin
  31. Logging:=True;
  32. Writeln(KeyLog,'Start logging keystrokes at: ',TimeStamp);
  33. end;
  34.  
  35. Procedure StopKeyLogging;
  36.  
  37. begin
  38. Writeln(KeyLog,'Stop logging keystrokes at: ',TimeStamp);
  39. Logging:=False;
  40. end;
  41.  
  42. Function IsKeyLogging : Boolean;
  43.  
  44. begin
  45. IsKeyLogging:=Logging;
  46. end;
  47.  
  48. Function LogGetKeyEvent : TKeyEvent;
  49.  
  50. Var
  51. K : TKeyEvent;
  52.  
  53. begin
  54. K:=OldkeyboardDriver.GetKeyEvent();
  55. If Logging then
  56. begin
  57. Write(KeyLog,TimeStamp,': Key event: ');
  58. Writeln(KeyLog,KeyEventToString(TranslateKeyEvent(K)));
  59. end;
  60. LogGetKeyEvent:=K;
  61. end;
  62.  
  63. Procedure LogInitKeyBoard;
  64.  
  65. begin
  66. OldKeyBoardDriver.InitDriver();
  67. Assign(KeyLog,logFileName);
  68. Rewrite(KeyLog);
  69. Active:=True;
  70. StartKeyLogging;
  71. end;
  72.  
  73. Procedure LogDoneKeyBoard;
  74.  
  75. begin
  76. StopKeyLogging;
  77. Close(KeyLog);
  78. Active:=False;
  79. OldKeyBoardDriver.DoneDriver();
  80. end;
  81.  
  82. Procedure SetKeyLogFileName(FileName : String);
  83.  
  84. begin
  85. If Not Active then
  86. LogFileName:=FileName;
  87. end;
  88.  
  89. Initialization
  90. GetKeyBoardDriver(OldKeyBoardDriver);
  91. NewKeyBoardDriver:=OldKeyBoardDriver;
  92. NewKeyBoardDriver.GetKeyEvent:=@LogGetKeyEvent;
  93. NewKeyBoardDriver.InitDriver:=@LogInitKeyboard;
  94. NewKeyBoardDriver.DoneDriver:=@LogDoneKeyboard;
  95. LogFileName:='keyboard.log';
  96. Logging:=False;
  97. SetKeyboardDriver(NewKeyBoardDriver);
  98. end.
  99.  
  100. =======
  101.  
  102. program example9;
  103.  
  104. { This program demonstrates the logkeys unit }
  105.  
  106. uses keyboard,logkeys;
  107.  
  108. Var
  109. K : TKeyEvent;
  110.  
  111. begin
  112. InitKeyBoard;
  113. Writeln('Press keys, press "q" to end, "s" toggles logging.');
  114. Repeat
  115. K:=GetKeyEvent;
  116. K:=TranslateKeyEvent(K);
  117. Writeln('Got key : ',KeyEventToString(K));
  118. if GetKeyEventChar(K)='s' then
  119. if IsKeyLogging then
  120. StopKeyLogging
  121. else
  122. StartKeyLogging;
  123. Until (GetKeyEventChar(K)='q');
  124. DoneKeyBoard;
  125. end.

Report this snippet  

You need to login to post a comment.