Recent

Author Topic: ActiveControl  (Read 5718 times)

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
ActiveControl
« 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 :)
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4917
Re: ActiveControl
« Reply #1 on: July 21, 2021, 03:24:32 pm »
The form it self has a keypreview property where by setting this to true will allow you to view keystrokes before going to controls that live on the form.

 Use the key stroke events of the form instead and you test who has the focus at that time, too.
The only true wisdom is knowing you know nothing

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: ActiveControl
« Reply #2 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."
« Last Edit: July 21, 2021, 04:19:05 pm by Weitentaaal »
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: ActiveControl
« Reply #3 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 ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: ActiveControl
« Reply #4 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 ? :)
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: ActiveControl
« Reply #5 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:
  • Set the form's KeyPreview to True; and
  • The OnKeyDown property is correctly set to point to the handler

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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 298
Re: ActiveControl
« Reply #6 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.  
« Last Edit: July 23, 2021, 09:04:31 am by Weitentaaal »
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

dseligo

  • Sr. Member
  • ****
  • Posts: 360
Re: ActiveControl
« Reply #7 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