GetKeyState Bug? lazarus / 31-LCL ---------------------------------------------------------------------- Handoko · 2021-11-09 16:18 Tested on Lazarus 2.0.10 GTK2 Ubuntu Mate 21.10, I found GetKeyState not working correctly: Pressing the [right_shift], triggers both signalsPressing the [left_shift] and moving the mouse pointer to the form will trigger the right_shift signalCan you please test it on your system, before I buy myself a new keyboard. 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  Classes, Forms, Graphics, ExtCtrls, LCLIntf, LCLType; type   { TForm1 }   TForm1 = class(TForm)    Shape1: TShape;    Shape2: TShape;    Timer1: TTimer;    procedure Timer1Timer(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject);begin   // Left Shift  if GetKeyState(VK_LSHIFT) < 0 then    Shape1.Brush.Color := clRed  else  Shape1.Brush.Color := clWhite;   // Right Shift  if GetKeyState(VK_RSHIFT) < 0 then    Shape2.Brush.Color := clRed  else  Shape2.Brush.Color := clWhite; end; end. ---------------------------------------------------------------------- 440bx · 2021-11-09 17:20 Just in case this information might be helpful... tested on Win7 SP1, Lazarus 1.8.4, FPC v3.0.4 -> no problems tested on Win10 (21H1), Lazarus 2.2 RC1, FPC (whatever the version Lazarus uses is) -> no problems. ---------------------------------------------------------------------- howardpc · 2021-11-09 18:49 I can confirm the bug on Laz 2.2.0RC2 Linux Mint GTK2. Left shift works as expected, but moving the mouse pointer over the form then triggers the right shift signal. Right shift triggers both signals, which can be cleared by moving the mouse pointer into the form area. ---------------------------------------------------------------------- Lulu · 2021-11-09 18:57 All fine here, on Win10 (20H2) + Lazarus 2.010 ---------------------------------------------------------------------- lagprogramming · 2021-11-09 19:01 Indeed. Targeting linux-x86_64-gtk2 I find the same signal problems: Quote    Pressing the [right_shift], triggers both signals     Pressing the [left_shift] and moving the mouse pointer to the form will trigger the right_shift signal ---------------------------------------------------------------------- winni · 2021-11-09 19:41 Hi! Add this to your timer procedure and sit back in amazment: 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 label1.Caption := IntToStr (GetKeyState(VK_LSHIFT))+' / '+                  IntToStr (GetKeyState(VK_RSHIFT)); VK_LShift returns :  -127 / 0 VK_RShift returns : -127 / -128, then -128/-128, then again -127/-128 ..... So you can decide: if (VK_Rshift is 0) and (VK_Lshift < 0) then VKLshift ist pressed if VK_Rshift is -128 then VK_Rshift is pressed Miracles, miracles.... Lin64, gtk2 Winni ---------------------------------------------------------------------- MarkMLl · 2021-11-09 20:03 Quote from: Handoko on November 09, 2021, 05:18:48 pmPressing the [right_shift], triggers both signalsPressing the [left_shift] and moving the mouse pointer to the form will trigger the right_shift signalCan you please test it on your system, before I buy myself a new keyboard. Confirmed, also after left+over+release the right box remains red until the mouse if moved off. Lazarus 2.0.12 FPC 3.2.0 Debian "Buster" x86_64 using KDE desktop. /However/, I'd caution that the right shift (and for that matter control etc.) has always been a bit weird (I've looked at this carefully on multiple occasions so that I can build a "mavenwerk" backdoor into a UI), and it's likely that the current situation is a feature carefully-crafted for Delphi compatibility. MarkMLl ---------------------------------------------------------------------- winni · 2021-11-09 20:09 Hi! There will be a day where the only bug free parts of fpc/Lazarus are those which are not Delphi compatible .... Winni ---------------------------------------------------------------------- MarkMLl · 2021-11-09 20:50 Quote from: winni on November 09, 2021, 09:09:19 pmThere will be a day where the only bug free parts of fpc/Lazarus are those which are not Delphi compatible I very carefully didn't (quite) say that, lest it sounded like criticism of the core team. In actual fact I've been looking back through various code, and found comments (from myself) wingeing about how Delphi+Windows was actually better-implemented in that specific area. Hence this is very likely to be a widget set (or at least widget-set specific) area. My earlier test where I could duplicate the problem used gtk2. Switching to gtk3... "Error while linking". SEP (sorry). Switching to qt5... oh crap. This is a mess. If the window is foreground but the mouse is not over it releasing (not pressing) sets the LH window red, and even after it's released it stays red until the mouse is moved over the window. Pressing does exactly the same. If the mouse is over the window pressing either shift sets the LH window red, with some toggling behaviour that I can't quite fathom. MarkMLl ---------------------------------------------------------------------- winni · 2021-11-09 21:30 Hi! Aside from this special problem: The idea of " This is not a bug because this bug is Delphi compatible" - that can drive people to madness. Including me. Winni ---------------------------------------------------------------------- MarkMLl · 2021-11-10 08:47 Quote from: winni on November 09, 2021, 10:30:41 pmThe idea of " This is not a bug because this bug is Delphi compatible" - that can drive people to madness. Including me. For once we're in agreement, but I'd also extend that to "this isn't in the spirit of Pascal so isn't going into this implementation". MarkMLl ---------------------------------------------------------------------- loaded · 2021-11-10 10:48 The code is working properly. My work computer; Lazarus 2.0.12 32 Bit, Win 10 Enterprise 64 Bit ---------------------------------------------------------------------- Martin_fr · 2021-11-10 11:47 Apparently it works differently on Windows and Linux. I don't think that such a diff is a "Delphi compatibility feature".... ---------------------------------------------------------------------- PascalDragon · 2021-11-10 12:19 Quote from: MarkMLl on November 09, 2021, 09:50:14 pmHence this is very likely to be a widget set (or at least widget-set specific) area. Yes, cause it's implemented in LCLIntf which is mainly involved for providing WinAPI compatibility. GetKeyState is a WinAPI function and on other widget sets this needs to be emulated. So either something is broken there or the underlying API can not provide the data correctly. ---------------------------------------------------------------------- MarkMLl · 2021-11-10 13:03 Quote from: PascalDragon on November 10, 2021, 01:19:13 pmYes, cause it's implemented in LCLIntf which is mainly involved for providing WinAPI compatibility. GetKeyState is a WinAPI function and on other widget sets this needs to be emulated. So either something is broken there or the underlying API can not provide the data correctly. I've always found the RH shift/ctrl keys to be a bit dodgy and widgetset-specific. I'm not as able to work back through older versions as I used to be, so can't be of very much practical help. Has anybody checked GTK3 yet? MarkMLl ---------------------------------------------------------------------- Handoko · 2021-11-11 17:15 Hello, I'm back. Does the strange behavior also happen on Delphi? And should I submit a bug report? ---------------------------------------------------------------------- MarkMLl · 2021-11-11 17:43 Quote from: Handoko on November 11, 2021, 06:15:32 pmDoes the strange behavior also happen on Delphi? And should I submit a bug report? I don't have a recent version to test against, but nobody's seen it on a Windows system yet. I think you should definitely report it, noting in particular that gtk2 and Qt5 behave differently. Are you in a position to check gtk3 etc.? MarkMLl ---------------------------------------------------------------------- winni · 2021-11-11 17:52 Quote from: MarkMLl on November 10, 2021, 02:03:45 pmHas anybody checked GTK3 yet? MarkMLl Hi! Just tested: gtk3 shows the same wrong  behaviour as gtk2 Winni ---------------------------------------------------------------------- MarkMLl · 2021-11-11 18:36 Quote from: winni on November 11, 2021, 06:52:13 pmgtk3 shows the same wrong  behaviour as gtk2 Thanks for doing that. MarkMLl ---------------------------------------------------------------------- ArtLogi · 2022-02-09 01:54 Hello. I just ran a similar problem with editbox 'OnUTF8KeyPress' event, while implementing number type check (tried to, did drive me mad until changed the event to 'OnChange' where my logic did work as expected.) Going to sleep, took so long. I can post a sample code and version (older FPC/LCL) info tomorrow. It seems at my case that there is key stack index error in form that returned key would be n-1 value, can be seen that if I do enter 0 it returns 'not integer', when I hit backspace and box is empty it returns 'is integer' This in my old win7 machine. Or am I just too sleep deprived.  Edit. ..or the edit.text and 'OnUTF8KeyPress' event is out of sync. Edit on next day: LAZ 2.0.4 / FPC 3.0.4 / Win7 UTF8KeyPress event the UTF8Key is on sync, but the edit.text value is out of sync in n-1 fashion, not updated to event.. I wonder if this is intentional, I see logic at both ways n and n-1 value on occasion of keypress event, which both can cause a headache depending of the situation. Standard Laz. application from wiz, in unit1 implementation as follows 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 Function isStrInt(const inStr:String): boolean;var nTest : int64;begin     Try        isStrInt := True;        nTest := strToInt(inStr);        Except        on E:Exception do begin              isStrInt := False;        end;     end; end; procedure TForm1.Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);begin if IsStrInt(Edit1.Text) Then begin    edit3.text :='OK';    end    else begin         edit3.text :='not'; end;   if IsStrInt(UTF8Key) Then begin     edit4.text := 'OK'     end     else begin         edit4.Text:= 'not';  end;end; procedure TForm1.Edit1Change(Sender: TObject);begin   if IsStrInt(Edit1.Text) Then begin         edit2.text :='ok';    end    else begin         edit2.text :='not';    end;end; ---------------------------------------------------------------------- Handoko · 2022-02-18 12:28 I tested your code, I can understand what you meant but I don't think it is GetKeyState related. But, here I provide a compile-able demo so others can download and test it. 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  Classes, SysUtils, Forms, Controls, StdCtrls, LCLType; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    Label1: TLabel;    Label2: TLabel;  Label3: TLabel;    procedure Edit1Change(Sender: TObject);    procedure Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);var  S: string;  L: LongInt;begin  case TryStrToInt(Edit1.Text, L) of    True:  S := 'ok';    False: S := 'not';  end;  Label2.Caption := 'TryStroToInt(Edit1) in UTF8KeyPress: ' + S;   case TryStrToInt(UTF8Key, L) of    True:  S := 'ok';    False: S := 'not';  end;  Label3.Caption := 'TryStroToInt(UTF8Key) in UTF8KeyPress: ' + S;end; procedure TForm1.FormCreate(Sender: TObject);begin  Edit1.Text   := 'Type something';  Label1.Caption := '';  Label2.Caption := '';  Label3.Caption := '';end; procedure TForm1.Edit1Change(Sender: TObject);var  S: string;  L: LongInt;begin  case TryStrToInt(Edit1.Text, L) of    True:  S := 'ok';    False: S := 'not';  end;  Label1.Caption := 'TryStroToInt in OnChange: ' + S;end; end. ---------------------------------------------------------------------- Zaxxon · 2022-02-19 10:50 I've noticed this some time ago. I use this to check for Esc and it has worked so far. 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 function EscPressed:Boolean;{$ifdef Linux}var G:SmallInt;{$endif}begin  {$ifdef Linux}  G:=GetKeyState(VK_ESCAPE);  EscPressed:=(G=-128) or (G=-127);            //GTK2  {$else}  EscPressed:=(GetKeyState(VK_ESCAPE)<0);  {$endif}end; ---------------------------------------------------------------------- Zaxxon · 2022-02-19 15:08 Running this code. 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.Button1Click(Sender: TObject); var G:SmallInt; begin  while true do begin    G:=GetKeyState(VK_ESCAPE);    Self.Label1.Caption:=IntToStr(G);     Application.ProcessMessages;  end;end; Pressing Escape gives a toggle between -128 and -127.  In Linux Mint Mate. ---------------------------------------------------------------------- Thaddy · 2022-02-19 16:30 The key state is a word, not a smallint. ---------------------------------------------------------------------- Zaxxon · 2022-02-19 16:42 In winapih.inc 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 function GetKeyState(nVirtKey: Integer): Smallint; {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}