Envoi de messages windows de pression de touche [ DEPLHI ] (source) - Programmation
Marsh Posté le 03-04-2002 à 14:51:55
à mon avis le problème n'est pas dans le code que tu as posté, car il fonctionne, mais plutot dans la manière dont tu l'utilises, il serait plus utile que tu donnes un exemple et que tu précises les symptômes
Marsh Posté le 03-04-2002 à 16:16:29
Taureau a écrit a écrit : à mon avis le problème n'est pas dans le code que tu as posté, car il fonctionne, mais plutot dans la manière dont tu l'utilises, il serait plus utile que tu donnes un exemple et que tu précises les symptômes |
Hehe, justement, je n'arrive pas a men servir
si tu pouvais m'expliquer en 2 ou 3 ligne..
Merci
Marsh Posté le 03-04-2002 à 00:13:02
Salut j'ai beau chercher a faire fonctionner cette function (SendKeysTo), je n'ai pas trouvé le probleme... si vous pouviez m'aider...
Voici la source :
//debut de code
unit Winutils;
interface
uses WinTypes;
function SendKeys(const AKeys: string; Wait: boolean): boolean;
function SendKeysTo(Handle: Hwnd; const AKeys: string; Wait: boolean): boolean;
implementation
uses Messages, SysUtils, WinProcs{,
KeyCodes, StrUtil};
procedure HandleMessages;
var
Msg: TMsg;
begin
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
function SendAMessage(Wnd: HWnd; Msg, wParam: Word; lParam: LongInt): LongInt;
begin
SendMessage(Wnd, Msg, wParam, lParam);
HandleMessages;
end;
function SendKeys(const AKeys: string; Wait: boolean): boolean;
var
h : Hwnd;
begin
h := GetFocus;
SendKeys := SendKeysTo(h, AKeys, Wait);
end; { SendKeys }
function SendKeysTo(Handle: Hwnd; const AKeys: string; Wait: boolean): boolean;
function AHandle: Hwnd;
var
h: Hwnd;
p: PChar;
begin
p := StrAlloc(32);
h := GetFocus;
{ GetClassName(h, p, 31);
if StrComp(p, 'Button' = 0 then
AHandle := GetParent(h)
else}
AHandle := h;
StrDispose(p);
end;
var
Char : boolean;
CtrlDown : boolean;
k : integer;
Keys : string;
p : integer;
s : string;
ShiftDown : boolean;
SysKey : boolean;
begin
CtrlDown := false;
SysKey := false;
ShiftDown := false;
Keys := AKeys;
while length(Keys) > 0 do
begin
if Keys[1] = '%' then
begin
SysKey := true;
if Wait then
SendAMessage(AHandle, WM_SysKeyDown, VK_Menu, $20000001)
else
PostMessage(AHandle, WM_SysKeyDown, VK_Menu, $20000001);
Keys := copy(Keys, 2, length(Keys));
end
else if Keys[1] = '+' then
begin
ShiftDown := true;
if Wait then
SendAMessage(AHandle, WM_KeyDown, VK_Shift, $00000001)
else
PostMessage(AHandle, WM_KeyDown, VK_Shift, $00000001);
Keys := copy(Keys, 2, length(Keys));
end
else if Keys[1] = '^' then
begin
CtrlDown := true;
if Wait then
SendAMessage(AHandle, WM_KeyDown, VK_Control, $00000001)
else
PostMessage(AHandle, WM_KeyDown, VK_Control, $00000001);
Keys := copy(Keys, 2, length(Keys));
end;
if Keys[1] = '{' then
begin
p := pos('}', Keys);
s := lowercase(copy(Keys, 2, p-2));
Keys := copy(Keys, p+1, length(Keys));
Char := true;
if s = 'tab' then
k := VK_Tab
else if s = 'enter' then
k := VK_Return
else
begin
Char := false;
if s = 'down' then
k := VK_Down
else if s = 'up' then
k := VK_Down
else if s = 'right' then
k := VK_Right
else if s = 'left' then
k := VK_Left
else if s = 'capslock' then
k := VK_Capital
else
begin
Char := false;
if s = 'f1' then
k := VK_F1
else if s = 'f2' then
k := VK_F2
else if s = 'f3' then
k := VK_F3
else if s = 'f4' then
k := VK_F4
else if s = 'f5' then
k := VK_F5
else if s = 'f6' then
k := VK_F6
else if s = 'f7' then
k := VK_F7
else if s = 'f8' then
k := VK_F8
else if s = 'f9' then
k := VK_F9
else if s = 'f10' then
k := VK_F10
else if s = 'f11' then
k := VK_F11
else if s = 'f12' then
k := VK_F12
else if s = 'f13' then
k := VK_F13
else if s = 'f14' then
k := VK_F14
else if s = 'f15' then
k := VK_F15
else if s = 'f16' then
k := VK_F16;
end;
end;
if SysKey then
begin
if Wait then
begin
SendAMessage(AHandle, WM_SysKeyDown, k, $20000001);
SendAMessage(AHandle, WM_SysKeyUp, k, $e0000001);
end
else
begin
PostMessage(AHandle, WM_SysKeyDown, k, $20000001);
PostMessage(AHandle, WM_SysKeyUp, k, $e0000001);
end;
end
else
begin
if Wait then
begin
SendAMessage(AHandle, WM_KeyDown, k, $08000001);
if Char then
SendAMessage(AHandle, WM_Char, k, $08000001);
SendAMessage(AHandle, WM_KeyUp, k, $c8000001);
end
else
begin
PostMessage(AHandle, WM_KeyDown, k, $08000001);
if Char then
PostMessage(AHandle, WM_Char, k, $08000001);
PostMessage(AHandle, WM_KeyUp, k, $c8000001);
end;
end;
end
else
begin { Ordinary characters }
if Wait then
begin
SendMessage(AHandle, WM_KeyDown, ord(UpCase(Keys[1])), $08000001);
if ShiftDown then
SendMessage(AHandle, WM_Char, ord(UpCase(Keys[1])), $08000001)
else
SendMessage(AHandle, WM_Char, ord(Keys[1]), $08000001);
SendAMessage(AHandle, WM_KeyUp, ord(UpCase(Keys[1])), $c8000001);
end
else
begin
PostMessage(AHandle, WM_KeyDown, ord(UpCase(Keys[1])), 1);
if ShiftDown then
PostMessage(AHandle, WM_Char, ord(UpCase(Keys[1])), 1)
else
PostMessage(AHandle, WM_Char, ord(Keys[1]), 1);
PostMessage(AHandle, WM_KeyUp, ord(UpCase(Keys[1])), $c0000001);
end;
Keys := copy(Keys, 2, length(Keys));
end;
if SysKey then
begin
if Wait then
SendAMessage(AHandle, WM_KeyUp, VK_Menu, $c0000001)
else
PostMessage(AHandle, WM_KeyUp, VK_Menu, $c0000001);
SysKey := false;
end;
if ShiftDown then
begin
if Wait then
SendAMessage(AHandle, WM_KeyUp, VK_Shift, $c0000001)
else
PostMessage(AHandle, WM_KeyUp, VK_Shift, $c0000001);
ShiftDown := false;
end;
if CtrlDown then
begin
if Wait then
SendAMessage(AHandle, WM_KeyUp, VK_Control, $c0000001)
else
PostMessage(AHandle, WM_KeyUp, VK_Control, $c0000001);
CtrlDown := false;
end;
end; { while }
SendKeysTo := true;
end; { SendKeysTo }
end.
//fin de code