Lazarus

Programming => LCL => Topic started by: Weitentaaal on July 21, 2021, 09:31:58 am

Title: ActiveControl
Post by: Weitentaaal on July 21, 2021, 09:31:58 am
Hello :)

Just a Short Question:

I have i field in wich i can move lets say an object with arrow Key's. But if i Press key Down then i noticed that my ComboBox still got the Focus and changed the Itemindex there to. How can i unfocus all Controls before doing some KeyDown Events ?

Thanks in advice :)
Title: Re: ActiveControl
Post by: Weitentaaal on July 21, 2021, 04:17:15 pm
could not follow you  :-[

can u explain this a bit better ?

"Use the key stroke events of the form instead and you test who has the focus at that time, too."
Title: Re: ActiveControl
Post by: lucamar on July 21, 2021, 05:59:06 pm
"Use the key stroke events of the form instead and you test who has the focus at that time, too."
Set the form's KeyPreview to True and use its OnKeyDown event. This will then be triggered before that of any other control.

Inside the handler for the later you can use:
Code: Pascal  [Select][+][-]
  1. if SomeControl.Focused then ....
to know if some control has the focus and act (or not) accordingly, if you need to.

That's basically it ;)
Title: Re: ActiveControl
Post by: Weitentaaal on July 22, 2021, 03:04:07 pm
Code: Pascal  [Select][+][-]
  1. procedure TStartUp.FormKeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   case Key of
  5.      VK_UP: if (ssShift in Shift) then MoveWholeUnit(dirBackward) else MoveWholeUnit(dirUp);
  6.      VK_DOWN: if (ssShift in Shift) then MoveWholeUnit(dirForward) else MoveWholeUnit(dirDown);
  7.      VK_LEFT: MoveWholeUnit(dirLeft);
  8.      VK_RIGHT: MoveWholeUnit(dirRight);
  9.   end;
  10. end;
  11.  

Wont work with this code ... any idea ? :)
Title: Re: ActiveControl
Post by: lucamar on July 22, 2021, 08:05:26 pm
"Won't work" is not quite a good description of the problem, don't you think? :)

Rather tell us what it does that it shouldn't or doesn't that should. Just in case, check (again?) whether you have both:

Also, set a breakpont at the start (or add a ShowMessage() or set some label's caption or a status bar panel's Text) to ascertain whether the handler is called.

Last, but not least, one thing lacking in your handler: after you respond to a key set Key to 0 to prevent its propagation to "downstream" handlers.
Title: Re: ActiveControl
Post by: Weitentaaal on July 23, 2021, 08:02:57 am
Changed my code to this:

Code: Pascal  [Select][+][-]
  1.  
  2.   //Focus von anderen Componenten nehmen
  3.  
  4.   KeyPreview:= True;
  5.  
  6.   case Key of
  7.      VK_UP: if (ssShift in Shift) then MoveWholeUnit(dirBackward) else MoveWholeUnit(dirUp);
  8.      VK_DOWN: if (ssShift in Shift) then MoveWholeUnit(dirForward) else MoveWholeUnit(dirDown);
  9.      VK_LEFT: MoveWholeUnit(dirLeft);
  10.      VK_RIGHT: MoveWholeUnit(dirRight);
  11.   end;
  12.   Key:= 0;
  13.  
  14.   KeyPreview:= False;
  15.  

Now it does the Exact opposite of what i wanted to have :D

Sorry for the Useless Description last time just thought i have the Code like u explained and it didn't work .. even with all settings.

EDIT: I obviously placed "KeyPreview:= True;" wrong ... where should i put it ? if i put it at start it wont allow me to Press any Key.

EDIT2: Deleted every keyPreview. But now i cant press anything (Key := 0 was the Problem). Still don't know how to use Keypreview

EDIT3: This did the Job :

Code: Pascal  [Select][+][-]
  1. procedure TStartUp.FormKeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.   //Focus von anderen Componenten nehmen
  5.   case Key of
  6.      VK_UP: if (ssShift in Shift) then MoveWholeUnit(dirBackward) else MoveWholeUnit(dirUp);
  7.      VK_DOWN: if (ssShift in Shift) then MoveWholeUnit(dirForward) else MoveWholeUnit(dirDown);
  8.      VK_LEFT: MoveWholeUnit(dirLeft);
  9.      VK_RIGHT: MoveWholeUnit(dirRight);
  10.   end;
  11.  
  12.   case Key of
  13.      VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT: Key:= 0;
  14.   end;
  15. end;
  16.  
  17.  

EDIT4: Ok  no it didn't. It Helped me to move Object without Problems but took my possibility to use Arrow Keys in generall

EDIT5: All i acheived were Some workarounds wich do NOT do what i wanted but get Close to it.

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TStartUp.FormKeyDown(Sender: TObject; var Key: Word;
  3.   Shift: TShiftState);
  4. begin
  5.    If CbDeviceType.Focused then begin
  6.      RemoveFocus(True);
  7.      Exit;
  8.    end;
  9.  
  10.   //Focus von anderen Componenten nehmen
  11.   case Key of
  12.      VK_UP: if (ssShift in Shift) then MoveWholeUnit(dirBackward) else MoveWholeUnit(dirUp);
  13.      VK_DOWN: if (ssShift in Shift) then MoveWholeUnit(dirForward) else MoveWholeUnit(dirDown);
  14.      VK_LEFT: MoveWholeUnit(dirLeft);
  15.      VK_RIGHT: MoveWholeUnit(dirRight);
  16.   end;
  17.  
  18.   //Dies soll davon abhalten dass nebenbei ComboBoxen ihren Wert ändern
  19.   case Key of
  20.      VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT: Key:= 0;
  21.   end;
  22. end;
  23.  
Title: Re: ActiveControl
Post by: dseligo on July 24, 2021, 12:27:40 am
EDIT: I obviously placed "KeyPreview:= True;" wrong ... where should i put it ? if i put it at start it wont allow me to Press any Key.

You need to set KeyPreview in Object Inspector for your form. I put screenshot in attachment.
TinyPortal © 2005-2018