Détecter l'appui sur les touches de direction

cs_tsiky Messages postés 10 Date d'inscription mercredi 5 avril 2006 Statut Membre Dernière intervention 24 juillet 2006 - 14 avril 2006 à 11:06
cs_Delphiprog Messages postés 4297 Date d'inscription samedi 19 janvier 2002 Statut Membre Dernière intervention 9 janvier 2013 - 19 avril 2006 à 12:26
Bonjour à tous,

comment peut-on détecter l'appui sur les touches F1 à F10?
j'ai trouvé une solution sur http://pascal.developpez.com/faq/?page=Pg_FAQ_Kbd#TouchesDirection mais delphi ne reconnait ni ctr ni readkey.

11 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
14 avril 2006 à 13:22
autre solution qui consiste a utiliser un timer regler a 25ms

et mettre :

begin
if GetAsyncKeyState(VK_LEFT) < 0 then ...
if GetAsyncKeyState(VK_RIGHT)< 0 then ...
if GetAsyncKeyState(VK_UP) < 0 then ...
if GetAsyncKeyState(VK_DOWN) < 0 then ...


if GetAsyncKeyState(VK_F1) < 0 then ...

if GetAsyncKeyState(VK_F2) < 0 then ...

if GetAsyncKeyState(VK_F3) < 0 then ...

if GetAsyncKeyState(VK_F4) < 0 then ...

if GetAsyncKeyState(VK_F5) < 0 then ...

if GetAsyncKeyState(VK_F6) < 0 then ...

if GetAsyncKeyState(VK_F7) < 0 then ...

if GetAsyncKeyState(VK_F8) < 0 then ...

if GetAsyncKeyState(VK_F9) < 0 then ...

if GetAsyncKeyState(VK_F10) < 0 then ...

if GetAsyncKeyState(VK_F11) < 0 then ...
if GetAsyncKeyState(VK_F12) < 0 then ...

end;

ou encore :

function KeyPressed : Integer;
var
i : Integer;
buffer : TKeyboardState;
begin
Result := -1;
if GetKeyboardState(buffer) then begin
for i := 0 to High(buffer) do begin
if (buffer[i] and $80) <> 0 then begin
Result := i;
Exit;
end;
end;
end;
end;

et comme je suis sympa je te donne ma fonction magique :

type
TByteSet = Set of byte;

function KeyboardState : TByteSet;
var K : byte;
begin
result := [];

// caps lock
if GetKeyState(144) = 1 then result := result + [144];
// scroll lock
if GetKeyState(145) = 1 then result := result + [145];
// Num lock
if GetKeyState(20) = 1 then result := result + [20];
// pause
if GetKeyState(19) = 1 then result := result + [19];

for K := 0 to 255 do
if windows.GetAsyncKeyState(K) < 0 then begin
result := result + [K];
case K of
// correction of Ctrl, Alt, Shift
17,16,18 : result := result - [K];
// correction of AltGr
165 : result := result - [162];
end;
end;
end;

utilisation :

procedure TForm1.Timer1Timer(Sender: TObject);
var N : integer;
begin
Label1.Caption := '';
for N := 0 to 255 do
if N in KeyboardState then begin
label1.Caption := Label1.Caption + inttostr(N) +' ';
end;
end;

grace a celle ci, tu peu connaitre l'etat de n'importe qu'elle touche et surtout les combinaisons de touche en cours (CTRL+ALT, CTRL Gauche, CTRL Droit, VerrNum, CapsLock ect...)
beaucoup plus pratique que la lecture d'une variable ne donnant que la derniere touche appuyée et encore...

il suffit de tester la presence des touches dans KeyboardState et hop ... on as le resultat.

si on doit tester plusieurs touche, afin d'eviter plusieurs appel a KeyboardState il suffirat de stocker le resultat dans une variable TByteSet :

var BS : TByteSet;
begin
BS := KeyboardState;
if VK_LEFT in BS then ... {fleche gauche}
if VK_ESCAPE in BS then ... {touche echape}
if [VK_LCTRL,VK_LMENU,82] <= BS then ... {combinaison Ctrl+Alt+R}
end;
1