在 Java 程序 运行 中生成键盘输出作为 Windows 服务

Generate Keyboard Output in Java Program running as Windows Service

我创建了一个简单的独立 Java 应用程序 (JAR),它通过虚拟 COM 端口连接到霍尼韦尔条码扫描器(使用 RxTx 库来设置 COM 连接)。

连接后,程序接收来自扫描仪的输入,使用一些自定义逻辑对其进行转换,最后使用 Robot class 将转换后的输出重定向到标准输出。没什么特别复杂的。

所以,如果我 运行 程序然后扫描一个值 "A",应用程序将其转换为 "B" 并且当记事本处于活动状态时(或其他一些输入 field/program), "B" 输出就像它最初是由扫描仪扫描的一样。

当 运行 作为 jar 文件(或包装 bat 文件)时,此程序 运行 非常完美。但是,当将此 JAR/BAT 文件包装为 windows 服务(使用 JSW Community Edition 或 YAJSW)时,程序将完美运行,但不会输出执行的 "mimicked" 击键通过 Robot.class.

我什至用过 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 但这也没用。目前我运行别无选择。

所以我的问题是:当 运行ning 作为 Windows 服务时,您不能生成键盘按下事件是否正确?我可以想象这可能是一个很大的安全问题,因此可能是不允许的。

非常欢迎任何反馈或可能的解决方案!非常感谢!

这是从Windows Vista引入的称为session 0 isolation的规范。
作为Windows服务的程序没有用户界面。
请参考这些文章和文档。

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


另外
请尝试注册到任务计划程序。
Register task in windows task scheduler in java

那样的话,除非是"Run only when user is logged on".
否则可能无效 这是一篇关于 Windows 服务器的文章。
show window of task scheduler schedule program.

但是,也有可能即使做了也不行。
Simple c++ program fails to run as a scheduled-task (interactive/non-interactive issue?)