Recent

Author Topic: GetKeyState Bug?  (Read 7973 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5547
  • My goal: build my own game engine using Lazarus
Re: GetKeyState Bug?
« Reply #15 on: November 11, 2021, 06:15:32 pm »
Hello, I'm back.

Does the strange behavior also happen on Delphi? And should I submit a bug report?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: GetKeyState Bug?
« Reply #16 on: November 11, 2021, 06:43:06 pm »
Does 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: GetKeyState Bug?
« Reply #17 on: November 11, 2021, 06:52:13 pm »
Has anybody checked GTK3 yet?

MarkMLl

Hi!

Just tested:

gtk3 shows the same wrong  behaviour as gtk2

Winni

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: GetKeyState Bug?
« Reply #18 on: November 11, 2021, 07:36:14 pm »
gtk3 shows the same wrong  behaviour as gtk2

Thanks for doing that.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: GetKeyState Bug?
« Reply #19 on: February 09, 2022, 02:54:31 am »
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' :o 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][+][-]
  1. Function isStrInt(const inStr:String): boolean;
  2. var nTest : int64;
  3. begin
  4.      Try
  5.         isStrInt := True;
  6.         nTest := strToInt(inStr);
  7.         Except
  8.         on E:Exception do begin
  9.               isStrInt := False;
  10.         end;
  11.      end;
  12.  
  13. end;
  14.  
  15. procedure TForm1.Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
  16. begin
  17.  if IsStrInt(Edit1.Text) Then begin
  18.     edit3.text :='OK';
  19.     end
  20.     else begin
  21.          edit3.text :='not';
  22.  end;
  23.  
  24.   if IsStrInt(UTF8Key) Then begin
  25.      edit4.text := 'OK'
  26.      end
  27.      else begin
  28.          edit4.Text:= 'not';
  29.   end;
  30. end;
  31.  
  32. procedure TForm1.Edit1Change(Sender: TObject);
  33. begin
  34.    if IsStrInt(Edit1.Text) Then begin
  35.          edit2.text :='ok';
  36.     end
  37.     else begin
  38.          edit2.text :='not';
  39.     end;
  40. end;
« Last Edit: February 09, 2022, 10:45:18 am by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Handoko

  • Hero Member
  • *****
  • Posts: 5547
  • My goal: build my own game engine using Lazarus
Re: GetKeyState Bug?
« Reply #20 on: February 18, 2022, 01:28:29 pm »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Label3: TLabel;
  19.     procedure Edit1Change(Sender: TObject);
  20.     procedure Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
  21.     procedure FormCreate(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. procedure TForm1.Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
  32. var
  33.   S: string;
  34.   L: LongInt;
  35. begin
  36.   case TryStrToInt(Edit1.Text, L) of
  37.     True:  S := 'ok';
  38.     False: S := 'not';
  39.   end;
  40.   Label2.Caption := 'TryStroToInt(Edit1) in UTF8KeyPress: ' + S;
  41.  
  42.   case TryStrToInt(UTF8Key, L) of
  43.     True:  S := 'ok';
  44.     False: S := 'not';
  45.   end;
  46.   Label3.Caption := 'TryStroToInt(UTF8Key) in UTF8KeyPress: ' + S;
  47. end;
  48.  
  49. procedure TForm1.FormCreate(Sender: TObject);
  50. begin
  51.   Edit1.Text     := 'Type something';
  52.   Label1.Caption := '';
  53.   Label2.Caption := '';
  54.   Label3.Caption := '';
  55. end;
  56.  
  57. procedure TForm1.Edit1Change(Sender: TObject);
  58. var
  59.   S: string;
  60.   L: LongInt;
  61. begin
  62.   case TryStrToInt(Edit1.Text, L) of
  63.     True:  S := 'ok';
  64.     False: S := 'not';
  65.   end;
  66.   Label1.Caption := 'TryStroToInt in OnChange: ' + S;
  67. end;
  68.  
  69. end.

Zaxxon

  • New Member
  • *
  • Posts: 30
Re: GetKeyState Bug?
« Reply #21 on: February 19, 2022, 11:50:06 am »
I've noticed this some time ago. I use this to check for Esc and it has worked so far.

Code: Pascal  [Select][+][-]
  1. function EscPressed:Boolean;
  2. {$ifdef Linux}
  3. var G:SmallInt;
  4. {$endif}
  5. begin
  6.   {$ifdef Linux}
  7.   G:=GetKeyState(VK_ESCAPE);
  8.   EscPressed:=(G=-128) or (G=-127);              //GTK2
  9.   {$else}
  10.   EscPressed:=(GetKeyState(VK_ESCAPE)<0);
  11.   {$endif}
  12. end;

Zaxxon

  • New Member
  • *
  • Posts: 30
Re: GetKeyState Bug?
« Reply #22 on: February 19, 2022, 04:08:42 pm »
Running this code.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3. var G:SmallInt;
  4.  
  5. begin
  6.   while true do begin
  7.     G:=GetKeyState(VK_ESCAPE);
  8.     Self.Label1.Caption:=IntToStr(G);
  9.  
  10.     Application.ProcessMessages;
  11.   end;
  12. end;  

Pressing Escape gives a toggle between -128 and -127.  In Linux Mint Mate.

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: GetKeyState Bug?
« Reply #23 on: February 19, 2022, 05:30:11 pm »
The key state is a word, not a smallint.
objects are fine constructs. You can even initialize them with constructors.

Zaxxon

  • New Member
  • *
  • Posts: 30
Re: GetKeyState Bug?
« Reply #24 on: February 19, 2022, 05:42:56 pm »
In winapih.inc

Code: Pascal  [Select][+][-]
  1. function GetKeyState(nVirtKey: Integer): Smallint; {$IFDEF IF_BASE_MEMBER}virtual;{$ENDIF}

 

TinyPortal © 2005-2018