Lazarus

Programming => General => Topic started by: jeff on April 03, 2018, 12:07:00 am

Title: How to get keyboard scan codes
Post by: jeff on April 03, 2018, 12:07:00 am
Hi there,
I want to write a small program that gets the hardware keyboard scan codes. I'm not sure that is the right term though, PassMark KeyboardTest calls it BIOS key code. For instance when I press Enter, it reads 0x1c, for numeric enter, 0x1c extended and so on. How to read these codes with lazarus?
Title: Re: How to get keyboard scan codes
Post by: RayoGlauco on April 03, 2018, 12:17:23 am
You can search the web, there is a lot of information.

You can see this too:

http://forum.lazarus.freepascal.org/index.php?topic=33028.0 (http://forum.lazarus.freepascal.org/index.php?topic=33028.0)

https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa299374(v=vs.60) (https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa299374(v=vs.60))
Title: Re: How to get keyboard scan codes
Post by: jeff on April 03, 2018, 08:51:42 am
Actually I had been googling for hours and I had found those web pages before I wrote my question here. Thanks for making the effort but unfortunately none of those code pieces are useful for me. They provide different codes, not the one I need for. For instance when I press the '7' and numeric '7' I get the same code (provided that NumLock is on). When the NumLock is off, pressing 'Home' and numeric '7' gives the same result, and because of that I cannot use them. I want to distinguish all the phisical buttons, including buttons in the numeric area, modifiers etc. independently from NumLock, CapsLock state.
Title: Re: How to get keyboard scan codes
Post by: jeff on April 03, 2018, 08:55:25 am
I forgot to mention that I use windows.
Title: Re: How to get keyboard scan codes
Post by: Thaddy on April 03, 2018, 09:06:17 am
Guess what? you can examine and use the keyboard unit.....
Title: Re: How to get keyboard scan codes
Post by: GetMem on April 03, 2018, 09:20:33 am
Low level keyboard hook(WH_KEYBOARD_LL) with PkbDLLHookStruct should do the job. I posted similar solutions many times before. Please do a forum search.
Title: Re: How to get keyboard scan codes
Post by: jeff on April 03, 2018, 10:43:57 am
Thank you Getmem, I found your keylog example, and that is what I have been looking for :)
Title: Re: How to get keyboard scan codes
Post by: GetMem on April 03, 2018, 10:46:14 am
Quote
Thank you Getmem, I found your keylog example, and that is what I have been looking for :)
You don't need all of it, just a small part(please check attachment). Something like this:
Code: Pascal  [Select][+][-]
  1. const
  2.   WH_KEYBOARD_LL = 13;
  3.  
  4. type
  5.   PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  6.   TKBDLLHOOKSTRUCT = packed record
  7.     vkCode: DWORD;
  8.     scanCode: DWORD;
  9.     flags: DWORD;
  10.     time: DWORD;
  11.     dwExtraInfo: DWORD;
  12.   end;
  13.  
  14. var
  15.   llKeyboardHook: HHOOK = 0;
  16.  
  17. function LowLevelKeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): HRESULT; stdcall;
  18. var
  19.   pkbhs: PKBDLLHOOKSTRUCT;
  20. begin
  21.   pkbhs := PKBDLLHOOKSTRUCT(Pointer(lParam));
  22.   if nCode = HC_ACTION then
  23.     fMain.Caption := '0x' + IntToHex(pkbhs^.scanCode, 2);
  24.   Result := CallNextHookEx(llKeyboardHook, nCode, wParam, lParam);
  25. end;
  26.  
  27. function TfMain.Start: boolean;
  28. begin
  29.   if llKeyboardHook = 0 then
  30.     llKeyboardHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardHook, HInstance, 0);
  31.   Result := (llKeyboardHook <> 0)
  32. end;
TinyPortal © 2005-2018