![]() |
Steuerungstasten "präzise" abfangen
Seas,
zur Erklärung des Titels: Ich möchte auf das Drücken von Steuerungstasten (Ctrl,Alt,Shift) reagieren. Mit "präzise" meine ich dass mein Programm unterscheiden muss (!) zwischen LCtrl und RCtrl,LAlt und AltGR, LShift und RShift. Der Zweck? In einem Memo soll ein "Script" erstellt werden, dafür reagiere ich auf OnKeyDown und OnKeyUp. Mit allen anderen Tasten klappt alles auch schon wunderbar, aber irgendwie verstehe ich nicht inwiefern ich diese Tasten anderweitig analysieren muss. Ich hab mir mal einen Quelltext aus dem Internet gezogen und diesen modifiziert. Diese Funktion sollte eigentlich je nach Result werd zeigen welche Taste gedrückt wurde bzw. jetzt nicht mehr (!) gedrückt ist.
Delphi-Quellcode:
Hoffe ihr könnt damit etwas anfangen, weil ich dieses Thema null durchblicke (mit LCtrl und RCtrl).
function SetKeysGetChange:shortint;
var b1,b2,b3:boolean; begin result:=0; b1:= GetKeyState(16) and (-32768+128) <> 0; b2:= GetKeyState(17) and (-32768+128) <> 0; b3:= GetKeyState(18) and (-32768+128) <> 0; if LShift <> (b1 and (GetKeyState(160) <> 0)) then result:= C_LShift; LShift := result = C_LShift; if RShift <> (b1 and (GetKeyState(161)<> 0)) then result:=C_RShift; RShift := result = C_RShift; if LCtrl <> (b2 and (GetKeyState(162)<> 0)) then result:=C_LCtrl; LCtrl := b2 and (GetKeyState(162)<> 0); if RCtrl <> (b2 and (GetKeyState(163)<> 0)) then result := C_RCtrl; RCtrl := result = C_RCtrl; if LAlt <> (b3 and (GetKeyState(164)<> 0)) then result := C_LAlt; LAlt:= result = C_LAlt; if b3 and b2 and (GetKeyState(165)<> 0) then begin RAlt:=true; if RAlt <> LAlt then result:=C_RAlt; end else RAlt:=false; end; MfG |
Re: Steuerungstasten "präzise" abfangen
Hallo Coder,
zu deinem Problem kann ich erstmal nichts genaueres sagen, aber folgendes macht mich stutzig: Zitat:
Ansonsten sind die Konstanten für alle drei b-Werte gleich. Soll das so sein? MfG xZise |
Re: Steuerungstasten "präzise" abfangen
Zitat:
also kommt immer dieses raus (mit oder ohne Klammer ist egal)
Delphi-Quellcode:
b1 ist true, wenn eines der Bits gesetzt ist ... ich weiß jetzt nur grad nicht wofür diese Bits stehen.
b1 := GetKeyState(16) and $8080 <> 0;
b1 := (GetKeyState(16) and $8080) <> 0; |
Re: Steuerungstasten "präzise" abfangen
Ich kann leider auch nix genaueres zu der Funktionsweiße sagen, nur dass ich meine Quellenprocedure so umgeschrieben hab, dass sie mir erstma die Stati der Control Tasten speichert und dann auf Änderungen vergleicht und diese ausgibt.
![]()
Delphi-Quellcode:
MfG
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var b1, b2, b3: integer; Taststring: string; begin if GetKeyState(16) and (-32768+128) <> 0 then b1 := 1 else b1 := 0; if GetKeyState(17) and (-32768+128) <> 0 then b2 := 1 else b2 := 0; if GetKeyState(18) and (-32768+128) <> 0 then b3 := 1 else b3 := 0; Taststring := ''; if (b1 = 1) and (GetKeyState(160) and (-32768+128) <> 0) then Taststring := 'linke Shift'; if (b1 = 1) and (GetKeyState(161) and (-32768+128) <> 0) then Taststring := 'rechte Shift'; if (b2 = 1) and (GetKeyState(162) and (-32768+128) <> 0) thenTaststring := 'linke STRG'; if (b2 = 1) and (GetKeyState(163) and (-32768+128) <> 0) then Taststring := 'rechte STRG'; if (b3 = 1) and (GetKeyState(164) and (-32768+128) <> 0) then Taststring := 'linke Alt'; if (b3 = 1) and (b2 = 1) and (GetKeyState(165) and (-32768+128) <> 0) then if Taststring = 'linke STRG' then Taststring := 'rechte AltGr'; if (GetKeyState(20) and (-32768+128) <> 0) then Taststring := 'linke Feststell-Shift'; Label1.Caption := Taststring; end; |
Re: Steuerungstasten "präzise" abfangen
*push*
|
Re: Steuerungstasten "präzise" abfangen
Mit diesen Zeilen sollte am Ende in S eine textuelle Darstellung der gedrückten Tasten stehen.
Besonderheit: <Alt Gr> schaltet automatisch <LControl> mit ein!
Delphi-Quellcode:
S := '';
if GetKeyState(VK_LSHIFT) < 0 then S := S + ' LShift'; if GetKeyState(VK_RSHIFT) < 0 then S := S + ' RShift'; if GetKeyState(VK_LCONTROL) < 0 then S := S + ' LControl'; if GetKeyState(VK_RCONTROL) < 0 then S := S + ' RControl'; if GetKeyState(VK_LMENU) < 0 then S := S + ' Alt'; if GetKeyState(VK_RMENU) < 0 then S := S + ' Alt Gr'; |
Re: Steuerungstasten "präzise" abfangen
Was ist denn dabei der Fehler? Ich habe o.g. Code bei mir mal in Delphi eingespielt (Delphi 7 Enterprise) und bei mir klappt alles wunderbar. Wird auch eindeutig und richtig genannt, welche Shift/Str/Alt-Taste ich drücke.
|
Re: Steuerungstasten "präzise" abfangen
@Uwe Raabe
Ty dein Post war mir eine große Hilfe, jetzt scheint es zu funktionieren!
Delphi-Quellcode:
@quendolineDD
const
C_LShift = 1; C_RShift = 2; C_LCtrl = 3; C_RCtrl = 4; C_LAlt = 5; C_RAlt = 6; var LAlt,RAlt,LCtrl,RCtrl,LShift,RShift:boolean; function SetKeysGetChange:shortint; implementation uses Windows; function SetKeysGetChange:shortint; begin result:=0; if (GetKeyState(VK_LSHIFT) < 0) <> LShift then result := C_LShift; LShift := GetKeyState(VK_LSHIFT) < 0; if (GetKeyState(VK_RSHIFT) < 0) <> RShift then result := C_RShift; RShift := GetKeyState(VK_RSHIFT) < 0; if (GetKeyState(VK_LCONTROL) < 0) <> LCtrl then result := C_LCtrl; LCtrl := GetKeyState(VK_LCONTROL) < 0; if (GetKeyState(VK_RCONTROL) < 0) <> RCtrl then result := C_RCtrl; RCtrl := GetKeyState(VK_RCONTROL) < 0; if (GetKeyState(VK_LMENU) < 0) <> LAlt then result := C_LAlt; LAlt := GetKeyState(VK_LMENU) < 0; if (GetKeyState(VK_RMENU) < 0) <> RAlt then result := C_RAlt; RAlt := GetKeyState(VK_RMENU) < 0; end; Danke für das Lesen der Problemstellung! (nicht) Es geht hier darum Änderungen festzustellen und nicht einfach alles in einem Label auszugeben.. MfG |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:54 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