Recent

Author Topic: [SOLVED] OnKeyDown  (Read 2000 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 596
[SOLVED] OnKeyDown
« on: October 22, 2023, 05:10:11 pm »
Hello forum members, I would like to ask for an explanation why the code does not work, what I wrote wrong
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   case Key of
  4.   32:
  5.     begin
  6.       //code
  7.     end;
  8.   27:
  9.     begin
  10.       SpeedButton1Click(SpeedButton1);
  11.     end;
  12.   end;
  13. end;
  14.  
« Last Edit: October 22, 2023, 05:45:11 pm by Pe3s »

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: OnKeyDown
« Reply #1 on: October 22, 2023, 05:33:20 pm »
is KeyPreview := true in the form?
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: OnKeyDown
« Reply #2 on: October 22, 2023, 05:35:44 pm »
@Pe3s

That should work.

After years of doing programming I can be sure to tell you, bug can be hidden anywhere in the code, sometimes in the location you don't expect to be possible. You should look elsewhere. If you can't find it, you can show us the whole code.

Or you can download the code below, which have been tested to work on Linux:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Dialogs, Buttons;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     SpeedButton1: TSpeedButton;
  16.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  17.     procedure SpeedButton1Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.SpeedButton1Click(Sender: TObject);
  30. begin
  31.   ShowMessage('Button clicked!');
  32. end;
  33.  
  34. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  35.   );
  36. begin
  37.   case key of
  38.     32: Caption := 'You press space bar!';
  39.     27: SpeedButton1Click(SpeedButton1);
  40.     else
  41.       Caption := '';
  42.   end;
  43. end;
  44.  
  45. end.

is KeyPreview := true in the form?

I tested on Linux, that still worked even Form.KeyPrevious is False.
« Last Edit: October 22, 2023, 05:37:57 pm by Handoko »

Pe3s

  • Hero Member
  • *****
  • Posts: 596
Re: OnKeyDown
« Reply #3 on: October 22, 2023, 05:44:56 pm »
Sorry, I forgot about KeyPreview, it works now

Thank you for your answer, gentlemen

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: [SOLVED] OnKeyDown
« Reply #4 on: October 22, 2023, 05:50:36 pm »
Good to know about it. It behaves differently in Linux.

Pe3s

  • Hero Member
  • *****
  • Posts: 596
Re: [SOLVED] OnKeyDown
« Reply #5 on: October 22, 2023, 06:07:47 pm »
I'm thinking about switching from Windows to Linux

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: [SOLVED] OnKeyDown
« Reply #6 on: October 22, 2023, 06:26:50 pm »
I'm thinking about switching from Windows to Linux

 You can do that, if you want to be the "Maytag RepairMan"https://youtu.be/8ZHsxPEAUOI

The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: [SOLVED] OnKeyDown
« Reply #7 on: October 22, 2023, 07:46:08 pm »
I'm thinking about switching from Windows to Linux

Willing to try something new is good.

Linux is great because:
- Linux is free
- Plenty of free programs are available in Linux, no trial versions
- No need to install antivirus
- No need to defrag harddisk
- Faster than Windows because no antivirus and low disk fragmentation

The feature I like most in Linux is, I can keep all my program settings if I format and reinstall Linux. I set my user's configuration location (home folder) to its own disk partition. I already did it many times, after I formatted my root partition, reinstalled Linux and all the programs, all my program settings would be kept. My email program would have all the data intact, my browser bookmark were kept even the history and the last page were still there. And of course my Lazarus configurations won't changed. It is not possible in Windows, after format Windows and reinstall all the programs, we have to reconfigure the programs one-by-one, not an easy task.

All of those above sound great, aren't they? But:

- Too many Linux distros
- Installing certain VGA drivers are not easy
- Some USB Wifi have no drivers for Linux
- A lot of Windows-based games can't run properly on Linux
- Not many good video editing software are available for Linux
- Rarely but Linux can crash too
- Linux is as buggy as Windows

Some games could run properly on Linux using Wine but after Linux upgraded they became buggy. I configured my Samba correctly and my Linux computer could read and write Windows share folder. But after I upgraded the Linux it couldn't find Windows share folder, after several attempts I gave up. I could hibernate my computer, newer versions of Linux do not support hibernate. After emptying Linux recycle bin, the files are impossible to recover, in Windows we can use undelete programs.

n7800

  • Sr. Member
  • ****
  • Posts: 347
Re: [SOLVED] OnKeyDown
« Reply #8 on: December 22, 2023, 02:34:34 am »
Hello forum members, I would like to ask for an explanation why the code does not work, what I wrote wrong

The form never receives keyboard events if it has components that might have focus.

I tested on Linux, that still worked even Form.KeyPrevious is False.

Good to know about it. It behaves differently in Linux.

TSpeedButton just cannot have focus, unlike TButton.

--
And I recommend that you include the LCLType unit and use the constants VK_SPACE and VK_ESCAPE instead of the incomprehensible numbers 32 and 27. This unit is cross-platform.

Handoko

  • Hero Member
  • *****
  • Posts: 5436
  • My goal: build my own game engine using Lazarus
Re: [SOLVED] OnKeyDown
« Reply #9 on: December 22, 2023, 03:35:25 am »
And I recommend that you include the LCLType unit and use the constants VK_SPACE and VK_ESCAPE instead of the incomprehensible numbers 32 and 27. This unit is cross-platform.

You're right. That was a good advice.

 

TinyPortal © 2005-2018