Recent

Author Topic: Can not get the Key Scan Code - only the virtual key code  (Read 5493 times)

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #15 on: June 30, 2020, 06:16:21 pm »
This works perfectly with my Win10_64 bit app.

if it does not work for you with the same setup then you must have a keyboard that does not produce separate scan codes for that key.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows,Classes, SysUtils, Forms, Controls, Graphics, Dialogs,lcltype;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  17.   private
  18.   public
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.   TheHookHandle:THandle;
  24.   OEMScanCode:LParam;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29. Function KeyboardProc(code:integer;aWparam:WPARAM;aLParam:LParam):LResult; StdCall;
  30. Begin
  31.   Result := 0;
  32.   OEMScanCode := aLParam;
  33.   if Code < 0 Then
  34.   Result := CallNextHookEx(TheHookHandle,code,aWparam, ALParam);
  35. end;
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. begin
  41.   TheHookHandle:= SetWindowsHookEx(WH_KEYBOARD,@KeyboardProc,Hinstance,GetThreadID);
  42. end;
  43.  
  44. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  45.   );
  46. var
  47.  D:WORD;
  48. begin
  49.  If  (key=VK_Return) Then If (OEMSCANCode and $1000000 <> 0) then Caption := 'VK_ENTER' else
  50.    caption := 'VK_RETURN'
  51.     else Caption := 'Not Interested';
  52. end;
  53.  
  54. end.
  55.  
  56.  
The only true wisdom is knowing you know nothing

thehidden

  • Jr. Member
  • **
  • Posts: 76
  • Freelancer
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #16 on: June 30, 2020, 07:04:38 pm »
Dell Keyboard.
Will check it later with another one.

I am mostly writing software for Image/Video based Process Documentation and Warehouse/Invoicing.
Other Software only if the project is interesting.

thehidden

  • Jr. Member
  • **
  • Posts: 76
  • Freelancer
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #17 on: July 01, 2020, 12:08:01 am »
This works perfectly with my Win10_64 bit app.

if it does not work for you with the same setup then you must have a keyboard that does not produce separate scan codes for that key.


Works perfect with another keyboard.

Looks like that the Dell Keyboard is not working. Time to get something new  :D
I am mostly writing software for Image/Video based Process Documentation and Warehouse/Invoicing.
Other Software only if the project is interesting.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #18 on: July 01, 2020, 01:16:22 am »
I used a cheap K120 Logitech from Walmarts

I go through them like changing my underwear! 

The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #19 on: August 13, 2020, 04:18:11 pm »
@Jamie - Sorry to butt in, but do you have any idea how to get the scancode while using WH_KEYBOARD_LL?

Let me expand a little. I am trying to create a SHIFT LOCK program.

So far I have this :-

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, lcltype,
  9.   StdCtrls;
  10.  
  11.  
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.   private
  21.   public
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   HookHandle:THandle;
  27.   OEMScanCode:LParam;
  28.  
  29. const
  30.   WH_KEYBOARD_LL = 13;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35. Function KeyboardHOOK(nCode : integer; aWparam:WPARAM; aLParam:LParam) : LResult; StdCall;
  36. type
  37.   PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  38.   TKBDLLHOOKSTRUCT = packed record
  39.     vkCode: DWORD;
  40.     scanCode: DWORD;
  41.     flags: DWORD;
  42.     time: DWORD;
  43.     dwExtraInfo: DWORD;
  44. end;
  45.  
  46. var
  47.   LKBDLLHOOKSTRUCT : PKBDLLHOOKSTRUCT;
  48. Begin
  49.   case nCode of
  50.     HC_ACTION :
  51.       begin
  52.         LKBDLLHOOKSTRUCT := PKBDLLHOOKSTRUCT(alParam);
  53.  
  54.         if (LKBDLLHOOKSTRUCT^.vkCode = VK_UP)  then
  55.           begin
  56.             if (awParam=WM_KEYUP) or (awParam = WM_SYSKEYUP) then
  57.               begin
  58.  
  59.                 keybd_event(LKBDLLHOOKSTRUCT^.vkCode, 0, KEYEVENTF_KEYUP, 0)
  60.               end
  61.             else
  62.               begin
  63.                 keybd_event(LKBDLLHOOKSTRUCT^.vkCode, 0, 0, 0);
  64.               end;
  65.             result := 1;
  66.             exit;
  67.           end;
  68.     end;
  69.   end;
  70.  
  71.   Result := CallNextHookEx(HookHandle, nCode, awParam, alParam);
  72. end;
  73.  
  74. { TForm1 }
  75.  
  76. procedure TForm1.FormCreate(Sender: TObject);
  77. begin
  78.   HookHandle := SetWindowsHookEx(WH_KEYBOARD_LL ,@KeyboardHOOK, Hinstance, 0);
  79. end;
  80.  
  81. procedure TForm1.FormDestroy(Sender: TObject);
  82. begin
  83.   if (HookHandle <> 0) then
  84.     begin
  85.       UnhookWindowsHookEx(HookHandle);
  86.       HookHandle := 0;
  87.     end;
  88. end;
  89.  
  90. end.
  91.  

The first problem I have is that I can't differentiate between the keyboard and keypad.
(I don't to return ! when I press 1 on the keypad)

Thanks in advance.
« Last Edit: August 13, 2020, 05:10:09 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #20 on: August 13, 2020, 11:52:53 pm »
These are  your key values

https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

VK_NUMPAD0..VK_NUMPAD9

 Also, I believe not all the keys are processed yet while in the hook event, you may need to use the GetKeyState(VK_??????) which will return <0 if currently pressed and testing BIT0 will tell you if its been toggled since the last time it was read.

The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Can not get the Key Scan Code - only the virtual key code
« Reply #21 on: August 14, 2020, 01:02:29 am »
Thanks.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018