Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Keypress Problem bei Setfocus (https://www.delphipraxis.net/128023-keypress-problem-bei-setfocus.html)

thomas2009 22. Jan 2009 23:54


Keypress Problem bei Setfocus
 
Hallo


Delphi-Quellcode:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

if(Key =39)then
    begin
      ShowMessage('Pfeil rechts');
    end;
Es funktioniert nur wenn der Focus in einem Edit !
Wenn ein BitBtn Button zum beispiel den Focus hat, dann funktioniert nicht

turboPASCAL 23. Jan 2009 03:07

Re: Keypress Problem bei Setfocus
 
Im OI für die Form KeyPreview auf True setzen.

Uwe Raabe 23. Jan 2009 07:04

Re: Keypress Problem bei Setfocus
 
Zitat:

Zitat von turboPASCAL
Im OI für die Form KeyPreview auf True setzen.

Ich weiß nicht, wie oft ich ihm das schon gesagt habe...

Hawkeye219 23. Jan 2009 08:20

Re: Keypress Problem bei Setfocus
 
Hallo,

zum sicheren Abfangen der Pfeiltasten könnte man das Ereignis TForm.OnShortCut verwenden:

Delphi-Quellcode:
procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
var
  Key : TShortCut;
begin
  Key := {Menus.}ShortCut(Msg.CharCode, KeyDataToShiftState(Msg.KeyData));

  if (Key = VK_RIGHT) then
    begin
      ShowMessage ('Gotcha!');
      Handled := True;
    end
Gruß Hawkeye

thomas2009 23. Jan 2009 21:49

Re: Keypress Problem bei Setfocus
 
Zitat:

Zitat von turboPASCAL
Im OI für die Form KeyPreview auf True setzen.

Natürlich ist KeyPreview auf true
Teste doch

______
Zitat:

Zitat von Hawkeye219
Hallo,

zum sicheren Abfangen der Pfeiltasten könnte man das Ereignis TForm.OnShortCut verwenden:

Delphi-Quellcode:
procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
var
  Key : TShortCut;
begin
  Key := {Menus.}ShortCut(Msg.CharCode, KeyDataToShiftState(Msg.KeyData));

  if (Key = VK_RIGHT) then
    begin
      ShowMessage ('Gotcha!');
      Handled := True;
    end

Dein Code funktioniert viel besser und unabhängig von dem Fokus

Edit :
Es gibt aber ein problem mit der Tasten kombination zum beispiel :
Delphi-Quellcode:
  if ((Key = VK_CONTROL) and (Key = VK_RIGHT) ) then
    begin
      ShowMessage ('CTRL + Pfeil'); // Reagiert nicht !
      Handled := True;
    end
Danke :thumb:

Hawkeye219 12. Feb 2009 18:25

Re: Keypress Problem bei Setfocus
 
Hallo Thomas,

ich sehe deine Ergänzung leider erst jetzt. Natürlich kannst du auch Kombinationen mit den Umschalttasten abfragen:

Delphi-Quellcode:
procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
var
  Key : TShortCut;
begin
  Key := {Menus.}ShortCut(Msg.CharCode, KeyDataToShiftState(Msg.KeyData));

  if (Key = scShift or VK_RIGHT) then
    begin
      ShowMessage ('Gotcha!');
      Handled := True;
    end;
end;
Gruß Hawkeye

Blackheart 12. Feb 2009 18:37

Re: Keypress Problem bei Setfocus
 
Key Taste die zwölfte (oder waren es schon mehr) von thomas2009 ? :wink:

thomas2009 18. Feb 2009 13:26

Re: Keypress Problem bei Setfocus
 
Ich verstehe nicht, warum mit "or" funktioniert aber mit "and" nicht !
Delphi-Quellcode:
...
if (Key = scShift or VK_RIGHT) then

SirThornberry 18. Feb 2009 13:36

Re: Keypress Problem bei Setfocus
 
die Pfeiltasten müssen über die entsprechende Message erlaubt werden wm_getdlgkey oder wo ähnlich.

Christian Seehase 18. Feb 2009 13:43

Re: Keypress Problem bei Setfocus
 
Moin Thomas,

in dem Beispiel sieht es so aus:
Code:
scShift = 8192 = 10000000000000 (bin)
VK_RIGHT =  39 =        100111 (bin)
Wenn jetzt diese Tasten gleichzeitig gedrückt werden, ist
Code:
Key = 8231 = 10000000100111 (bin)
da jetzt
Code:
scShift and VK_RIGHT = 0
ist (bei logischer Und-Verknüpfung werden alle Bits 1, die bei beiden Werten 1 sind, ansonsten wird's 0)
und
Code:
scShift or VK_RIGHT = 10000000100111
ist (bei logischer Oder-Verknüpfung werden alle Bits 0, die bei beiden Werten 0 sind, ansonsten wird's 1),
ist die Gleichheit nur bei OR gegeben.


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:07 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz