Can not get the Key Scan Code - only the virtual key code lazarus / 31-LCL ---------------------------------------------------------------------- thehidden · 2020-06-30 12:07 Hi, For an Application I have set KeyPreview := true on all forms. I have tried some ways to get the keyboard scan code because I need to select if the return key or the enter key ad the number pad has been pressed. WM_KEYDOWN shows nothing. The sample from GetMem does not compile with Lazarus and 64bit. Any idea how to get the different scan codes? The virtual key codes (VK_RETURN) are identical. Thank you. ---------------------------------------------------------------------- jamie · 2020-06-30 12:46 https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key- codes The return keys are the same I believe ---------------------------------------------------------------------- thehidden · 2020-06-30 13:25 Quote from: jamie on June 30, 2020, 02:46:38 pm https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key- codes The return keys are the same I believe This are the virtual key codes. There are also Scan codes from the keyboard controller. There is "VK_Return" for the Return Key and "VK_Return + Enhanced Key" for the Enter Key at the Num Pad. The Member GetMem has made 2015 a demo for catching it, but this does not work with x64 compilation. ---------------------------------------------------------------------- jamie · 2020-06-30 13:32 Please show a very simple block of code you are using.. I can capture the vk's of the numpad they are in the list I showed you. There isn't much of in the way of capturing keys are the hardware level. You can also use GetKeyBoardState which captures all 255 keys at once but still, that isn't going to help you much.. So please show some minimum example block you are using.. btw, you need to include LCLtype and WINDOWS unit depending on how deep you go ---------------------------------------------------------------------- thehidden · 2020-06-30 13:51 The following code is only a part of the complete application. Some units are in use for other functions... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses LCLType, Windows,   LMessages, LCLIntf; .... procedure TForm1.FormKeyUp(Sender : TObject; var Key : Word; Shift : TShiftState);var   key1 : string;   //further usage in the applicationbegin   case key of      VK_ESCAPE: key1 := 'ESC';      VK_UP: key1 := 'UP';      VK_F1: key1 := 'F 1';      VK_NUMLOCK: key1 := 'NUMLOCK';      VK_SCROLL: key1 := 'SCROLLOCK';      VK_LCL_CAPSLOCK: key1 := 'CAPSLOCK';      VK_SNAPSHOT: key1 := 'PRINT';      VK_RCONTROL: key1 := 'CTRL R';      VK_PAUSE: key1 := 'PAUSE';      VK_LCL_LALT: key1 := 'L ALT';      VK_LCL_RALT: key1 := 'R ALT';      VK_LCL_ALT: key1 := 'ALT';      VK_LWIN: key1 := 'LWIN';      VK_RWIN: key1 := 'RWIN';      VK_TAB: key1 := '.TAB.';    VK_END: key1 := 'END';      VK_HOME: key1 := 'Home/Pos1';      VK_BACK: key1 := 'BACKSPACE';      VK_DELETE: key1 := 'Delete';      VK_INSERT: key1 := 'Insert';      VK_RETURN: key1 := 'Return-Main';//  VK_ENTER: key1 := 'Enter - Numpad';   // NOT WORKING!!!!      VK_NUMPAD5: key1 :='Num Pad 5';      VK_NEXT: key1 := 'PG DOWN';      VK_PRIOR: key1 :='PG UP';   end;                                                                end;  I need a solution for Line 31, because I need to know if the Return Key or Enter Key has been pressed. This will start two difference functions. There is an example from 2007, but this is for Delphi and gives no result with keypreview := true. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.WMKeyDown(var Message: TWMKeyDown);begin  inherited;  case Message.CharCode of    VK_RETURN: // ENTER pressed      if (Message.KeyData and $1000000 <> 0) then        // Test bit 24 of lParam        ShowMessage('ENTER on numeric keypad')      else        ShowMessage('ENTER on Standard keyboard');  end;end; ---------------------------------------------------------------------- rvk · 2020-06-30 14:30 Quote from: thehidden on June 30, 2020, 03:51:09 pmThere is an example from 2007, but this is for Delphi and gives no result with keypreview := true. I don't think you can catch the numpad enter with OnKeyDown. And definitely not with OnKeyUp (which is even less flexible). But what's wrong with catching WM_KEYDOWN yourself? The problem is that you can't do it the same way as Delphi. In Delphi you can just add "message WM_KEYDOWN" in the implementation and catch that message. In Lazarus you can't catch non-user messages like this. See https://wiki.freepascal.org/Win32/64_Interface#Processing_non- user_messages_in_your_window You'll need to put in a hook to catch those. See for an example here https://forum.lazarus.freepascal.org/index.php /topic,38889.msg265561.html#msg265561 ---------------------------------------------------------------------- balazsszekely · 2020-06-30 14:30 @thehidden QuoteThe Member GetMem has made 2015 a demo for catching it, but this does not work with x64 compilation.Did you try $MODE DELPHI? I cannot test now, I'm not in front of my computer. ---------------------------------------------------------------------- thehidden · 2020-06-30 14:43 Quote from: GetMem on June 30, 2020, 04:30:37 pm@thehidden QuoteThe Member GetMem has made 2015 a demo for catching it, but this does not work with x64 compilation.Did you try $MODE DELPHI? I cannot test now, I'm not in front of my computer. Error Message with {$mode objfpc} umain.pas(141,77) Error: Incompatible type for arg no. 2: Got "
", expected "" With {$mode delphi} it compiles completely but... keyhook shows 0xc1 for return and enter keypress does not show if return or enter is pressed and each new character has position 1 at line 1 ---------------------------------------------------------------------- balazsszekely · 2020-06-30 14:53 This with mode delphi: https://forum.lazarus.freepascal.org/index.php/topic,40752.msg281697.h tml#msg281697 PS: If does not work, I cannot help you now. ---------------------------------------------------------------------- jamie · 2020-06-30 14:57 There are issues with LCL not allowing to override the MESSAGES property... This should be corrected but it will never happen....  The Over ridden messages should be processed first before user messages gets called, this isn't the case however,....  Here is a example of the case … its one key stroke in the back. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows,Classes, SysUtils, Forms, Controls, Graphics, Dialogs,lcltype; type   { TForm1 }   TForm1 = class(TForm)    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);  private     OEMScanCode:Lparam;  public  procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;   procedure WMKeyUp(var Message: TWMKeyUP); message WM_KEYUP;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }procedure TForm1.WMKeyDown(var Message: TWMKeyDown);Begin  OEMSCANCode := Message.KeyData;  inherited;  end;procedure TForm1.WMKeyUp(var Message: TWMKeyUp);Begin  OEMSCANCode := Message.KeyData;  inherited;  end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState  );begin If  (key=VK_Return) Then If (OEMSCANCode and $1000000 <> 0) then Caption := 'VK_ENTER' else   caption := 'VK_RETURN'    else Caption := 'Not Interested';end; end. ---------------------------------------------------------------------- winni · 2020-06-30 15:01 Hi! Have a look at getmems LowLevelKeyboadHook in this forum: https://forum.lazarus.freepascal.org/index.php/topic,40752.msg281697.h tml#msg281697 Winni ---------------------------------------------------------------------- thehidden · 2020-06-30 15:07 Quote from: winni on June 30, 2020, 05:01:11 pmHi! Have a look at getmems LowLevelKeyboadHook in this forum: https://forum.lazarus.freepascal.org/index.php/topic,40752.msg281697.h tml#msg281697 Winni Tested. Result is always VK_Return ---------------------------------------------------------------------- thehidden · 2020-06-30 15:19 QuoteThere are issues with LCL not allowing to override the MESSAGES property... This should be corrected but it will never happen....  The Over ridden messages should be processed first before user messages gets called, this isn't the case however,....  Here is a example of the case … its one key stroke in the back. I have have copied everything complete, it shows on my keyboard only Return and never enter, regardless what key I am pressing. ---------------------------------------------------------------------- balazsszekely · 2020-06-30 15:28 @ thehidden QuoteTested. Result is always VK_ReturnYou should also check flags in the structure. See here for more details(bits/description): https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser- kbdllhookstruct QuoteSpecifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0. ---------------------------------------------------------------------- jamie · 2020-06-30 15:47 The example I posted does work for me, its just one stroke behind and that's the fault of the LCL handling it in the wrong order.  Oh well... ---------------------------------------------------------------------- jamie · 2020-06-30 16:16 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows,Classes, SysUtils, Forms, Controls, Graphics, Dialogs,lcltype; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);  private  public  end; var  Form1: TForm1;  TheHookHandle:THandle;  OEMScanCode:LParam; implementation {$R *.lfm}Function KeyboardProc(code:integer;aWparam:WPARAM;aLParam:LParam):LResult; StdCall;Begin  Result := 0;  OEMScanCode := aLParam;  if Code < 0 Then  Result := CallNextHookEx(TheHookHandle,code,aWparam, ALParam);end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  TheHookHandle:= SetWindowsHookEx(WH_KEYBOARD,@Keyboard Proc,Hinstance,GetThreadID);end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState  );var D:WORD;begin If  (key=VK_Return) Then If (OEMSCANCode and $1000000 <> 0) then Caption := 'VK_ENTER' else   caption := 'VK_RETURN'    else Caption := 'Not Interested';end; end. ---------------------------------------------------------------------- thehidden · 2020-06-30 17:04 Dell Keyboard. Will check it later with another one. ---------------------------------------------------------------------- thehidden · 2020-06-30 22:08 Quote from: jamie on June 30, 2020, 06:16:21 pmThis 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 ---------------------------------------------------------------------- jamie · 2020-06-30 23:16 I used a cheap K120 Logitech from Walmarts I go through them like changing my underwear! ---------------------------------------------------------------------- pcurtis · 2020-08-13 14:18 @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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, lcltype,  StdCtrls;   type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private  public  end; var  Form1: TForm1;  HookHandle:THandle;  OEMScanCode:LParam; const  WH_KEYBOARD_LL = 13; implementation {$R *.lfm}Function KeyboardHOOK(nCode : integer; aWparam:WPARAM; aLParam:LParam) : LResult; StdCall;type  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;  TKBDLLHOOKSTRUCT = packed record    vkCode: DWORD;    scanCode: DWORD;    flags: DWORD;    time: DWORD;    dwExtraInfo: DWORD;end; var  LKBDLLHOOKSTRUCT : PKBDLLHOOKSTRUCT;Begin  case nCode of    HC_ACTION :      begin      LKBDLLHOOKSTRUCT := PKBDLLHOOKSTRUCT(alParam);         if (LKBDLLHOOKSTRUCT^.vkCode = VK_UP)  then          begin            if (awParam=WM_KEYUP) or (awParam = WM_SYSKEYUP) then              begin                 keybd_event(LKBDLLHOOKSTRUCT^.vkCode, 0, KEYEVENTF_KEYUP, 0)              end            else              begin                keybd_event(LKBDLLHOOKSTRUCT^.vkCode, 0, 0, 0);            end;            result := 1;            exit;          end;    end;  end;   Result := CallNextHookEx(HookHandle, nCode, awParam, alParam);end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  HookHandle := SetWindowsHookEx(WH_KEYBOARD_LL ,@KeyboardHOOK, Hinstance, 0);end; procedure TForm1.FormDestroy(Sender: TObject);begin  if (HookHandle <> 0) then  begin      UnhookWindowsHookEx(HookHandle);      HookHandle := 0;    end;end; end.  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. ---------------------------------------------------------------------- jamie · 2020-08-13 21:52 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. ---------------------------------------------------------------------- pcurtis · 2020-08-13 23:02 Thanks.