unit uWinKeySend;
{
This code is part of the TiCodEd Application, designed to develop for the
TI-99/4a Homecomputer in a modern, structured way.

BSD License
Copyright (c) 2026, Stefan Bauch, TiCodEd <at> lizardware <dot> de


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

VAR WinKeySendDelay : Word;

function GetWindowHandle(idx:Integer;par:String): THandle;
Function SendKeys(H:Thandle;s:String):Boolean;
function SendString(H:Thandle;s:String):Boolean;


implementation

{$IFDEF WINDOWS}

uses windows,clipbrd;

const maxvk=48;
  vk : Array[1..maxvk] of record
                            tkn : String[8];
                            val : Word;
                           end
    = (
    ( tkn: ':OEM102.' ; val: 226),
    ( tkn: ':OEM1.' ; val: 186),
    ( tkn: ':OEM2.' ; val: 191),
    ( tkn: ':OEM3.' ; val: 192),
    ( tkn: ':OEM4.' ; val: 219),
    ( tkn: ':OEM5.' ; val: 220),
    ( tkn: ':OEM6.' ; val: 221),
    ( tkn: ':OEM7.' ; val: 222),
    ( tkn: ':OEM8.' ; val: 223),
    ( tkn: ':TAB.' ; val: 9),
    ( tkn: ':RET.' ; val: 13),
    ( tkn: ':ESC.' ; val: 27),
    ( tkn: ':WAIT' ; val: 246),
    ( tkn: ':F10.' ; val: 121),
    ( tkn: ':F11.' ; val: 122),
    ( tkn: ':F12.' ; val: 123),
    ( tkn: ':F13.' ; val: 124),
    ( tkn: ':F14.' ; val: 125),
    ( tkn: ':F15.' ; val: 126),
    ( tkn: ':F16.' ; val: 127),
    ( tkn: ':F17.' ; val: 128),
    ( tkn: ':F18.' ; val: 129),
    ( tkn: ':F19.' ; val: 130),
    ( tkn: ':F20.' ; val: 131),
    ( tkn: ':F21.' ; val: 132),
    ( tkn: ':F22.' ; val: 133),
    ( tkn: ':F23.' ; val: 134),
    ( tkn: ':F24.' ; val: 135),
    ( tkn: ':F1.' ; val: 112),
    ( tkn: ':F2.' ; val: 113),
    ( tkn: ':F3.' ; val: 114),
    ( tkn: ':F4.' ; val: 115),
    ( tkn: ':F5.' ; val: 116),
    ( tkn: ':F6.' ; val: 117),
    ( tkn: ':F7.' ; val: 118),
    ( tkn: ':F8.' ; val: 119),
    ( tkn: ':F9.' ; val: 120),
    ( tkn: ':S+' ; val: 16),
    ( tkn: ':C+' ; val: 17),
    ( tkn: ':A+' ; val: 18),
    ( tkn: ':S-' ; val: 216),
    ( tkn: ':C-' ; val: 217),
    ( tkn: ':A-' ; val: 218),
    ( tkn: ';' ; val: 186),
    ( tkn: '+' ; val: 187),
    ( tkn: ',' ; val: 188),
    ( tkn: '-' ; val: 189),
    ( tkn: '.' ; val: 190));



function GetWindowHandle(idx:Integer;par:String): THandle;
begin
  CASE idx of
    0 : Result := 0;
    1 : Result := FindWindow('TIWndClass',NIL);
    2 : Result := FindWindow(PChar(par),NIL);
    3 : Result := FindWindow(NIL,PChar(par))
  end;
end;

function SendKeys(H: Thandle; s: String): Boolean;
var i : integer;
    c:Int64;
begin
  s := UpperCase(trim(s));
  for i:=1 to maxvk do s:=StringReplace(s,vk[i].tkn,chr(vk[i].val),[rfReplaceAll]);

  SetForegroundWindow(h);
  Sleep(max(WinKeySendDelay * 20,400));

  i:=1;
  while i<=length(s) do begin
    c := int64(s[i]);
    case c of
      16..18     : keybd_event(C, 0, KEYEVENTF_EXTENDEDKEY, 0);// PostMessage(h,WM_KEYDOWN,C,0);
      216 .. 218 : keybd_event(C-200, 0, KEYEVENTF_EXTENDEDKEY+KEYEVENTF_KEYUP, 0); // PostMessage(h,WM_KEYUP,C-200,0);
      246 : begin Sleep(StrToInt(copy(s,i+1,1))*1000); inc(i,2); end;
      else begin
        keybd_event(C,0,0,0); // key is down
        Sleep(WinKeySendDelay);
        keybd_event(C,0,KEYEVENTF_KEYUP,0); // key is up
      end;
    end;
    Sleep(WinKeySendDelay);
    inc(i);
  end;
  Result := true;
end;

function SendString(H:Thandle;s:String):Boolean;

begin
  Clipboard.AsText :=s;
  Result := SendKeys(H,':C+:F1.:C-');
  Result := SendKeys(H,':RET.');
end;

{$ELSE not Windows}

uses unix;

const maxvk=12;
  vk : Array[1..maxvk] of record
                            tkn : String[8];
                            val : String[5];
                           end
    = (
    ( tkn: ':TAB.' ; val: #9),
    ( tkn: ':RET.' ; val: #10),
    ( tkn: ':ESC.' ; val: #27),
    ( tkn: ':ERASE.' ; val: #132+'3'+#133),
    ( tkn: ':UP.'  ;   val: #132+'E'+#133),
    ( tkn: ':RIGHT.' ; val: #132+'D'+#133),
    ( tkn: ':LEFT.';   val: #132+'S'+#133),
    ( tkn: ':DOWN.';   val: #132+'X'+#133),
    ( tkn: ':DEFCPU.'; val: #252),
    ( tkn: ':MAXCPU.'; val: #253),
    ( tkn: ':RESET.';  val: #254),
    ( tkn: ':WAIT' ;   val: #246));

var pout : Textfile;

function GetWindowHandle(idx:Integer;par:String): THandle;
begin
  CASE idx of
    0 : Result := 0;
    1 : Result := 0;
    2 : Result := 0;
    3 : Result := 0;
    //    4 : Result := FileOpen(par,fmOpenWrite+fmShareDenyNone);
    4 : if FileExists(par) then begin
          assign(pout,par);
          rewrite(pout);
          Result := 99;
        end else Result := 0;
  end;
end;

Function SendKeys(H:Thandle;s:String):Boolean;
var i,c : integer;
begin
  s := UpperCase(trim(s));
  for i:=1 to maxvk do s:=StringReplace(s,vk[i].tkn,vk[i].val,[rfReplaceAll]);
  i:=1;
  while i<=length(s) do begin
    c := integer(s[i]);
    case c of
      10  : begin Write(pout,chr(c));Flush(pout);end;
      246 : begin Flush(pout);Sleep(StrToInt(copy(s,i+1,1))*1000); inc(i,2); end;
      else begin
        //FileWrite(H,c,1);
        Write(pout,chr(c));
        //Sleep(WinKeySendDelay);
      end;
    end;
    inc(i);
  end;
  Flush(pout);
  Sleep(WinKeySendDelay*4);
  Result := true;
end;

function SendString(H:Thandle;s:String):Boolean;
begin
   Result := false;
end;

{$ENDIF}

begin
  WinKeySendDelay := 20
end.

