Search code examples
javaserial-portwindows-servicesbarcode-scannerkeyboard-events

Generate Keyboard Output in Java Program running as Windows Service


I created a simple standalone Java application (JAR) that connects to a Honeywell barcode scanner over a virtual COM port (using RxTx libraries for setting up the COM connection).

After connecting, the program then receives the input from the scanner, transforms it using some custom logic and finally redirects the transformed output to the standard out using the Robot class. Nothing really complex.

So, if I run the program and then scan an value "A", the app transforms it into "B" and when notepad is active (or some other input field/program), "B" is outputted as if it was originally scanned by the scanner.

This program runs perfectly when run as a jar file (or wrapping bat file). However, when wrapping this JAR/BAT file as a windows service (using JSW Community Edition or YAJSW) the program will function perfectly, but will not output the "mimicked" keystrokes performed by the Robot.class.

I even used JNA libraries with the sendInput() method (as replacement for the Robot class) to create the keystrokes as close to the OS level as possbile, but this didn't work also... I also tried to make the service interactive but also this didnt work. Currently i am running out of options.

So my question is: is it correct that you cannot generate keyboard press events when running as a Windows Service? I could imagine that this could be a big security problem and maybe therefore it is disallowed.

Any feedback or possible solution would be very welcome! Many Thanks in advance!


Solution

  • It is a specification called session 0 isolation, which was introduced from Windows Vista.
    Programs that act as WindowsService do not have a user interface.
    Please refer to these articles and documents.

    Application Compatibility – Session 0 Isolation
    Session 0 Isolation - Microsoft Download Center

    What is Session 0 Isolation? What do I need to know about it?
    Session 0 Isolation and Secure Desktop: Windows 7 AppCompat Series


    In Addition
    Please try registering to Task Scheduler.
    Register task in windows task scheduler in java

    In that case, it may be ineffective unless it is "Run only when user is logged on".
    This is an article on Windows Server.
    show window of task scheduler schedule program.

    However, there is a possibility that it will not work even if you do it.
    Simple c++ program fails to run as a scheduled-task (interactive/non-interactive issue?)