Board index » delphi » exec procedure - keypresses to prog??

exec procedure - keypresses to prog??

I am trying to run a program using Exec procedure in BP 7.0. The
sample code in help and the programmers guide doesn't work, but I
got things going using Glenn Grotzinger's excellent tutorial.

Trouble is, the prog I called needs the space bar pressed in order
to exit to DOS, and thence to my program, where it is used again
with different parameters. Is there a way of passing these
keypresses to the program so that it terminates without me having
to press any keys?

I've tried lots of variations in the 'command' passed via Exec in
the line -- exec(getenv('COMSPEC'), command);-- but to no avail.

Is there a way of getting these keystrokes to the program, or will I
have to press the keys myself?  I am very much the beginner, so
if any solution is in assembler, please make it clear. Many thanks
in anticipation.      Tony

 

Re:exec procedure - keypresses to prog??


Quote
On Sun, 12 Oct 1997, Tony Gill wrote:
> Trouble is, the prog I called needs the space bar pressed in order
> to exit to DOS, and thence to my program, where it is used again
> with different parameters. Is there a way of passing these
> keypresses to the program so that it terminates without me having
> to press any keys?

> I've tried lots of variations in the 'command' passed via Exec in
> the line -- exec(getenv('COMSPEC'), command);-- but to no avail.

> Is there a way of getting these keystrokes to the program, or will I
> have to press the keys myself?  I am very much the beginner, so
> if any solution is in assembler, please make it clear. Many thanks
> in anticipation.      Tony

You can't just use the COMSPEC with a "command" you have to give the
COMSPEC the parameters to tell it you want to give it a program to run.

'/c filename.bat'    will tell COMSPEC to execute the file filename.bat
                     ( a batchfile) and then exit once it has done so.
'/k filename.bat'    tells COMSPEC to execute the filename.bat (batchfile)
                     but to continue on afterward as if it was called with
                     no parameters
'/c filename.exe'    will execute a program with the COMSPEC called
                     filename.exe

And to answer you actual question of how to send keys to the program you
are running (This ONLY works *IF* the program reads from Standard Input):

{$M 16384,0,0}     { You must remember to give up memory so DOS can run prgs}

var  textfile: text;

