delphi

capturing all keystrokes

knoen 2016. 2. 21. 22:18
Hi, I've a need to capture ALL key presses using a program which won't
actually have focus when it is needed. It'll probably live in the system
tray.
Let me explain with an example what this program needs to do.
1) Capture a key press.
2) If it is an 'A' then make it a 'B' and stuff it back into the keyboard
buffer.
3) If its not an 'A' then let it pass through unchanged.
Is this even possible?
Any help would be very gratefully received.
Thanks,
Roy.
0
Roy
‎2003‎년 ‎12‎월 ‎8‎일
comp.lang.pascal.delphi.misc 5785 articles. 0 followers. miniFAQ (1) is leader. Post Follow

14 Replies
150 Views

Similar Articles

0.004 - 1
See related articles to this posting                         

Roy wrote:
> Hi, I've a need to capture ALL key presses using a program which won't
> actually have focus when it is needed. It'll probably live in the
> system tray.
>
> Let me explain with an example what this program needs to do.
>
> 1) Capture a key press.
> 2) If it is an 'A' then make it a 'B' and stuff it back into the
> keyboard buffer.
> 3) If its not an 'A' then let it pass through unchanged.
>
> Is this even possible?
The solution is creating a .dll with a Keyboard hook. I have one installed
on this pc which helps me avoid turning on CapsLock. Beware that I didn't
find a way to stop the CapsLock from being processed, so I had to send
another one ;-)
Here it goes (.Exe file loading the .dll:):
program CapsLock;
{$R *.RES}
uses
  Windows;
var
  HDLL: HMODULE;
  HookAddr: pointer;
const
  AHook: HHOOK = 0;
begin
  HDLL:=LoadLibrary('keybhook.dll');
  if HDLL > HINSTANCE_ERROR then begin
    HookAddr:=GetProcAddress(HDLL, 'KeybHookCallback');
    if Assigned(HookAddr) then
      AHook:=SetWindowsHookEx(WH_KEYBOARD, HookAddr, HDLL, 0);
  end;
  Sleep(INFINITE);
  if AHook > 0 then
    UnhookWindowsHookEx(AHook);
  if HDLL > HINSTANCE_ERROR then
    FreeLibrary(HDLL);
end.
------------
(.dll code):
library KeybHook;
uses
  Windows,
  Messages,
  UHook in 'UHook.pas';
exports
  KeybHookCallback;
{$R *.RES}
begin
end.
------------
unit UHook;
interface
uses Windows, Messages, SysUtils;
function KeybHookCallback(Code, wParam, lParam: integer): integer; stdcall;
implementation
const
  gFilterCapsLock: boolean = TRUE;
  gInSettingCaps: boolean = false;
procedure SetCapsLockState(Value: boolean);
var
   KeyState: TKeyboardState;
begin
  GetKeyboardState(keyState);
  if (Value xor boolean(keyState[VK_CAPITAL])) then begin
    gInSettingCaps:=true;
    keybd_event(VK_CAPITAL, $45, KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(VK_CAPITAL, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP,
0);
  end;
end;
function KeybHookCallback(Code, wParam, lParam: integer): integer; stdcall;
var
  AKeyFlag: Word;
begin
  result:=0;
  AKeyFlag:=(lParam shr 16);
  if  gFilterCapsLock
  and (Code = HC_ACTION)
  and ((wParam and $FFFF) = VK_CAPITAL) then begin
    if ((AKeyFlag and $FF00) = $C100) then // True only when key is sent
from keybd_event
      gInSettingCaps:=false
    else begin
      if not gInSettingCaps then begin
        beep;
        SetCapsLockState(false);
      end;
    end;
  end
  CallNextHookEx(0, Code, wParam, lParam);
end;
end.

- See more at: http://compgroups.net/comp.lang.pascal.delphi.misc/capturing-all-keystrokes/2405021#sthash.ASF0EXFu.dpuf

'delphi' 카테고리의 다른 글

다른 개발언어용 dll 만들기  (0) 2019.04.24
capslock wide hook  (0) 2016.07.13
Hide scrollbars in a TWebBrowser  (0) 2014.01.26
Save a web page from TWebBrowser as a JPEG file  (0) 2014.01.26
how to save everything in the webbrowser as Image  (0) 2014.01.26