Forum > LCL
How can I capture all key presses despite what component is in focus?
FragmentedCurve:
Let's say I have the following class,
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TMainForm = class(TForm) InputText: TLabel; ResetButton: TButton; BackButton: TButton; NextButton: TButton; procedure FormCreate(Sender: TObject); procedure HandleKeyPress(Sender: TObject; var Key: char); end;
HandleKeyPress updates the caption in InputText. How can I force HandleKeyPress to be called even if one of the 3 buttons are focused?
dsiders:
--- Quote from: FragmentedCurve on June 04, 2023, 02:33:34 am ---Let's say I have the following class,
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TMainForm = class(TForm) InputText: TLabel; ResetButton: TButton; BackButton: TButton; NextButton: TButton; procedure FormCreate(Sender: TObject); procedure HandleKeyPress(Sender: TObject; var Key: char); end;
HandleKeyPress updates the caption in InputText. How can I force HandleKeyPress to be called even if one of the 3 buttons are focused?
--- End quote ---
Focused or hovered? Controls have OnEnter and OnExit event handlers for focus changes. They also have OnMouseEnter and OnMouseLeave handlers for mouse hover.
Handoko:
Maybe:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, Forms, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure KeyPress(Sender: TObject; var Key: char); virtual; end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin Button1.OnKeyPress := @KeyPress; Button2.OnKeyPress := @KeyPress; Button3.OnKeyPress := @KeyPress;end; procedure TForm1.KeyPress(Sender: TObject; var Key: char);var S: string;begin if Sender = Button1 then S := 'first button.' else if Sender = Button2 then S := 'second button.' else if Sender = Button3 then S := 'third button.' else Exit; Label1.Caption := Key + ' is pressed on the ' + S;end; end.
FragmentedCurve:
--- Quote from: dsiders on June 04, 2023, 03:50:30 am ---Focused or hovered? Controls have OnEnter and OnExit event handlers for focus changes. They also have OnMouseEnter and OnMouseLeave handlers for mouse hover.
--- End quote ---
All the above. I'd like to capture all keyboard input under all conditions. Even if a TEdit instance is entered/focused/active/etc.
FragmentedCurve:
--- Quote from: Handoko on June 04, 2023, 03:57:56 am ---Maybe:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, Forms, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure KeyPress(Sender: TObject; var Key: char); virtual; end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin Button1.OnKeyPress := @KeyPress; Button2.OnKeyPress := @KeyPress; Button3.OnKeyPress := @KeyPress;end; procedure TForm1.KeyPress(Sender: TObject; var Key: char);var S: string;begin if Sender = Button1 then S := 'first button.' else if Sender = Button2 then S := 'second button.' else if Sender = Button3 then S := 'third button.' else Exit; Label1.Caption := Key + ' is pressed on the ' + S;end; end.
--- End quote ---
I'd rather not do it this way for two reasons: 1) I'd have to do this for every widget in the window which is more than the example in this post. 2) The keys are still be handled by the buttons. If the user hits enter, KeyPress does catch it but so does the button which means the button is clicked.
Edit: If you can prevent the second, this isn't so bad.
Navigation
[0] Message Index
[#] Next page