How to simulate keyboard presses in Scala (shell) script?

时间:2015-07-28 16:06:43

标签: java scala shell keyboard-events headless

I want to simulate a key pressed event in my Scala script. (Clarification: it is the same program who shall receive the key pressed event.)

However, I can't instatiate java.awt.Robot.

#!/bin/bash
exec scala $0
!#
new java.awt.Robot()

leads to

java.awt.AWTException: headless environment
        at java.awt.Robot.<init>(Robot.java:94)

and

#!/bin/bash
exec scala $0
!#
System.setProperty("java.awt.headless", "false")
new java.awt.Robot()

leads to

java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
        at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
        at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
        at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:110)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:188)
        at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
        at java.awt.Robot.<init>(Robot.java:96)

So it looks like Robot not only needs a keyboard, but also a graphical display (and I only have a simple text terminal).

Any idea how to do this (with or without java.awt.Robot)?

0 个答案:

没有答案