BEGIN
 Assign(textfile, 'keys.dat');
 ReWrite(textfile);
  Write(textfile,#13);  { same as enter }
 Close(textfile);

 swapvectors;
  Exec(GetEnv('COMSPEC'), '/c date <keys.dat');
 swapvectors;

 Erase(textfile);
END.

--
 Jeff Patterson                       Internet: aa...@fan.nb.ca
  Author of jpIRC DOS IRC Client        PGPKey: pgp-public-k...@keys.pgp.net
| HomePage: http://www.geocities.com/SiliconValley/Vista/7104/               |
| PGP Info: 2048/A8A1DCD5 : E0 9E 9B EF C8 E4 68 3D  B5 9C 72 4C EC 61 DD 7A |

Re:exec procedure - keypresses to prog??


On Mon, 13 Oct 1997 23:57:40 -0300, Jeff Patterson

Quote
<aa...@fan1.fan.nb.ca> wrote:
>On Sun, 12 Oct 1997, Tony Gill wrote:

>> Trouble is, the prog I called needs the space bar pressed in order
>> to exit to DOS, and thence to my program, where it is used again
>> with different parameters. Is there a way of passing these
>> keypresses to the program so that it terminates without me having
>> to press any keys?

>> I've tried lots of variations in the 'command' passed via Exec in
>> the line -- exec(getenv('COMSPEC'), command);-- but to no avail.

>> Is there a way of getting these keystrokes to the program, or will I
>> have to press the keys myself?  I am very much the beginner, so
>> if any solution is in assembler, please make it clear. Many thanks
>> in anticipation.      Tony

>You can't just use the COMSPEC with a "command" you have to give the
>COMSPEC the parameters to tell it you want to give it a program to run.

>'/c filename.bat'    will tell COMSPEC to execute the file filename.bat
>                     ( a batchfile) and then exit once it has done so.
>'/k filename.bat'    tells COMSPEC to execute the filename.bat (batchfile)
>                     but to continue on afterward as if it was called with
>                     no parameters
>'/c filename.exe'    will execute a program with the COMSPEC called
>                     filename.exe

>And to answer you actual question of how to send keys to the program you
>are running (This ONLY works *IF* the program reads from Standard Input):

>{$M 16384,0,0}     { You must remember to give up memory so DOS can run prgs}

>var  textfile: text;

>BEGIN
> Assign(textfile, 'keys.dat');
> ReWrite(textfile);
>  Write(textfile,#13);  { same as enter }
> Close(textfile);

> swapvectors;
>  Exec(GetEnv('COMSPEC'), '/c date <keys.dat');
> swapvectors;

> Erase(textfile);
>END.

If your program does not use standard input, there may be another way,
using the following small program.

Bob Ferguson.

{ KEYSIM.PAS : Keyboard simulation program

  Titel   : KEYSIM
  Version : 1.1
  Date    : Dec 17,1996
  Author  : J. Theunisz in HCC Nieuwsbrief 126, march 1990
            Slightly modified and translated into English by
            Bob Ferguson
  Language: Turbo Pascal v4.0 or higher

  KEYSIM stores its command parameters into the keyboard buffer.
  Use this program in a batch file before the program that needs
  this text as input.
  Example
    KEYSIM Y
    progname
  The program "progname" will get "Y" as an answer to its first
  question.

  Type KEYSIM without parameters for an explanation how to enter
  special key codes like function keys.

Quote
}

PROGRAM KEYSIM;

var x,z,s: byte; code: integer; stg: string;

procedure Help;
begin
  writeln('KEYSIM v1.1   (c) 19-1-1989 by J. Theunisz  Haarlem, NL');
  writeln('Syntax: KEYSIM string1 [string2 ...]');
  writeln('Strings may contain [nnn] where "nnn" is a decimal ASCII');
  writeln('code. For instance [013] for ENTER. Function keys are');
  writeln('coded as [000;sss]. For instance F10=[000;068]');
  writeln('The keyboard buffer will hold 15 key codes.');
end;

procedure Submit(asci,scan: byte);
var
  BufHead: byte absolute $0000:$041A;
  BufTail: byte absolute $0000:$041C;
  BufKeyb: array[0..31] of byte absolute $0000:$041E;
begin
  BufKeyb[BufTail-30]:= asci; BufTail:= BufTail + 1;
  BufKeyb[BufTail-30]:= scan; BufTail:= BufTail + 1;
  if BufTail > 60 then BufTail:= 30;
end;

begin { program }
  if ParamCount = 0 then Help
  else begin
    stg:= ParamStr(1);
    for x:= 2 to ParamCount do stg:= Concat(stg,' ',ParamStr(x));
    x:= 1;
    repeat
      if stg[x] <> '[' then begin
        Submit(Ord(stg[x]),$1E); x:= x+1
      end
      else begin
        Val(Copy(stg,x+1,3),z,code);
        if stg[x+4] <> ';' then begin
          Submit(z,$1E); x:= x+5
        end
        else begin
          Val(Copy(stg,x+5,3),s,code); Submit(z,s); x:= x+9
        end;
      end;
    until x > Length(stg);
  end;
end.

--
J.R. Ferguson, Amsterdam, The Netherlands
e-mail: j.r.fergu...@iname.com

Re:exec procedure - keypresses to prog??


In article <3443a976.6321...@news.xs4all.nl> of Tue, 14 Oct 1997
17:35:24 in comp.lang.pascal.borland, "J.R. Ferguson"

Quote
<j.r.fergu...@iname.com> wrote:

>If your program does not use standard input, there may be another way,
>using the following small program.

>Bob Ferguson.

>{ KEYSIM.PAS : Keyboard simulation program

>  Titel   : KEYSIM
>  Version : 1.1
>  Date    : Dec 17,1996
>  Author  : J. Theunisz in HCC Nieuwsbrief 126, march 1990
>            Slightly modified and translated into English by
>            Bob Ferguson
>  Language: Turbo Pascal v4.0 or higher

>  KEYSIM stores its command parameters into the keyboard buffer.
>  Use this program in a batch file before the program that needs
>  this text as input.
>  Example
>    KEYSIM Y
>    progname
>  The program "progname" will get "Y" as an answer to its first
>  question.

>  Type KEYSIM without parameters for an explanation how to enter
>  special key codes like function keys.
>}

>PROGRAM KEYSIM;

>var x,z,s: byte; code: integer; stg: string;

>procedure Help;
>begin
>  writeln('KEYSIM v1.1   (c) 19-1-1989 by J. Theunisz  Haarlem, NL');
>  writeln('Syntax: KEYSIM string1 [string2 ...]');
>  writeln('Strings may contain [nnn] where "nnn" is a decimal ASCII');
>  writeln('code. For instance [013] for ENTER. Function keys are');
>  writeln('coded as [000;sss]. For instance F10=[000;068]');
>  writeln('The keyboard buffer will hold 15 key codes.');
>end;

>procedure Submit(asci,scan: byte);
>var
>  BufHead: byte absolute $0000:$041A;
>  BufTail: byte absolute $0000:$041C;
>  BufKeyb: array[0..31] of byte absolute $0000:$041E;
>begin
>  BufKeyb[BufTail-30]:= asci; BufTail:= BufTail + 1;
>  BufKeyb[BufTail-30]:= scan; BufTail:= BufTail + 1;
>  if BufTail > 60 then BufTail:= 30;
>end;
> ...

***Ideally***, not $0000:$041A but $0040:$001A, or, newer,
Seg0040:%001A; but this does not matter.

Some subtraction would be saved by making BufKeyb [30..61] or [$1E..$3D]

BUT the  more important point is that the buffer is not necessarily at
$40:$1E, nor is it $10 characters long; there are programs to extend it.
The buffer starts at $40:MemW[$40:$80] and ends at $40:MemW[$40:$82].

KBBUFDEV is such a program - I forget where it was printed, but I last
created kbbufdev.sys on 90/02/09.

Int $16/05 is said to write a scancode to the buffer of an enhanced
keyboard on a PC/AT or PS/2 - it *ought* to recognise extended buffers.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ - FAQqish topics, acronyms and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Other Threads