There are lots of examples of how to use the Windows API to simulate pressing of keys. One option would be something like the code below.

However what i'm trying to achieve is one step further. The code below requires the app you want to send the keys, to be always the foreground window. I'm trying to sending keys to a window that is NOT in focus, so i can automate tasks / macros with this window minimized. 

Is there any alternative ?

Thanks

```
procedure PostKeyEx32(key: Word; const shift: TShiftState;
  specialkey: Boolean);
type
    TShiftKeyInfo = record
      shift: Byte ;
      vkey: Byte ;
    end;
    ByteSet = set of 0..7 ;
 const
    shiftkeys: array [1..3] of TShiftKeyInfo =
      ((shift: Ord(ssCtrl) ; vkey: VK_CONTROL),
      (shift: Ord(ssShift) ; vkey: VK_SHIFT),
      (shift: Ord(ssAlt) ; vkey: VK_MENU)) ;
 var
    flag: DWORD;
    bShift: ByteSet absolute shift;
    j: Integer;
 begin
    for j := 1 to 3 do
    begin
      if shiftkeys[j].shift in bShift then
        keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0) ;
    end;
    if specialkey then
      flag := KEYEVENTF_EXTENDEDKEY
    else
      flag := 0;

    if key = 47 then
      begin
      keybd_event(VkKeyScan('/'), 1, 0, 0);
      end
      else
      begin
      keybd_event(key, MapvirtualKey(key, 0), flag, 0) ;
      flag := flag or KEYEVENTF_KEYUP;
      keybd_event(key, MapvirtualKey(key, 0), flag, 0) ;
      end;

    for j := 3 downto 1 do
    begin
      if shiftkeys[j].shift in bShift then
        keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), KEYEVENTF_KEYUP, 0) ;
    end;
 end;

```