Recent

Author Topic: Console Program - Keyboard Simulation  (Read 240 times)

Ruler

  • New Member
  • *
  • Posts: 10
Console Program - Keyboard Simulation
« on: February 19, 2021, 07:32:36 pm »
I am writing a small console program.  It opens, waits for the user, accepts a small chunk of data from a barcode reader (system sees it as a usb keyboard), does some processing on that data, then puts the resulting information in the clipboard buffer so that the user can paste it into another program.  Works perfectly as evidenced by pasting the data into a text document.

Unfortunately, the program the user needs the processed information in does not accept tab characters to move between fields on the screen when using the paste function.  Works perfectly fine when you're at the keyboard and press the tab key, but not when pasting.  The client machine is under windoze 10.

The only solution I can think of is to somehow either close the console window for the program with it still running or switch to the previously active program and then simulate the keys being pressed on a their keyboard so the input will go into the third party program.  Don't know if the former is possible with a console application.  As far as the latter, I simply do not know how to go about simulating key presses in Lazarus.  If I'm able to get it working this way, I won't have to mess with the user's clipboard buffer either. :)

Anyone have any ideas or hints?  (Or see another method of accomplishing this?)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2093
Re: Console Program - Keyboard Simulation
« Reply #1 on: February 19, 2021, 08:19:26 pm »
I'm not a Windows user but the term you're looking for is "wedge program" hence e.g. Google for pascal keyboard wedge program[\tt]

Anything useful in https://forum.lazarus.freepascal.org/index.php?topic=15456.0 ?

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Ruler

  • New Member
  • *
  • Posts: 10
Re: Console Program - Keyboard Simulation
« Reply #2 on: February 19, 2021, 10:20:34 pm »
Thanks for the reply.

The thread you linked had a reference to 'MouseAndKeyInput'.  Going from there, I was able to get things operational:

Code: Pascal  [Select][+][-]
  1. uses ... <added MouseAndKeyInput, Interfaces, LCLType, KeyInputIntf>
  2. ...
  3.   KeyInput.Down(VK_MENU);   //The alt key is VK_MENU.  Have to use Down() & Up() rather than Press() for this one
  4.   KeyInput.Press(VK_TAB);   //Simple enough...
  5.   KeyInput.Up(VK_MENU);   //Let alt key up
  6.   KeyInput.Press(data1);   //String to send
  7.   KeyInput.Press(VK_TAB);   //Move to next field
  8.   KeyInput.Press(data2);   //String to put in next field
  9. ...
  10.  


I've tested it with a text editor and it works perfectly!  :D   Just hope that it works as well when interfacing with the actual program - I'll be able to test this when our office girl gets in on Monday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2093
Re: Console Program - Keyboard Simulation
« Reply #3 on: February 19, 2021, 10:31:08 pm »
Well done, glad to be able to help.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Ruler

  • New Member
  • *
  • Posts: 10
Re: Console Program - Keyboard Simulation
« Reply #4 on: February 23, 2021, 10:11:25 pm »
For the sake of posterity, one needs a Sleep() after the Alt-Tab is 'pressed' or the above doesn't work on Windoze 10, even though it does on XP & 7.  (Gotta love microsoft.  >:( )  I found 1500 ms works, though didn't push it down to see how fast it could be done before causing problems.  For my application, a second and a half is completely acceptable as it saves the user between 45 and 60 seconds & I'd much rather have it be slower and more guaranteed of working than perhaps not functioning.

 

TinyPortal © 2005-2018