Mandar teclas a mi app !

Hola:
Necesito saber como puedo mandar teclas a una aplicacion, se que se puede hacer teniendo en cuenta el titulo de la aplicacion y como parametros las teclas que le vas a mandar, lo que quiero hacer es, por ejemplo:
Mandarle al mediaplayer, Ctrol + Alt + C, o otras teclas y combinaciones de teclas.
Gracias de antemano.

1 respuesta

Respuesta
1
Lo que dices es asi, pero no tanmto, puedes enviar eventos de tecla a una aplicacion, lo que si no es segura que esa aplicacion lo procese. Te mando aqui el codigo fuente para que lo pruebes tu mismo. Funciona aleatoriamente. Si quieres puedes comentar la linea que da foco al MediaPlayer.
Ah, y una instancia del Media Player tiene que estar activa para que funcione.
procedure SendKey(aHandle:HWND;key:Word;ShiftState:TShiftState);
type
TBuffers = array [0..1] of TKeyboardState;
var
pKeyBuffers: ^TBuffers;
lParam: LongInt;
ProcessId,LocalProcessId:Cardinal;
begin
(* check if the target window exists *)
if IsWindow(aHandle) then
begin
(* set local variables to default values *)
pKeyBuffers := nil;
lParam := MakeLong(0, MapVirtualKey(key, 0));
(* allocate space for the key state buffers *)
New(pKeyBuffers);
try
(* Fill buffer 1 with current state so we can later restore it.
Null out buffer 0 to get a "no key pressed" state. *)
ProcessId := GetWindowThreadProcessId(aHandle,Nil);
LocalProcessId := GetCurrentThreadId();
ShowWindow(aHandle,SW_NORMAL);
UpdateWindow(aHandle);
if ProcessId <> LocalProcessId then
begin
AttachThreadInput(ProcessId ,LocalProcessId,true);
try
GetKeyboardState(pKeyBuffers^[1]);
FillChar(pKeyBuffers^[0], SizeOf(TKeyboardState), 0);
(* set the requested modifier keys to "down" state in the buffer*)
if ssShift in ShiftState then
pKeyBuffers^[0][VK_SHIFT] := $80;
if ssAlt in ShiftState then
begin
(* Alt needs special treatment since a bit in lparam needs also be set *)
pKeyBuffers^[0][VK_MENU] := $80;
lParam := lParam or $20000000;
end;
if ssCtrl in ShiftState then
pKeyBuffers^[0][VK_CONTROL] := $80;
if ssLeft in ShiftState then
pKeyBuffers^[0][VK_LBUTTON] := $80;
if ssRight in ShiftState then
pKeyBuffers^[0][VK_RBUTTON] := $80;
if ssMiddle in ShiftState then
pKeyBuffers^[0][VK_MBUTTON] := $80;
(* make out new key state array the active key state map *)
SetKeyboardState(pKeyBuffers^[0]);
(* post the key messages *)
if ssAlt in ShiftState then
begin
PostMessage(aHandle, WM_SYSKEYDOWN, key, lParam);
PostMessage(aHandle, WM_SYSKEYUP, key, lParam or $C00);
end
else
begin
PostMessage(aHandle, WM_KEYDOWN, key, lParam);
PostMessage(aHandle, WM_KEYUP, key, lParam or $C00);
end;
(* process the messages *)
Application.ProcessMessages;
(* restore the old key state map *)
SetKeyboardState(pKeyBuffers^[1]);
finally
AttachThreadInput(ProcessId ,LocalProcessId,false);
end;
end;
finally
(* free the memory for the key state buffers *)
if pKeyBuffers <> nil then
Dispose(pKeyBuffers);
end; { If }
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var handle:HWND;
begin
handle := FindWindow('WMPlayerApp',nil);
if handle <> 0 then
SendKey(handle,Ord('A'),[ssCtrl]);
end;
Si tienes la version en ingles reemplaza Ord('A') por Ord('O') y haz click muchas veces en el boton
Finaliza la consulta

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas