Recent

Author Topic: How to get keyboard scan codes  (Read 3859 times)

jeff

  • New Member
  • *
  • Posts: 18
How to get keyboard scan codes
« 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?

RayoGlauco

  • Full Member
  • ***
  • Posts: 176
  • Beers: 1567
To err is human, but to really mess things up, you need a computer.

jeff

  • New Member
  • *
  • Posts: 18
Re: How to get keyboard scan codes
« Reply #2 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.

jeff

  • New Member
  • *
  • Posts: 18
Re: How to get keyboard scan codes
« Reply #3 on: April 03, 2018, 08:55:25 am »
I forgot to mention that I use windows.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: How to get keyboard scan codes
« Reply #4 on: April 03, 2018, 09:06:17 am »
Guess what? you can examine and use the keyboard unit.....
Specialize a type, not a var.

balazsszekely

  • Guest
Re: How to get keyboard scan codes
« Reply #5 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.

jeff

  • New Member
  • *
  • Posts: 18
Re: How to get keyboard scan codes
« Reply #6 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 :)

balazsszekely

  • Guest
Re: How to get keyboard scan codes
« Reply #7 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