Solved

Sending keypress to a program

Posted on 1998-08-29
1
745 Views
Last Modified: 2010-04-04
Is there a way to make a program send a keypress to another program, eg: If I start a program where the... lets say 'C' key starts playing sound, can I make a program that will make that program think that I pressed the 'C' key ?
If this is possible, then how... I don't mind messing with Win32 API calls (that's probably also the only way)
0
Comment
Question by:carceri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1338070
Hi carceri,

 sure it's possible, you need the handle of that window-control!

here is an example (not made by me) :

Sending Keystrokes/Text to a Window...

From: "David Zajac" <dzajac@HiWAAY.net> Hope this helps:
--------------------------------------------------------------------------------
 
unit Unit1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    AppInst: THandle;
    AppWind: THandle;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses ShellAPI;
 

procedure SendShift(H: HWnd; Down: Boolean);
var vKey, ScanCode, wParam: Word;
    lParam: longint;
begin
  vKey:= $10;
  ScanCode:= MapVirtualKey(vKey, 0);
  wParam:= vKey or ScanCode shl 8;
  lParam:= longint(ScanCode) shl 16 or 1;
  if not(Down) then lParam:= lParam or $C0000000;
  SendMessage(H, WM_KEYDOWN, vKey, lParam);
end;

procedure SendCtrl(H: HWnd; Down: Boolean);
var vKey, ScanCode, wParam: Word;
    lParam: longint;
begin
  vKey:= $11;
  ScanCode:= MapVirtualKey(vKey, 0);
  wParam:= vKey or ScanCode shl 8;
  lParam:= longint(ScanCode) shl 16 or 1;
  if not(Down) then lParam:= lParam or $C0000000;
  SendMessage(H, WM_KEYDOWN, vKey, lParam);
end;

procedure SendKey(H: Hwnd; Key: char);
var vKey, ScanCode, wParam: Word;
    lParam, ConvKey: longint;
    Shift, Ctrl: boolean;
begin
  ConvKey:= OemKeyScan(ord(Key));
  Shift:= (ConvKey and $00020000) <> 0;
  Ctrl:= (ConvKey and $00040000) <> 0;
  ScanCode:= ConvKey and $000000FF or $FF00;
  vKey:= ord(Key);
  wParam:= vKey;
  lParam:= longint(ScanCode) shl 16 or 1;
  if Shift then SendShift(H, true);
  if Ctrl then SendCtrl(H, true);
  SendMessage(H, WM_KEYDOWN, vKey, lParam);
  SendMessage(H, WM_CHAR, vKey, lParam);
  lParam:= lParam or $C0000000;
  SendMessage(H, WM_KEYUP, vKey, lParam);
  if Shift then SendShift(H, false);
  if Ctrl then SendCtrl(H, false);
end;

function EnumFunc(Handle: HWnd; TF: TForm1): Bool; Far;
begin
  TF.AppWind:= 0;
  if GetWindowWord(Handle, GWW_HINSTANCE) = TF.AppInst then
    TF.AppWind:= Handle;
  result:= (TF.AppWind = 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var Text: Array[0..255] of char;
begin
  AppInst:= ShellExecute(Handle, 'open', 'notepad.exe', nil, '', SW_NORMAL);
  EnumWindows(@EnumFunc, longint(self));
  AppWind:= GetWindow(AppWind, GW_CHILD);
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  SendKey(AppWind, 'T');
  SendKey(AppWind, 'e');
  SendKey(AppWind, 's');
  SendKey(AppWind, 't');
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if AppWind <> 0 then SendKey(AppWind, Key);
end;

end.

PS. To find the window handle of a specific windon, you can use the windows API's

HWND FindWindow(

    LPCTSTR  lpClassName,      // address of class name
    LPCTSTR  lpWindowName       // address of window name
   );

or

HWND FindWindowEx(

    HWND  hwndParent,      // handle of parent window
    HWND  hwndChildAfter,      // handle of a child window
    LPCTSTR  lpszClass,      // address of class name
    LPCTSTR  lpszWindow      // address of window name
   );

Regards, Zif.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month3 days, 13 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question